Class: Googol::GoogleAccount

Inherits:
Object
  • Object
show all
Includes:
Authenticable
Defined in:
lib/googol/google_account.rb

Overview

Provides read & write access to a Google (or Google+) account.

Examples:

Retrieve the email and given name of a Google account:

* Set up two pages: one with a link to authenticate, one to redirect to
* In the first page, add a link to the authentication page:

    Googol::GoogleAccount.oauth_url(redirect_url)

* The user authenticates and lands on the second page, with an extra +code+ query parameter
* Use the authorization code to initialize the GoogleAccount and retrieve information:

    account = Googol::GoogleAccount.new code: code, redirect_url: redirect_url
    account.email # => '[email protected]'
    account.given_name # => 'Example user'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Authenticable

#credentials, #initialize

Methods included from Requestable

#request!

Methods included from ClientTokens

#client_id, client_id=, #client_secret, client_secret=

Class Method Details

.attribute(name) ⇒ Object

Define a method to return each attribute of the profile separately.



51
52
53
# File 'lib/googol/google_account.rb', line 51

def self.attribute(name)
  define_method(name) { info[name] }
end

.oauth_scopesObject

Set the scopes to grant access to Google user profile and email



70
71
72
# File 'lib/googol/google_account.rb', line 70

def self.oauth_scopes
  %w(profile email)
end

Instance Method Details

#emailObject

Return the email attribute of the Google Account.



56
# File 'lib/googol/google_account.rb', line 56

attribute :email

#family_nameObject

Return the family_name attribute of the Google Account.



60
# File 'lib/googol/google_account.rb', line 60

attribute :family_name

#genderObject

Return the gender attribute of the Google Account.



63
# File 'lib/googol/google_account.rb', line 63

attribute :gender

#given_nameObject

Return the given_name attribute of the Google Account.



59
# File 'lib/googol/google_account.rb', line 59

attribute :given_name

#hdObject

Return the hd attribute of the Google Account.



65
# File 'lib/googol/google_account.rb', line 65

attribute :hd

#idObject

Return the id attribute of the Google Account.



55
# File 'lib/googol/google_account.rb', line 55

attribute :id

#infoHash

Return the profile info of a Google account in OpenID Connect format.

Returns:

  • (Hash)
    • :id [String] The ID of the authenticated account

    • :email [String] The account’s email address.

    • :verified_email [String] Boolean flag which is true if the email address is verified.

    • :name [String] The account’s full name.

    • :given_name [String] The account’s given (first) name.

    • :family_name [String] The account’s family (last) name.

    • :link [String] The URL of the account’s profile page.

    • :picture [String] The URL of the account’s profile picture.

    • :gender [String] The account’s gender

    • :locale [String] The account’s preferred locale.

    • :hd [String] The hosted domain name for the accounts’s Google Apps.

See Also:



37
38
39
40
41
42
43
# File 'lib/googol/google_account.rb', line 37

def info
  @info ||= request! method: :get,
    auth: credentials[:access_token],
    host: 'https://www.googleapis.com',
    path: '/oauth2/v2/userinfo',
    valid_if: -> response, body {response.code == '200'}
end

Return the link attribute of the Google Account.



61
# File 'lib/googol/google_account.rb', line 61

attribute :link

#localeObject

Return the locale attribute of the Google Account.



64
# File 'lib/googol/google_account.rb', line 64

attribute :locale

#nameObject

Return the name attribute of the Google Account.



58
# File 'lib/googol/google_account.rb', line 58

attribute :name

#pictureObject

Return the picture attribute of the Google Account.



62
# File 'lib/googol/google_account.rb', line 62

attribute :picture

#verified_emailObject

Return the verified_email attribute of the Google Account.



57
# File 'lib/googol/google_account.rb', line 57

attribute :verified_email