class Glass::Mirror

Attributes

client_id[RW]
client_secret[RW]
redirect_uri[RW]
scopes[RW]
client[RW]

Public Class Methods

build_client(credentials) click to toggle source

Build a Mirror client instance.

@param [Signet::OAuth2::Client] credentials

OAuth 2.0 credentials.

@return [Google::APIClient]

Client instance
# File lib/glass.rb, line 226
def self.build_client(credentials)
  m = Mirror.new()
  m.client = Google::APIClient.new
  m.client.authorization = credentials
  m.client = m.client.discovered_api('mirror', 'v1')
  m
end
build_with_code(authorization_code) click to toggle source
# File lib/glass.rb, line 234
def self.build_with_code(authorization_code)
  return build_client(get_credentials(authorization_code))
end
exchange_code(authorization_code) click to toggle source

Exchange an authorization code for OAuth 2.0 credentials.

@param [String] auth_code

Authorization code to exchange for OAuth 2.0 credentials.

@return [Signet::OAuth2::Client]

OAuth 2.0 credentials.
# File lib/glass.rb, line 105
def self.exchange_code(authorization_code)
  client = Google::APIClient.new
  client.authorization.client_id = client_id
  client.authorization.client_secret = client_secret
  client.authorization.code = authorization_code
  client.authorization.redirect_uri = redirect_uri

  begin
    client.authorization.fetch_access_token!
    return client.authorization
  rescue Signet::AuthorizationError
    raise CodeExchangeError.new(nil)
  end
end
get_authorization_url(user_id, state) click to toggle source

Retrieve authorization URL.

@param [String] user_id

User's Google ID.

@param [String] state

State for the authorization URL.

@return [String]

Authorization URL to redirect the user to.
# File lib/glass.rb, line 155
def self.get_authorization_url(user_id, state)
  client = Google::APIClient.new
  client.authorization.client_id = client_id
  client.authorization.redirect_uri = redirect_uri
  client.authorization.scope = scopes

  return client.authorization.authorization_uri(
      :options => {
          :approval_prompt => :force,
          :access_type => :offline,
          :user_id => user_id,
          :state => state
      }).to_s
end
get_credentials(authorization_code, state="OAuth Failed") click to toggle source

Retrieve credentials using the provided authorization code.

This function exchanges the authorization code for an access token and queries
the UserInfo API to retrieve the user's Google ID.
If a refresh token has been retrieved along with an access token, it is stored
in the application database using the user's Google ID as key.
If no refresh token has been retrieved, the function checks in the application
database for one and returns it if found or raises a NoRefreshTokenError
with an authorization URL to redirect the user to.

@param [String] auth_code

Authorization code to use to retrieve an access token.

@param [String] state

State to set to the authorization URL in case of error.

@return [Signet::OAuth2::Client]

OAuth 2.0 credentials containing an access and refresh token.
# File lib/glass.rb, line 188
def self.get_credentials(authorization_code, state="OAuth Failed")
  user_id = ''
  begin
    credentials = exchange_code(authorization_code)
    user_info = get_user_info(credentials)
    user_id = user_info.id
    if credentials.refresh_token != nil
      store_credentials(user_id, credentials)
      return credentials
    else
      credentials = get_stored_credentials(user_id)
      if credentials != nil && credentials.refresh_token != nil
        return credentials
      end
    end
  rescue CodeExchangeError => error
    print 'An error occurred during code exchange.'
    # Drive apps should try to retrieve the user and credentials for the current
    # session.
    # If none is available, redirect the user to the authorization URL.
    error.authorization_url = get_authorization_url(user_id, state)
    raise error
  rescue NoUserIdError
    print 'No user ID could be retrieved.'
  end

  authorization_url = get_authorization_url(user_id, state)
  raise NoRefreshTokenError.new(authorization_url)
end
get_stored_credentials(user_id) click to toggle source

Retrieved stored credentials for the provided user ID.

@param [String] user_id

User's ID.

@return [Signet::OAuth2::Client]

Stored OAuth 2.0 credentials if found, nil otherwise.
# File lib/glass.rb, line 68
def self.get_stored_credentials(user_id)
  unless @@config.no_redis
    hash = Redis.get(user_id)
    client = Google::APIClient.new
    client.authorization.dup
    client.update_token!(hash)
  end
end
get_user_info(credentials) click to toggle source

Send a request to the UserInfo API to retrieve the user’s information.

@param [Signet::OAuth2::Client] credentials

OAuth 2.0 credentials to authorize the request.

@return [Google::APIClient::Schema::Oauth2::V2::Userinfo]

User's information.
# File lib/glass.rb, line 128
def self.get_user_info(credentials)
  client = Google::APIClient.new
  client.authorization = credentials
  oauth2 = client.discovered_api('oauth2', 'v2')
  result = client.execute!(:api_method => oauth2.userinfo.get)
  user_info = nil
  if result.status == 200
    user_info = result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
  if user_info != nil && user_info.id != nil
    return user_info
  end
  raise NoUserIdError, "Unable to retrieve the user's Google ID."
end
hello_world() click to toggle source
# File lib/glass.rb, line 38
def self.hello_world
  "Hello World!"
end
no_redis=(val) click to toggle source
# File lib/glass.rb, line 56
def self.no_redis= val
  @@config.no_redis=val
end
redis() click to toggle source

Returns the current Redis connection. If none has been created, will create a new one.

# File lib/glass.rb, line 44
def self.redis
  @@config.redis
end
redis=(val) click to toggle source
# File lib/glass.rb, line 48
def self.redis= val
  @@config.redis = val
end
redis_id() click to toggle source
# File lib/glass.rb, line 52
def self.redis_id
  @@config.redis_id
end
store_credentials(user_id, credentials) click to toggle source

Store OAuth 2.0 credentials in the application’s database.

@param [String] user_id

User's ID.

@param [Signet::OAuth2::Client] credentials

OAuth 2.0 credentials to store.
# File lib/glass.rb, line 85
def self.store_credentials(user_id, credentials)
  unless @@config.no_redis
    hash = Hash.new()
    hash[:access_token] = credentials.access_token
    hash[:refresh_token] = credentials.refresh_token
    hash[:expires_in] = credentials.expires_in
    hash[:issued_at] = credentials.issued_at

    Redis.set(user_id, hash)
  end
end

Public Instance Methods

delete(item) click to toggle source
# File lib/glass.rb, line 242
def delete(item)
  item.delete(client)
end
insert(item) click to toggle source
# File lib/glass.rb, line 238
def insert(item)
  item.insert(client)
end