Sha256: 6f77f2b1746380108360305e020332af83419b528638bad7af34bec995ac6556

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'gdata'

module Garlenko
  
  class Gmail < Base

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

    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

    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
garlenko-0.0.2 lib/garlenko/gmail.rb
garlenko-0.0.1 lib/garlenko/gmail.rb