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



104
105
106
# File 'lib/google_contacts_api/contact.rb', line 104

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

#addressesObject

Return an Array of Hashes representing addresses with formatted metadata.



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

def addresses
  self['gd$structuredPostalAddress'] ? self['gd$structuredPostalAddress'].map(&method(: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

Returns link to edit the contact



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

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

Returns link to add/replace the photo



51
52
53
54
# File 'lib/google_contacts_api/contact.rb', line 51

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



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

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.



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

def emails_full
  self["gd$email"] ? self["gd$email"].map(&method(:format_email)) : []
end

#family_nameObject



98
99
100
# File 'lib/google_contacts_api/contact.rb', line 98

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

#full_nameObject



101
102
103
# File 'lib/google_contacts_api/contact.rb', line 101

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

#given_nameObject



95
96
97
# File 'lib/google_contacts_api/contact.rb', line 95

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



84
85
86
# File 'lib/google_contacts_api/contact.rb', line 84

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



107
108
109
# File 'lib/google_contacts_api/contact.rb', line 107

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

#name_suffixObject



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

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



90
91
92
93
94
# File 'lib/google_contacts_api/contact.rb', line 90

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

#phone_numbersObject

Returns all phone numbers for the contact



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

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.



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

def phone_numbers_full
  self["gd$phoneNumber"] ? self["gd$phoneNumber"].map(&method(: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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/google_contacts_api/contact.rb', line 34

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)



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

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

#primary_emailObject

Returns primary email for the contact



73
74
75
76
77
78
79
80
# File 'lib/google_contacts_api/contact.rb', line 73

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



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

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



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

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