Class: GoogleContactsApi::Contact

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

Overview

Represents a single contact. Methods we could implement: :categories, (:content again), :links, (:title again), :email :extended_properties, :deleted, :im, :name, :organizations, :phone_numbers, :structured_postal_addresses, :where

Instance Attribute Summary

Attributes inherited from Result

#api

Instance Method Summary collapse

Methods inherited from Result

#categories, #content, #deleted?, #id, #initialize, #inspect, #title, #updated

Constructor Details

This class inherits a constructor from GoogleContactsApi::Result

Instance Method Details

#additional_nameObject



134
135
136
# File 'lib/google_contacts_api/contact.rb', line 134

def additional_name
  nested_field_name_only 'gd$name', 'gd$additionalName'
end

#additional_name_yomiObject



137
138
139
# File 'lib/google_contacts_api/contact.rb', line 137

def additional_name_yomi
  nested_field_yomi_only 'gd$name', 'gd$additionalName'
end

#addressesObject

Return an Array of Hashes representing addresses with formatted metadata.



164
165
166
# File 'lib/google_contacts_api/contact.rb', line 164

def addresses
  format_entities('gd$structuredPostalAddress', :format_address)
end

Returns alternative, possibly off-Google home page link



20
21
22
23
# File 'lib/google_contacts_api/contact.rb', line 20

def alternate_link
  _link = self["link"].find { |l| l.rel == "alternate" }
  _link ? _link.href : nil
end

#birthdayObject



146
147
148
149
150
151
# File 'lib/google_contacts_api/contact.rb', line 146

def birthday
  if self['gContact$birthday']
    day, month, year = self['gContact$birthday']['when'].split('-').reverse
    { year: year == '' ? nil : year.to_i, month: month.to_i, day: day.to_i }
  end
end

#deleted_group_membershipsObject



203
204
205
# File 'lib/google_contacts_api/contact.rb', line 203

def deleted_group_memberships
  group_membership_info.select { |info| info[:deleted] }.map { |info| info[:href] }
end

Returns link to edit the contact



66
67
68
69
# File 'lib/google_contacts_api/contact.rb', line 66

def edit_link
  _link = self["link"].find { |l| l.rel == "edit" }
  _link ? _link.href : nil
end

Returns link to add/replace the photo



60
61
62
63
# File 'lib/google_contacts_api/contact.rb', line 60

def edit_photo_link
  _link = self["link"].find { |l| l.rel == "http://schemas.google.com/contacts/2008/rel#edit_photo" }
  _link ? _link.href : nil
end

#emailsObject

Returns all email addresses for the contact



77
78
79
# File 'lib/google_contacts_api/contact.rb', line 77

def emails
  self["gd$email"] ? self["gd$email"].map { |e| e.address } : []
end

#emails_fullObject

Return an Array of Hashes representing emails with formatted metadata.



186
187
188
# File 'lib/google_contacts_api/contact.rb', line 186

def emails_full
  format_entities('gd$email')
end

#etagObject



25
26
27
# File 'lib/google_contacts_api/contact.rb', line 25

def etag
  self['gd$etag']
end

#family_nameObject



125
126
127
# File 'lib/google_contacts_api/contact.rb', line 125

def family_name
  nested_field_name_only 'gd$name', 'gd$familyName'
end

#family_name_yomiObject



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

def family_name_yomi
  nested_field_yomi_only 'gd$name', 'gd$familyName'
end

#format_group_membership(membership) ⇒ Object



197
198
199
# File 'lib/google_contacts_api/contact.rb', line 197

def format_group_membership(membership)
  { deleted: membership['deleted'] == 'true', href: membership['href'] }
end

#full_nameObject



131
132
133
# File 'lib/google_contacts_api/contact.rb', line 131

def full_name
  nested_t_field_or_nil 'gd$name', 'gd$fullName'
end

#given_nameObject



119
120
121
# File 'lib/google_contacts_api/contact.rb', line 119

def given_name
  nested_field_name_only 'gd$name', 'gd$givenName'
end

#given_name_yomiObject



122
123
124
# File 'lib/google_contacts_api/contact.rb', line 122

def given_name_yomi
  nested_field_yomi_only 'gd$name', 'gd$givenName'
end

#group_membership_infoObject



190
191
192
193
194
195
196
# File 'lib/google_contacts_api/contact.rb', line 190

def group_membership_info
  if self['gContact$groupMembershipInfo']
    self['gContact$groupMembershipInfo'].map(&method(:format_group_membership))
  else
    []
  end
end

#group_membershipsObject



200
201
202
# File 'lib/google_contacts_api/contact.rb', line 200

def group_memberships
  group_membership_info.select { |info| !info[:deleted] }.map { |info| info[:href] }
end

