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?, #etag, #id, #initialize, #inspect, #title, #updated

Constructor Details

This class inherits a constructor from GoogleContactsApi::Result

Instance Method Details

#additional_nameObject



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

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

#addressesObject

Return an Array of Hashes representing addresses with formatted metadata.



151
152
153
# File 'lib/google_contacts_api/contact.rb', line 151

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



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

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

Returns link to edit the contact



62
63
64
65
# File 'lib/google_contacts_api/contact.rb', line 62

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

Returns link to add/replace the photo



56
57
58
59
# File 'lib/google_contacts_api/contact.rb', line 56

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



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

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.



167
168
169
# File 'lib/google_contacts_api/contact.rb', line 167

def emails_full
  format_entities('gd$email')
end

#family_nameObject



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

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

#full_nameObject



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

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

#given_nameObject



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

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

#imsObject

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



89
90
91
# File 'lib/google_contacts_api/contact.rb', line 89

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



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

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

#name_suffixObject



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

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



110
111
112
113
114
# File 'lib/google_contacts_api/contact.rb', line 110

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

#organizationsObject



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

def organizations
  format_entities('gd$organization')
end

#phone_numbersObject

Returns all phone numbers for the contact



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

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.



162
163
164
# File 'lib/google_contacts_api/contact.rb', line 162

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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/google_contacts_api/contact.rb', line 39

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)



32
33
34
35
# File 'lib/google_contacts_api/contact.rb', line 32

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

#photo_link_entryObject

Returns link entry for the photo



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

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

#photo_with_metadataObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/google_contacts_api/contact.rb', line 93

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



78
79
80
81
82
83
84
85
# File 'lib/google_contacts_api/contact.rb', line 78

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



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

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.)



145
146
147
148
# File 'lib/google_contacts_api/contact.rb', line 145

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

#websitesObject



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

def websites
  format_entities('gContact$website')
end