module GoogleContactsApi class Contact < GoogleContactsApi::Result # :categories, (:content again), :links, (:title again), :email # :extended_properties, :deleted, :im, :name, # :organizations, :phone_numbers, :structured_postal_addresses, :where # Returns the array of links, as link is an array for Hashie. def links self["link"].map { |l| l.href } end # Returns link to get this contact def self_link _link = self["link"].find { |l| l.rel == "self" } _link ? _link.href : nil end # Returns alternative, possibly off-Google home page link def alternate_link _link = self["link"].find { |l| l.rel == "alternate" } _link ? _link.href : nil end # Returns link for photo # (still need authentication to get the photo data, though) def photo_link _link = self["link"].find { |l| l.rel == "http://schemas.google.com/contacts/2008/rel#photo" } _link ? _link.href : nil end # Returns binary data for the photo. You can probably # use it in a data-uri. This is in PNG format. def photo return nil unless @api && photo_link response = @api.oauth.get(photo_link) case response.code # 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 to add/replace the photo 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 # Returns link to edit the contact def edit_link _link = self["link"].find { |l| l.rel == "edit" } _link ? _link.href : nil end # Returns all email addresses for the contact def emails self["gd$email"] ? self["gd$email"].map { |e| e.address } : [] end # Returns primary email for the contact def primary_email if self["gd$email"] _email = self["gd$email"].find { |e| e.primary == "true" } _email ? _email.address : nil else nil # no emails at all end end end end