Sha256: 53a3407d77f3d964430c625631297d3506b69790a96c0c2e623f58e281f0a6a4

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 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

1 entries across 1 versions & 1 rubygems

Version Path
omnicontacts-0.1.6 lib/omnicontacts/importer/gmail.rb