Class: Contacts::Google::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/contacts/google.rb

Direct Known Subclasses

Contact, Group

Constant Summary collapse

BASE_XML =
"<entry><category scheme='http://schemas.google.com/g/2005#kind' /><title type='text' /></entry>"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gmail, xml = nil) ⇒ Base

Returns a new instance of Base.



281
282
283
284
285
# File 'lib/contacts/google.rb', line 281

def initialize(gmail, xml = nil)
  xml = BASE_XML if xml.nil?
  @xml = Hpricot(xml.to_s, :xml => true)
  @gmail = gmail
end

Instance Attribute Details

#gmailObject (readonly)

Returns the value of attribute gmail.



277
278
279
# File 'lib/contacts/google.rb', line 277

def gmail
  @gmail
end

#xmlObject (readonly)

Returns the value of attribute xml.



277
278
279
# File 'lib/contacts/google.rb', line 277

def xml
  @xml
end

Instance Method Details

#[](attr) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/contacts/google.rb', line 316

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



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/contacts/google.rb', line 327

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}' />", :xml => true)
    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



356
357
358
359
360
361
# File 'lib/contacts/google.rb', line 356

def create!
  raise "Cannot create existing entry" unless new?
  response = gmail.post(create_url, document_for_request, {
    'Content-Type' => 'application/atom+xml'
  })
end

#create_urlObject



348
349
350
# File 'lib/contacts/google.rb', line 348

def create_url
  raise "Contacts::Google::Base must be subclassed!"
end

#delete!Object



371
372
373
374
375
376
# File 'lib/contacts/google.rb', line 371

def delete!
  raise "Cannot delete new entry" if new?
  gmail.post(edit_url, document_for_request,
    'X-HTTP-Method-Override' => 'DELETE'
  )
end

#edit_urlObject



352
353
354
# File 'lib/contacts/google.rb', line 352

def edit_url
  @xml.at("link[@rel='edit']")['href'].gsub(/^http:\/\/www.google.com(\/.*)$/, '\1') unless new?
end

#idObject



298
299
300
# File 'lib/contacts/google.rb', line 298

def id
  @xml.at('id').inner_html unless new?
end

#load_attributes(attr) ⇒ Object



287
288
289
290
291
292
# File 'lib/contacts/google.rb', line 287

def load_attributes(attr)
  attr.each do |k,v|
    self.send((k.to_s+"=").to_sym, v)
  end
  self
end

#nameObject



302
303
304
# File 'lib/contacts/google.rb', line 302

def name
  @xml.at('title').inner_html
end

#name=(str) ⇒ Object



306
307
308
# File 'lib/contacts/google.rb', line 306

def name=(str)
  @xml.at('title').inner_html = str
end

#new?Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/contacts/google.rb', line 294

def new?
  @xml.at('id').nil?
end

#update!Object



363
364
365
366
367
368
369
# File 'lib/contacts/google.rb', line 363

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

#updated_atObject



310
311
312
313
314
# File 'lib/contacts/google.rb', line 310

def updated_at
  t = @xml.at('updated')
  return nil if t.nil?
  Time.parse(t.inner_html)
end