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.



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

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.



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

def gmail
  @gmail
end

#xmlObject (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_urlObject



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_urlObject



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

#idObject



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

#nameObject



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

Returns:

  • (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