Class: GoogleContacts::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/google_contacts/base.rb

Direct Known Subclasses

Contact, Group

Constant Summary collapse

NAMESPACES =
{
  'atom'        => 'http://www.w3.org/2005/Atom',
  'openSearch'  => 'http://a9.com/-/spec/opensearch/1.1/',
  'gContact'    => 'http://schemas.google.com/contact/2008',
  'batch'       => 'http://schemas.google.com/gdata/batch',
  'gd'          => 'http://schemas.google.com/g/2005',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapper, xml = nil) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/google_contacts/base.rb', line 12

def initialize(wrapper, xml = nil)
  raise "Cannot create instance of Base" if self.class.name.split(/::/).last == 'Base'
  @wrapper = wrapper

  # If a root node is given, create a new XML document based on
  # a deep copy. Otherwise, initialize a new XML document.
  @xml = if xml.present?
    self.class.new_xml_document(xml).root
  else
    self.class.initialize_xml_document.root
  end

  @proxies = HashWithIndifferentAccess.new
  register_base_proxies
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object (protected)

Try to proxy missing method to one of the proxies



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/google_contacts/base.rb', line 125

def method_missing(sym, *args, &blk)
  if sym.to_s =~ /^(\w+)(=)?$/ && @proxies[$1]
    if $2
      @proxies[$1].replace(args.first)
    else
      @proxies[$1]
    end
  else
    super
  end
end

Instance Attribute Details

#xmlObject (readonly)

Returns the value of attribute xml.



11
12
13
# File 'lib/google_contacts/base.rb', line 11

def xml
  @xml
end

Class Method Details

.feed_for_batchObject



42
43
44
# File 'lib/google_contacts/base.rb', line 42

def self.feed_for_batch
  new_xml_document('feed').root
end

Instance Method Details

#[](prop) ⇒ Object

Hash-like access to extended properties of both contacts and groups.



96
97
98
# File 'lib/google_contacts/base.rb', line 96

def [](prop)
  properties[prop]
end

#[]=(prop, value) ⇒ Object



100
101
102
# File 'lib/google_contacts/base.rb', line 100

def []=(prop, value)
  properties[prop] = value
end

#attributes=(attrs) ⇒ Object



28
29
30
31
32
# File 'lib/google_contacts/base.rb', line 28

def attributes=(attrs)
  attrs.each_pair do |key, value|
    send("#{key}=", value)
  end
end

#changed?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/google_contacts/base.rb', line 80

def changed?
  @proxies.values.any?(&:changed?)
end

#deleteObject



90
91
92
93
# File 'lib/google_contacts/base.rb', line 90

def delete
  return if new?
  @wrapper.append_operation(self, :delete)
end

#entry_for_batch(operation) ⇒ Object

Create new XML::Document that can be used in a Google Contacts batch operation.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/google_contacts/base.rb', line 48

def entry_for_batch(operation)
  root = self.class.new_xml_document(xml).root
  root.xpath('./xmlns:link'   ).remove
  root.xpath('./xmlns:updated').remove

  if operation == :update || operation == :delete
    root.at('./xmlns:id').content = url(:edit)
  end

  self.class.insert_xml(root, 'batch:id')
  self.class.insert_xml(root, 'batch:operation', :type => operation)

  root
end

#hrefObject



67
68
69
# File 'lib/google_contacts/base.rb', line 67

def href
  xml.at_xpath('./xmlns:id').text.strip unless new?
end

#insert_xml(tag, attributes = {}, &blk) ⇒ Object



34
35
36
# File 'lib/google_contacts/base.rb', line 34

def insert_xml(tag, attributes = {}, &blk)
  self.class.insert_xml(@xml, tag, attributes, &blk)
end

#new?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/google_contacts/base.rb', line 63

def new?
  xml.at_xpath('./xmlns:id').nil?
end

#remove_xml(tag) ⇒ Object



38
39
40
# File 'lib/google_contacts/base.rb', line 38

def remove_xml(tag)
  @xml.xpath(tag).remove
end

#saveObject



84
85
86
87
88
# File 'lib/google_contacts/base.rb', line 84

def save
  return unless changed?
  synchronize_proxies
  @wrapper.append_operation(self, new? ? :insert : :update)
end

#updated_atObject



71
72
73
# File 'lib/google_contacts/base.rb', line 71

def updated_at
  Time.parse xml.at_xpath('./xmlns:updated').text.strip unless new?
end

#url(rel) ⇒ Object



75
76
77
78
# File 'lib/google_contacts/base.rb', line 75

def url(rel)
  rel = 'http://schemas.google.com/contacts/2008/rel#photo' if rel == :photo
  xml.at_xpath(%{xmlns:link[@rel="#{rel}"]})[:href]
end