Class: GmailContacts
- Inherits:
-
Object
- Object
- GmailContacts
- Defined in:
- lib/gmail_contacts.rb,
lib/gmail_contacts/test_stub.rb
Overview
:stopdoc:
Defined Under Namespace
Constant Summary collapse
- VERSION =
'1.7'
Instance Attribute Summary collapse
-
#author_email ⇒ Object
readonly
Contact list author’s email.
-
#author_name ⇒ Object
readonly
Contact list author’s name.
-
#contact_api ⇒ Object
GData::Client::Contacts object accessor for testing.
-
#contacts ⇒ Object
readonly
Contact data.
-
#id ⇒ Object
readonly
Contacts list identifier.
-
#title ⇒ Object
readonly
Contacts list title.
Class Method Summary collapse
Instance Method Summary collapse
-
#fetch(*args) ⇒ Object
Fetches contacts from google for
email
. -
#fetch_photo(contact) ⇒ Object
Fetches the photo data for
contact
which may be a photo URL. -
#get_token ⇒ Object
Fetches an AuthSub session token.
-
#initialize(authsub_token = nil, session_token = false) ⇒ GmailContacts
constructor
Creates a new GmailContacts using
authsub_token
. -
#parse(xml) ⇒ Object
Extracts contact information from
xml
, appending it to the current contact information. -
#revoke_token ⇒ Object
Revokes our AuthSub token.
-
#token? ⇒ Boolean
Do we have an AuthSub session token?.
Constructor Details
#initialize(authsub_token = nil, session_token = false) ⇒ GmailContacts
Creates a new GmailContacts using authsub_token
. If you don’t yet have an AuthSub token, call contact_api.auth_url
providing your return endpoint.
See GData::Client::Base in the gdata gem and code.google.com/apis/accounts/docs/AuthSub.html for more details.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gmail_contacts.rb', line 72 def initialize(authsub_token = nil, session_token = false) @authsub_token = authsub_token @session_token = session_token @id = nil @title = nil @author_email = nil @author_name = nil @contacts ||= [] @contact_api = GData::Client::Contacts.new @contact_api.authsub_token = @authsub_token if @authsub_token end |
Instance Attribute Details
#author_email ⇒ Object (readonly)
Contact list author’s email
35 36 37 |
# File 'lib/gmail_contacts.rb', line 35 def @author_email end |
#author_name ⇒ Object (readonly)
Contact list author’s name
40 41 42 |
# File 'lib/gmail_contacts.rb', line 40 def @author_name end |
#contact_api ⇒ Object
GData::Client::Contacts object accessor for testing
45 46 47 |
# File 'lib/gmail_contacts.rb', line 45 def contact_api @contact_api end |
#contacts ⇒ Object (readonly)
Contact data
An Array with contact title, primary email and alternate emails
52 53 54 |
# File 'lib/gmail_contacts.rb', line 52 def contacts @contacts end |
#id ⇒ Object (readonly)
Contacts list identifier
57 58 59 |
# File 'lib/gmail_contacts.rb', line 57 def id @id end |
#title ⇒ Object (readonly)
Contacts list title
62 63 64 |
# File 'lib/gmail_contacts.rb', line 62 def title @title end |
Class Method Details
.stub ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 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 |
# File 'lib/gmail_contacts/test_stub.rb', line 141 def self.stub return if @stubbed @stubbed = true alias old_get_token get_token def get_token if @authsub_token == 'recycled_authsub_token' then res = GData::HTTP::Response.new res.status_code = 403 res.body = 'recycled token' raise GData::Client::AuthorizationError, res end old_get_token @contact_api.stub_reset @contact_api.stub_data << GmailContacts::TestStub::CONTACTS @contact_api.stub_data << GmailContacts::TestStub::CONTACTS2 end alias old_fetch fetch def fetch(*args) if @authsub_token == 'wrong_user_authsub_token' then res = GData::HTTP::Response.new res.status_code = 403 res.body = 'wrong user' raise GData::Client::AuthorizationError, res end old_fetch(*args) end alias old_fetch_photo fetch_photo def fetch_photo(contact) photo_url = if String === contact then contact else contact.photo_url end if photo_url =~ /404/ then res = Object.new def res.status_code() 404 end def res.body() 'Photo not found' end raise GData::Client::UnknownError, res end old_fetch_photo contact end end |
Instance Method Details
#fetch(*args) ⇒ Object
Fetches contacts from google for email
.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/gmail_contacts.rb', line 89 def fetch(email, revoke = true) get_token uri = "http://www.google.com/m8/feeds/contacts/#{email}/full" loop do res = @contact_api.get uri xml = Nokogiri::XML res.body parse xml next_uri = xml.xpath('//xmlns:feed/xmlns:link[@rel="next"]').first break unless next_uri uri = next_uri['href'] end yield if block_given? ensure revoke_token if revoke and token? end |
#fetch_photo(contact) ⇒ Object
Fetches the photo data for contact
which may be a photo URL
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/gmail_contacts.rb', line 115 def fetch_photo(contact) photo_url = if String === contact then contact else contact.photo_url end res = @contact_api.get photo_url res.body end |
#get_token ⇒ Object
Fetches an AuthSub session token
129 130 131 132 133 |
# File 'lib/gmail_contacts.rb', line 129 def get_token return if @session_token @contact_api.auth_handler.upgrade @session_token = true end |
#parse(xml) ⇒ Object
Extracts contact information from xml
, appending it to the current contact information
139 140 141 142 143 144 145 146 147 148 149 150 151 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 |
# File 'lib/gmail_contacts.rb', line 139 def parse(xml) @id = xml.xpath('//xmlns:feed/xmlns:id').first.text @title = xml.xpath('//xmlns:feed/xmlns:title').first.text @author_email = xml.xpath('//xmlns:feed/xmlns:author/xmlns:email').first.text @author_name = xml.xpath('//xmlns:feed/xmlns:author/xmlns:name').first.text xml.xpath('//xmlns:feed/xmlns:entry').each do |entry| title = entry.xpath('.//xmlns:title').first.text emails = [] primary = entry.xpath('.//gd:email[@primary]') next unless primary.first emails << primary.first['address'] alternates = entry.xpath('.//gd:email[not(@primary)]') emails.push(*alternates.map { |e| e['address'] }) ims = [] entry.xpath('.//gd:im').each do |im| ims << [im['address'], im['protocol']] end phones = [] entry.xpath('.//gd:phoneNumber').each do |phone| phones << [phone.text, phone['rel']] end addresses = [] entry.xpath('.//gd:postalAddress').each do |address| addresses << [address.text, address['rel']] end photo_link = entry.xpath('.//xmlns:link[@rel="http://schemas.google.com/contacts/2008/rel#photo"]').first photo_url = photo_link['href'] if photo_link contact = Contact.new title, emails, ims, phones, addresses, photo_url @contacts << contact end self end |
#revoke_token ⇒ Object
Revokes our AuthSub token
186 187 188 |
# File 'lib/gmail_contacts.rb', line 186 def revoke_token @contact_api.auth_handler.revoke end |
#token? ⇒ Boolean
Do we have an AuthSub session token?
193 194 195 |
# File 'lib/gmail_contacts.rb', line 193 def token? @session_token end |