Sha256: d889cb4ffa6103d1165b68ad6f4eb0dfd75b576a101baa352799d8c3ea5b9481

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

require "omnicontacts/middleware/oauth2"
require "rexml/document"

module OmniContacts
  module Importer
    class Gmail < Middleware::OAuth2

      attr_reader :auth_host, :authorize_path, :auth_token_path, :scope

      def initialize *args
        super *args
        @auth_host = "accounts.google.com"
        @authorize_path = "/o/oauth2/auth"
        @auth_token_path = "/o/oauth2/token"
        @scope = "https://www.google.com/m8/feeds"
        @contacts_host = "www.google.com"
        @contacts_path = "/m8/feeds/contacts/default/full"
      end

      def fetch_contacts_using_access_token access_token, token_type
        contacts_response = https_get(@contacts_host, @contacts_path, contacts_req_params, contacts_req_headers(access_token, token_type))    
        parse_contacts contacts_response
      end

      private 

      def contacts_req_params
        { "max-results" => "100"}
      end

      def contacts_req_headers token, token_type
        {"GData-Version" => "3.0",  "Authorization" => "#{token_type} #{token}"}  
      end

      def parse_contacts contacts_as_xml
        xml = REXML::Document.new(contacts_as_xml)
        contacts = []
        xml.elements.each('//entry') do |entry|
          gd_email = entry.elements['gd:email']
          if gd_email
            contact = {:email => gd_email.attributes['address']}
            gd_name = entry.elements['gd:name']
            if gd_name
              contact[:name] = gd_name.elements['gd:fullName'].text
            end
            contacts << contact
          end
        end
        contacts 
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
omnicontacts-0.1.5 lib/omnicontacts/importer/gmail.rb
omnicontacts-0.1.4 lib/omnicontacts/importer/gmail.rb
omnicontacts-0.1.3 lib/omnicontacts/importer/gmail.rb
omnicontacts-0.1.2 lib/omnicontacts/importer/gmail.rb
omnicontacts-0.1.1 lib/omnicontacts/importer/gmail.rb
omnicontacts-0.1.0 lib/omnicontacts/importer/gmail.rb