#imsObject

Returns all instant messaging addresses for the contact. Doesn’t yet distinguish protocols



93
94
95
# File 'lib/google_contacts_api/contact.rb', line 93

def ims
  self["gd$im"] ? self["gd$im"].map { |i| i.address } : []
end

Returns the array of links, as link is an array for Hashie.



9
10
11
# File 'lib/google_contacts_api/contact.rb', line 9

def links
  self["link"].map { |l| l.href }
end

#name_prefixObject



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

def name_prefix
  nested_t_field_or_nil 'gd$name', 'gd$namePrefix'
end

#name_suffixObject



143
144
145
# File 'lib/google_contacts_api/contact.rb', line 143

def name_suffix
  nested_t_field_or_nil 'gd$name', 'gd$nameSuffix'
end

#nested_t_field_or_nil(level1, level2) ⇒ Object

Convenience method to return a nested $t field. If the field doesn’t exist, return nil



114
115
116
117
118
# File 'lib/google_contacts_api/contact.rb', line 114

def nested_t_field_or_nil(level1, level2)
  if self[level1]
    self[level1][level2] ? self[level1][level2]['$t']: nil
  end
end

#organizationsObject



167
168
169
170
171
172
173
174
175
# File 'lib/google_contacts_api/contact.rb', line 167

def organizations
  format_entities('gd$organization').map do |org|
    if org[:org_name]
      org[:org_name_yomi] = org[:org_name]['yomi'] if org[:org_name]['yomi']
      org[:org_name] = name_only(org[:org_name])
    end
    org
  end
end

#phone_numbersObject

Returns all phone numbers for the contact



72
73
74
# File 'lib/google_contacts_api/contact.rb', line 72

def phone_numbers
  self["gd$phoneNumber"] ? self["gd$phoneNumber"].map { |e| e['$t'] } : []
end

#phone_numbers_fullObject

Return an Array of Hashes representing phone numbers with formatted metadata.



181
182
183
# File 'lib/google_contacts_api/contact.rb', line 181

def phone_numbers_full
  format_entities('gd$phoneNumber', :format_phone_number)
end

#photoObject

Returns binary data for the photo. You can probably use it in a data-uri. This is in PNG format.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/google_contacts_api/contact.rb', line 43

def photo
  return nil unless @api && photo_link
  response = @api.oauth.get(photo_link)

  case GoogleContactsApi::Api.parse_response_code(response)
  # maybe return a placeholder instead of nil
  when 400; return nil
  when 401; return nil
  when 403; return nil
  when 404; return nil
  when 400...500; return nil
  when 500...600; return nil
  else; return response.body
  end
end

Returns link for photo (still need authentication to get the photo data, though)



36
37
38
39
# File 'lib/google_contacts_api/contact.rb', line 36

def photo_link
  _link = photo_link_entry
  _link ? _link.href : nil
end

#photo_link_entryObject

Returns link entry for the photo



30
31
32
# File 'lib/google_contacts_api/contact.rb', line 30

def photo_link_entry
  self["link"].find { |l| l.rel == "http://schemas.google.com/contacts/2008/rel#photo" }
end

#photo_with_metadataObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/google_contacts_api/contact.rb', line 97

def 
  # etag is always specified if actual photo is present
  _link = photo_link_entry
  return nil unless @api && _link['gd$etag']

  response = @api.oauth.get(_link.href)
  if GoogleContactsApi::Api.parse_response_code(response) == 200
    {
        etag: _link['gd$etag'].gsub('"',''),
        content_type: response.headers['content-type'],
        data: response.body
    }
  end
end

#primary_emailObject

Returns primary email for the contact



82
83
84
85
86
87
88
89
# File 'lib/google_contacts_api/contact.rb', line 82

def primary_email
  if self["gd$email"]
     = self["gd$email"].find { |e| e.primary == "true" }
     ? .address : nil
  else
    nil # no emails at all
  end
end

#relationsObject



153
154
155
# File 'lib/google_contacts_api/contact.rb', line 153

def relations
  self['gContact$relation'] ? self['gContact$relation'] : []
end

Returns link to get this contact



14
15
16
17
# File 'lib/google_contacts_api/contact.rb', line 14

def self_link
  _link = self["link"].find { |l| l.rel == "self" }
  _link ? _link.href : nil
end

#spouseObject

Returns the spouse of the contact. (Assumes there’s only one.)



158
159
160
161
# File 'lib/google_contacts_api/contact.rb', line 158

def spouse
  spouse_rel = relations.find {|r| r.rel = 'spouse'}
  spouse_rel['$t'] if spouse_rel
end

#websitesObject



176
177
178
# File 'lib/google_contacts_api/contact.rb', line 176

def websites
  format_entities('gContact$website')
end