Class: Oauth::Contacts::Google

Inherits:
OAuth2 show all
Defined in:
lib/oauth/contacts/google.rb

Constant Summary

Constants inherited from OAuth2

OAuth2::VERSION

Instance Attribute Summary

Attributes inherited from OAuth2

#code

Attributes inherited from Base

#access_token, #callback_url, #consumer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OAuth2

#version

Constructor Details

#initializeGoogle

Returns a new instance of Google.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/oauth/contacts/google.rb', line 9

def initialize
  super
  @callback_url = Settings.oauth.contacts.google.callback_url

  # GOOGLE
  opts = { key: Settings.oauth.contacts.google.key, secret: Settings.oauth.contacts.google.secret,
    extra: { site: 'https://accounts.google.com',
      authorize_url: '/o/oauth2/auth',
      token_url: '/o/oauth2/token',
      max_redirects: 5,
      raise_errors: true,
      token_method: :post,
      connection_opts: {} # [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday with
    } }

  @consumer = ::OAuth2::Client.new(opts[:key], opts[:secret], opts[:extra])
end

Class Method Details

.slim(contacts) ⇒ Object

:nodoc:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oauth/contacts/google.rb', line 49

def slim(contacts) #:nodoc:
  _contacts = { 'contacts' => [] }

  contacts['feed']['entry'].each do |contact|
    emails, nickname = [], nil
    nickname = contact['title']['$t']

    contact['gd$email'].each do |email|
      emails << email['address']
    end
    _contacts['contacts'] << { :email => emails.first, :emails => emails, :name => nickname }
  end
  _contacts
end

Instance Method Details

#authorize_urlObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/oauth/contacts/google.rb', line 27

def authorize_url
  scopes = Settings.oauth.contacts.google.scopes

  # http://code.google.com/intl/fr-FR/apis/contacts/docs/3.0/developers_guide.html
  extra_params = { scope: scopes.join(' '),
    redirect_uri: callback_url,
    response_type: 'code',
    access_type: 'offline',
    approval_prompt: 'force',
    state: 'google' }

  consumer.auth_code.authorize_url( extra_params )
end

#contactsObject



41
42
43
44
45
46
# File 'lib/oauth/contacts/google.rb', line 41

def contacts
  token!

  request = "https://www.google.com/m8/feeds/contacts/default/full?alt=json"
  @contacts = normalize(access_token.get(request, :parse => :json).parsed)
end