Class: Annotation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Annotation
- Defined in:
- app/models/annotation.rb
Overview
COPYRIGHT:
Copyright (c) 2005-2010 North Carolina State University
Developed with funding for the National eXtension Initiative.
LICENSE:
BSD(-compatible)
see LICENSE file or view at http://about.extension.org/wiki/LICENSE
Constant Summary
- @@client =
nil- @@currentuser =
Person.find(1)
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) add(url, user)
- - (Object) added_at=(microseconds)
- - (Object) after_initialize
- - (Object) log_add
- - (Object) log_delete
- - (Object) log_event(action, user)
- - (Object) remove(user)
Class Method Details
+ (Object) initial_setup
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'app/models/annotation.rb', line 192 def initial_setup self.login urls = @@client.getAnnotations added = errs = 0 urls.each do |url| begin a = Annotation.new(url) # do not use model validations, this allows us to add dupes that # may be present at google rc = a.save(false) if rc added += 1 else errs += 1 end rescue errs += 1 end end return {:added => added, :errs => errs} end |
+ (Object) login
175 176 177 178 179 180 181 |
# File 'app/models/annotation.rb', line 175 def login if @@client.nil? @@client = GData::Client::Cse.new rc = @@client.clientlogin(AppConfig.configtable['cse_uid'], AppConfig.configtable['cse_secret']) end end |
+ (Object) remove(href)
183 184 185 186 187 188 189 190 |
# File 'app/models/annotation.rb', line 183 def remove(href) rc = false result = @@client.removeAnnotation(href) if result rc = true end return rc end |
+ (Object) remove_dupes
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'app/models/annotation.rb', line 219 def remove_dupes self.login total = 0 annotes = Annotation.all urls = Hash.new hrefs = Hash.new annotes.each do |an| urls[an.url] += 1 if urls.has_key?(an.url) urls[an.url] = 1 if ! urls.has_key?(an.url) hrefs[an.url] = an.href end urls.each do |url, cnt| if cnt > 1 goner = hrefs[url] p "removing duplidate entry: #{url}" Annotation.remove(goner) total += 1 end end return total end |
Instance Method Details
- (Object) add(url, user)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/annotation.rb', line 49 def add(url, user) returnhash = Hash.new returnhash[:success] = false msg = "" result = false @@currentuser = user # remove leading protocol stuff so we can try to match # it locally before sending it to google url.gsub!(/^http(s)?(\:)+\/(\/)+/,'') dupe = Annotation.find_by_url(url) if dupe msg << "URL already included in search" else begin result = @@client.addAnnotation(url) rescue GData::Client::BadRequestError msg << "Malformed URL; please try again." rescue GData::Client::ServerError msg << "Server error; please try again later." rescue GData::Client::UnknownError msg << "Server error; invalid response from Google." rescue Exception => detail if detail.respond_to?(:response) msg << detail.response.body else msg << "Unknown error - #{detail.inspect}." end end end if result data = result.pop # set our object properties to match googles response data.each do |key, value| self.send("#{key}=", value) end if ! self.save # added a duplicate, need to remove # we replace the url the user provided with the form provided by # google in their response, if the save failed, it is because we # tried to add a dupe to our local db # to keep us in sync with google, we then issue a remove back to # google self.remove msg << "URL already included in search" else returnhash[:success] = true msg << "successfully added" end end returnhash[:msg] = msg return returnhash end |
- (Object) added_at=(microseconds)
155 156 157 158 |
# File 'app/models/annotation.rb', line 155 def added_at=(microseconds) # response from Google is microseconds as a string write_attribute(:added_at, Time.at(Integer(microseconds)/1000000).utc) end |
- (Object) after_initialize
151 152 153 |
# File 'app/models/annotation.rb', line 151 def after_initialize Annotation.login end |
- (Object) log_add
160 161 162 |
# File 'app/models/annotation.rb', line 160 def log_add self.log_event(AnnotationEvent::URL_ADDED, @@currentuser) end |
- (Object) log_delete
164 165 166 |
# File 'app/models/annotation.rb', line 164 def log_delete self.log_event(AnnotationEvent::URL_DELETED, @@currentuser) end |
- (Object) log_event(action, user)
168 169 170 171 |
# File 'app/models/annotation.rb', line 168 def log_event(action, user) opts = {:annotation => self, :action => action, :person => user} AnnotationEvent.log_event(opts) end |
- (Object) remove(user)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/models/annotation.rb', line 113 def remove(user) returnhash = Hash.new returnhash[:success] = false msg = "" result = false @@currentuser = user begin result = Annotation.remove(self.href) rescue GData::Client::BadRequestError msg << "Invalid href ID; may have already been removed." rescue GData::Client::ServerError msg << "Server error; please try again later." rescue GData::Client::UnknownError msg << "Server error; invalid response from Google." rescue Exception => detail if detail.respond_to?(:response) msg << detail.response.body else msg << "Unknown error - #{detail.inspect}." end end if result # we only destroy ourselves if we have an id (were saved in the db) # if we caught a dupe after the response came back from google, # then we would not have an id self.destroy if self.id returnhash[:success] = true msg << "successfully removed" end returnhash[:msg] = msg return returnhash end |