Class: GoogleR::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/google_r/contact.rb

Defined Under Namespace

Classes: Address, Email, Organization, Phone, Website

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContact

Returns a new instance of Contact.



14
15
16
17
18
19
20
21
22
# File 'lib/google_r/contact.rb', line 14

def initialize
  @emails = []
  @phones = []
  @organizations = []
  @addresses = []
  @groups = []
  @websites = []
  @user_fields = {}
end

Instance Attribute Details

#additional_nameObject

Returns the value of attribute additional_name.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def additional_name
  @additional_name
end

#addressesObject (readonly)

Returns the value of attribute addresses.



10
11
12
# File 'lib/google_r/contact.rb', line 10

def addresses
  @addresses
end

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def content
  @content
end

#emailsObject (readonly)

Returns the value of attribute emails.



10
11
12
# File 'lib/google_r/contact.rb', line 10

def emails
  @emails
end

#etagObject

Returns the value of attribute etag.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def etag
  @etag
end

#family_nameObject

Returns the value of attribute family_name.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def family_name
  @family_name
end

#given_nameObject

Returns the value of attribute given_name.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def given_name
  @given_name
end

#google_idObject

Returns the value of attribute google_id.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def google_id
  @google_id
end

#groupsObject (readonly)

Returns the value of attribute groups.



10
11
12
# File 'lib/google_r/contact.rb', line 10

def groups
  @groups
end

#name_prefixObject

Returns the value of attribute name_prefix.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def name_prefix
  @name_prefix
end

#name_suffixObject

Returns the value of attribute name_suffix.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def name_suffix
  @name_suffix
end

#nicknameObject

Returns the value of attribute nickname.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def nickname
  @nickname
end

#organizationsObject (readonly)

Returns the value of attribute organizations.



10
11
12
# File 'lib/google_r/contact.rb', line 10

def organizations
  @organizations
end

#phonesObject (readonly)

Returns the value of attribute phones.



10
11
12
# File 'lib/google_r/contact.rb', line 10

def phones
  @phones
end

#updatedObject

Returns the value of attribute updated.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def updated
  @updated
end

#user_fieldsObject

Returns the value of attribute user_fields.



11
12
13
# File 'lib/google_r/contact.rb', line 11

def user_fields
  @user_fields
end

#websitesObject (readonly)

Returns the value of attribute websites.



10
11
12
# File 'lib/google_r/contact.rb', line 10

def websites
  @websites
end

Class Method Details

.api_headersObject



32
33
34
35
36
37
# File 'lib/google_r/contact.rb', line 32

def self.api_headers
  {
    'GData-Version' => '3.0',
    'Content-Type' => 'application/atom+xml',
  }
end

.from_xml(doc, *attrs) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/google_r/contact.rb', line 152

def self.from_xml(doc, *attrs)
  is_collection = doc.search("totalResults").size > 0
  return doc.search("entry").map { |e| from_xml(e) } if is_collection

  contact = GoogleR::Contact.new

  google_id = doc.search("id")

  if google_id.empty?
    contact.etag = contact.google_id = nil
  else
    contact.etag = doc["etag"]
    contact.google_id = google_id.inner_text
  end

  doc.search("email").each do |email|
    contact.add_email(GoogleR::Contact::Email.new(email[:address], email[:display_name], email[:label], email[:rel], email[:primary] == "true"))
  end

  doc.search("phoneNumber").each do |phone|
    contact.add_phone(GoogleR::Contact::Phone.new(phone[:rel], phone.inner_text))
  end

  doc.search("organization").each do |org|
    name = org.search("orgName").inner_text
    title = org.search("orgTitle").inner_text
    rel = org[:rel]
    contact.add_organization(GoogleR::Contact::Organization.new(name, title, rel))
  end

  doc.search("structuredPostalAddress").each do |address|
    rel = address[:rel]
    street = address.search("street").inner_text
    neighborhood = address.search("neighborhood").inner_text
    pobox = address.search("pobox").inner_text
    postcode = address.search("postcode").inner_text
    city = address.search("city").inner_text
    region = address.search("region").inner_text
    country = address.search("country").inner_text
    contact.add_address(GoogleR::Contact::Address.new(street, neighborhood, pobox, postcode, city, region, country, rel))
  end

  doc.search("userDefinedField").each do |field|
    contact.user_fields[field[:key]] = field[:value]
  end

  doc.search("groupMembershipInfo").each do |entry|
    group = GoogleR::Group.new
    group.google_id = entry[:href]
    contact.add_group(group)
  end

  doc.search("website").each do |entry|
    website = GoogleR::Contact::Website.new
    website.href = entry[:href]
    website.rel = entry[:rel]
    contact.add_website(website)
  end

  name_prefix = doc.search("name/namePrefix")
  contact.name_prefix = name_prefix.inner_text unless name_prefix.empty?

  given_name = doc.search("name/givenName")
  contact.given_name = given_name.inner_text unless given_name.empty?

  additional_name = doc.search("name/additionalName")
  contact.additional_name = additional_name.inner_text unless additional_name.empty?

  family_name = doc.search("name/familyName")
  contact.family_name = family_name.inner_text unless family_name.empty?

  name_suffix = doc.search("name/nameSuffix")
  contact.name_suffix = name_suffix.inner_text unless name_suffix.empty?

  content = doc.search("content")
  contact.content = content.inner_text unless content.empty?

  updated = doc.search("updated")
  contact.updated = Time.parse(updated.inner_text) unless updated.empty?

  nickname = doc.search("nickname")
  contact.nickname = nickname.inner_text unless nickname.empty?

  contact
