Class: GoogleToken

Inherits:
ConsumerToken
  • Object
show all
Defined in:
lib/muck_oauth/services/google_token.rb

Constant Summary collapse

GOOGLE_SETTINGS =
{
  :site=>"https://www.google.com", 
  :request_token_path => "/accounts/OAuthGetRequestToken",
  :authorize_path => "/accounts/OAuthAuthorizeToken",
  :access_token_path => "/accounts/OAuthGetAccessToken",
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.consumerObject


12
13
14
# File 'lib/muck_oauth/services/google_token.rb', line 12

def self.consumer
  @consumer||=create_consumer
end

.create_consumer(options = {}) ⇒ Object


16
17
18
# File 'lib/muck_oauth/services/google_token.rb', line 16

def self.create_consumer(options={})
  OAuth::Consumer.new credentials[:key], credentials[:secret], GOOGLE_SETTINGS.merge(options)
end

.get_request_token(callback_url, scope = nil) ⇒ Object


20
21
22
# File 'lib/muck_oauth/services/google_token.rb', line 20

def self.get_request_token(callback_url, scope=nil)
  consumer.get_request_token({:oauth_callback=>callback_url}, :scope=>scope||credentials[:scope]||"http://www-opensocial.googleusercontent.com/api/people")
end

Instance Method Details

#contacts(limit = 10000) ⇒ Object

Gets and parses contacts from google into objects. limit: Maximum number of contacts to retrieve


30
31
32
# File 'lib/muck_oauth/services/google_token.rb', line 30

def contacts(limit = 10000)
  convert_google_contacts_json_to_users(load_contacts(limit))
end

#convert_google_contacts_json_to_users(json) ⇒ Object

Converts json returned from google into a feed object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/muck_oauth/services/google_token.rb', line 40

def convert_google_contacts_json_to_users(json)
  if json['feed'] && json['feed']['entry']
    json['feed']['entry'].collect do |entry|
      emails = entry['gd$email'].collect { |gd| gd['address'] } if entry['gd$email']
      phones = entry['gd$phoneNumber'].collect { |gd| gd['t'] } if entry['gd$phoneNumber']
      if entry['gd$name']
        first_name = entry['gd$name']['gd$givenName'] if entry['gd$name']['gd$givenName']
        last_name = entry['gd$name']['gd$familyName'] if entry['gd$name']['gd$familyName']
      end
      OpenStruct.new( { :emails => emails,
                        :phones => phones,
                        :first_name => first_name,
                        :last_name => last_name } )
    end
  end
end

#get(path) ⇒ Object


62
63
64
65
66
67
68
69
# File 'lib/muck_oauth/services/google_token.rb', line 62

def get(path)
  response = self.client.get(path + "&alt=json&v=3.0")
  if response.code == '200'
    JSON.parse(response.body)
  else
    raise MuckOauth::Exceptions::HTTPResultError, I18n.t('muck.oauth.http_result_error', :error => response.to_s)
  end
end

#load_contacts(limit = 10000) ⇒ Object

Loads contacts using Google api and OAuth token.


35
36
37
# File 'lib/muck_oauth/services/google_token.rb', line 35

def load_contacts(limit = 10000)
  get("http://www.google.com/m8/feeds/contacts/default/full?max-results=#{limit}")
end

#load_groups(limit = 10000) ⇒ Object

Loads a user's groups using Google api and OAuth token.


58
59
60
# File 'lib/muck_oauth/services/google_token.rb', line 58

def load_groups(limit = 10000)
  get("http://www.google.com/m8/feeds/groups/default/full?max-results=#{limit}")
end

#portable_contactsObject


24
25
26
# File 'lib/muck_oauth/services/google_token.rb', line 24

def portable_contacts
  @portable_contacts||= PortableContacts::Client.new "http://www-opensocial.googleusercontent.com/api/people", client
end