Class: Contacts::Gmail

Inherits:
Base
  • Object
show all
Defined in:
lib/contacts/gmail.rb

Constant Summary collapse

CONTACTS_SCOPE =
'https://www.google.com/m8/feeds/'
CONTACTS_FEED =
CONTACTS_SCOPE + 'contacts/default/full/?max-results=1000'

Instance Method Summary collapse

Methods inherited from Base

#connect, #connected?, #initialize, #login, #password, #skip_gzip?

Constructor Details

This class inherits a constructor from Contacts::Base

Instance Method Details

#contactsObject



9
10
11
# File 'lib/contacts/gmail.rb', line 9

def contacts
  return @contacts if @contacts
end

#real_connectObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/contacts/gmail.rb', line 13

def real_connect
  gclient = GData::Client::Contacts.new

  if (@oauth_info)
    gclient.oauth_info = @oauth_info
  else
    gclient.clientlogin(@login, @password, @captcha_token, @captcha_response)
  end

  begin
    response = gclient.get(CONTACTS_FEED)
  rescue GData::Client::AuthorizationError
    # if oauth fails, try username & password
    if @oauth_info && @login && @password
      gclient.clientlogin(@login, @password, @captcha_token, @captcha_response)
      response = gclient.get(CONTACTS_FEED)
    else
      raise
    end
  end

  feed = response.to_xml

  @contacts = feed.elements.to_a('entry').collect do |entry|
    title, email = entry.elements['title'].text, nil
    entry.elements.each('gd:email') do |e|
      email = e.attribute('address').value if e.attribute('primary')
    end

    avatar_el = entry.elements['link[@type="image/*"]']
    avatar_url = avatar_el ? avatar_el.attribute('href').to_s : nil

    [title, email, avatar_url] unless email.nil?
  end
  @contacts.compact!
rescue GData::Client::AuthorizationError => e
  raise AuthenticationError, "Username or password are incorrect"
end