end

.pathObject



28
29
30
# File 'lib/google_r/contact.rb', line 28

def self.path
  "/m8/feeds/contacts/default/full/"
end

.urlObject



24
25
26
# File 'lib/google_r/contact.rb', line 24

def self.url
  "https://www.google.com"
end

Instance Method Details

#add_address(address) ⇒ Object



140
141
142
# File 'lib/google_r/contact.rb', line 140

def add_address(address)
  @addresses << address
end

#add_email(email) ⇒ Object



128
129
130
# File 'lib/google_r/contact.rb', line 128

def add_email(email)
  @emails << email
end

#add_group(group) ⇒ Object



144
145
146
# File 'lib/google_r/contact.rb', line 144

def add_group(group)
  @groups << group
end

#add_organization(organization) ⇒ Object



136
137
138
# File 'lib/google_r/contact.rb', line 136

def add_organization(organization)
  @organizations << organization
end

#add_phone(phone) ⇒ Object



132
133
134
# File 'lib/google_r/contact.rb', line 132

def add_phone(phone)
  @phones << phone
end

#add_website(website) ⇒ Object



148
149
150
# File 'lib/google_r/contact.rb', line 148

def add_website(website)
  @websites << website
end

#full_nameObject



47
48
49
# File 'lib/google_r/contact.rb', line 47

def full_name
  [name_prefix, given_name, additional_name, family_name, name_suffix].compact.join(" ")
end

#new?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/google_r/contact.rb', line 238

def new?
  self.google_id.nil?
end

#pathObject



39
40
41
42
43
44
45
# File 'lib/google_r/contact.rb', line 39

def path
  if new?
    self.class.path
  else
    self.class.path + google_id.split("/")[-1]
  end
end

#to_googleObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/google_r/contact.rb', line 51

def to_google
  builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
    root_attrs = {
      'xmlns:atom' => 'http://www.w3.org/2005/Atom',
      'xmlns:gd' => 'http://schemas.google.com/g/2005',
      'xmlns:gContact' => 'http://schemas.google.com/contact/2008',
    }
    root_attrs["gd:etag"] = self.etag unless new?
    xml.entry(root_attrs) do
      unless new?
        xml.id_ self.google_id
      end

      xml.updated self.updated.strftime("%Y-%m-%dT%H:%M:%S.%LZ") unless self.updated.nil?

      if self.full_name != ''
        xml['gd'].name do
          xml['gd'].givenName self.given_name unless self.given_name.nil?
          xml['gd'].additionalName self.additional_name unless self.additional_name.nil?
          xml['gd'].familyName self.family_name unless self.family_name.nil?
          xml['gd'].namePrefix self.name_prefix unless self.name_prefix.nil?
          xml['gd'].nameSuffix self.name_suffix unless self.name_suffix.nil?
        end
      end

      xml['atom'].content({'type' => 'text'}, self.content) unless self.content.nil?
      xml['gContact'].nickname self.nickname unless self.nickname.nil?

      phones.each do |phone|
        xml['gd'].phoneNumber({'rel' => phone.rel}, phone.text)
      end

      emails.each do |email|
        attrs = {'address' => email.address}
        attrs['rel'] = email.rel if email.rel
        attrs['label'] = email.label if email.label
        attrs['primary'] = email.primary
        xml['gd'].email(attrs)
      end

      organizations.each do |org|
        xml['gd'].organization({:rel => org.rel}) do
          xml['gd'].orgName org.name
          xml['gd'].orgTitle org.title
        end
      end

      addresses.each do |address|
        xml['gd'].structuredPostalAddress({'rel' => address.rel}) do
          xml['gd'].street address.street unless address.street.nil?
          xml['gd'].neighborhood address.neighborhood unless address.neighborhood.nil?
          xml['gd'].pobox address.pobox unless address.pobox.nil?
          xml['gd'].postcode address.postcode unless address.postcode.nil?
          xml['gd'].city address.city unless address.city.nil?
          xml['gd'].region address.region unless address.region.nil?
          xml['gd'].country address.country unless address.country.nil?
        end
      end

      user_fields.each do |key, value|
        xml['gContact'].userDefinedField({'key' => key, 'value' => value})
      end

      websites.each do |website|
        xml['gContact'].website({'href' => website.href, 'rel' => website.rel})
      end

      groups.each do |group|
        xml['gContact'].groupMembershipInfo({'href' => group.google_id, 'deleted' => 'false'})
      end

      xml.parent.namespace = xml.parent.namespace_definitions.find { |ns| ns.prefix == "atom" }
    end
  end
  builder.to_xml
end