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.



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

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
    self.class.new_xml_document(xml).root
  else
    self.class.initialize_xml_document.root
  end

  @proxies = Hash.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



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/google_contacts/base.rb', line 133

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.



13
14
15
# File 'lib/google_contacts/base.rb', line 13

def xml
  @xml
end

Class Method Details

.feed_for_batchObject



44
45
46
# File 'lib/google_contacts/base.rb', line 44

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.



98
99
100
# File 'lib/google_contacts/base.rb', line 98

def [](prop)
  properties[prop]
end

#[]=(prop, value) ⇒ Object



102
103
104
# File 'lib/google_contacts/base.rb', line 102

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

#attributes=(attrs) ⇒ Object



30
31
32
33
34
# File 'lib/google_contacts/base.rb', line 30

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

#changed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/google_contacts/base.rb', line 82

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

#deleteObject



92
93
94
95
# File 'lib/google_contacts/base.rb', line 92

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.



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

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



69
70
71
# File 'lib/google_contacts/base.rb', line 69

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

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



36
37
38
# File 'lib/google_contacts/base.rb', line 36

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

#nameObject

Alias “name” to “title”



107
108
109
# File 'lib/google_contacts/base.rb', line 107

def name
  method_missing(:title)
end

#name=(v) ⇒ Object



111
112
113
# File 'lib/google_contacts/base.rb', line 111

def name=(v)
  method_missing(:title=, v)
end

#new?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/google_contacts/base.rb', line 65

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

#remove_xml(tag) ⇒ Object



40
41
42
# File 'lib/google_contacts/base.rb', line 40

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

#saveObject



86
87
88
89
90
# File 'lib/google_contacts/base.rb', line 86

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

#updated_atObject



73
74
75
# File 'lib/google_contacts/base.rb', line 73

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

#url(rel) ⇒ Object



77
78
79
80
# File 'lib/google_contacts/base.rb', line 77

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