Class: Contacts::Google::Base
- Inherits:
-
Object
- Object
- Contacts::Google::Base
- Defined in:
- lib/contacts/google.rb
Constant Summary collapse
- BASE_XML =
"<entry><category scheme='http://schemas.google.com/g/2005#kind' /><title type='text' /></entry>"
Instance Attribute Summary collapse
-
#gmail ⇒ Object
readonly
Returns the value of attribute gmail.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
- #[](attr) ⇒ Object
- #[]=(attr, value) ⇒ Object
- #create! ⇒ Object
- #create_url ⇒ Object
- #delete! ⇒ Object
- #edit_url ⇒ Object
- #id ⇒ Object
-
#initialize(gmail, xml = nil) ⇒ Base
constructor
A new instance of Base.
- #load_attributes(attr) ⇒ Object
- #name ⇒ Object
- #name=(str) ⇒ Object
- #new? ⇒ Boolean
- #update! ⇒ Object
Constructor Details
Instance Attribute Details
#gmail ⇒ Object (readonly)
Returns the value of attribute gmail.
278 279 280 |
# File 'lib/contacts/google.rb', line 278 def gmail @gmail end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
278 279 280 |
# File 'lib/contacts/google.rb', line 278 def xml @xml end |
Instance Method Details
#[](attr) ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/contacts/google.rb', line 311 def [](attr) el = get_extended_property(attr) return nil if el.nil? if el.has_attribute?('value') el['value'] else Hpricot(el.inner_html) end end |
#[]=(attr, value) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/contacts/google.rb', line 322 def []=(attr, value) el = get_extended_property(attr) # Create element if it not already exists if el.nil? @xml.root.children.push *Hpricot.make("<gd:extendedProperty name='#{attr}' />") el = get_extended_property(attr) end if value.kind_of?(Hpricot) # If value is valid XML, set as element content el.remove_attribute('value') el.inner_html = value.to_s else # If value is not XML, set as value-attribute el['value'] = value el.inner_html = '' end value end |
#create! ⇒ Object
351 352 353 354 355 356 |
# File 'lib/contacts/google.rb', line 351 def create! raise "Cannot create existing entry" unless new? response = gmail.post(create_url, document_for_request, { 'Content-Type' => 'application/atom+xml' }) end |
#create_url ⇒ Object
343 344 345 |
# File 'lib/contacts/google.rb', line 343 def create_url raise "Contacts::Google::Base must be subclassed!" end |
#delete! ⇒ Object
366 367 368 369 370 371 |
# File 'lib/contacts/google.rb', line 366 def delete! raise "Cannot delete new entry" if new? gmail.post(edit_url, document_for_request, 'X-HTTP-Method-Override' => 'DELETE' ) end |
#edit_url ⇒ Object
347 348 349 |
# File 'lib/contacts/google.rb', line 347 def edit_url @xml.at("link[@rel='edit']")['href'].gsub(/^http:\/\/www.google.com(\/.*)$/, '\1') unless new? end |
#id ⇒ Object
299 300 301 |
# File 'lib/contacts/google.rb', line 299 def id @xml.at('id').inner_html unless new? end |
#load_attributes(attr) ⇒ Object
288 289 290 291 292 293 |
# File 'lib/contacts/google.rb', line 288 def load_attributes(attr) attr.each do |k,v| self.send((k.to_s+"=").to_sym, v) end self end |
#name ⇒ Object
303 304 305 |
# File 'lib/contacts/google.rb', line 303 def name @xml.at('title').inner_html end |
#name=(str) ⇒ Object
307 308 309 |
# File 'lib/contacts/google.rb', line 307 def name=(str) @xml.at('title').inner_html = str end |
#new? ⇒ Boolean
295 296 297 |
# File 'lib/contacts/google.rb', line 295 def new? @xml.at('id').nil? end |
#update! ⇒ Object
358 359 360 361 362 363 364 |
# File 'lib/contacts/google.rb', line 358 def update! raise "Cannot update new entry" if new? response = gmail.post(edit_url, document_for_request, 'Content-Type' => 'application/atom+xml', 'X-HTTP-Method-Override' => 'PUT' ) end |