Class: Garlenko::Gmail

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

Constant Summary

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

Instance Attribute Summary

Attributes inherited from Base

#password, #username

Instance Method Summary collapse

Methods inherited from Base

#connected?, #initialize

Constructor Details

This class inherits a constructor from Garlenko::Base

Instance Method Details

#connect!Object



36
37
38
39
40
41
42
# File 'lib/garlenko/gmail.rb', line 36

def connect!
  @client = GData::Client::Contacts.new
  @client.clientlogin(@username, @password, @captcha_token, @captcha_response)
  @connected = true
rescue GData::Client::AuthorizationError => e
  @connected = false
end

#contactsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/garlenko/gmail.rb', line 10

def contacts
  connect!
  feed = @client.get(CONTACTS_FEED).to_xml

  @contacts = feed.elements.to_a('entry').collect do |entry|
    title, email, photo = entry.elements['title'].text, nil, [nil, nil]

    begin
      entry.elements.each('link[@gd:etag]') do |e|
        gdata_response = @client.get(e.attribute('href').value)
        photo = [gdata_response.body, gdata_response.headers['content-type']] if gdata_response.status_code == 200 and !gdata_response.body.nil?
      end
    rescue StandardError
      photo = [nil, nil]
    end

    entry.elements.each('gd:email') do |e|
      email = e.attribute('address').value if e.attribute('primary')
    end
    { :name => title, :email => email, :photo => photo } unless email.nil?
  end
  @contacts.compact!
  @connected = true
  return @contacts if @contacts
end