class Gecko::Client

The Gecko::Client class

Attributes

access_token[RW]

Access the OAuth AccessToken for this client

@return [OAuth2::AccessToken]

@api public

oauth_client[R]

Return OAuth client

@return [OAuth2::Client]

@api private

Public Class Methods

default_headers() click to toggle source
# File lib/gecko/client.rb, line 116
def self.default_headers
  {
    'User-Agent' => ["Gecko/#{Gecko::VERSION}",
                     "OAuth2/#{OAuth2::Version}",
                     "Faraday/#{Faraday::VERSION}",
                     "Ruby/#{RUBY_VERSION}"].join(' ')
  }
end
new(client_id, client_secret, options = {}) click to toggle source

Initialize a new Gecko client object with an instantiated OAuth2::Client

@param [String] client_id @param [String] client_secret @param [#to_hash] extra options hash to pass to the OAuth2::Client

@return [undefined]

@api private

# File lib/gecko/client.rb, line 63
def initialize(client_id, client_secret, options = {})
  setup_oauth_client(client_id, client_secret, options)
end

Public Instance Methods

adapter_for(klass_name) click to toggle source

Fetch the adapter for a specified Gecko::Record class

@param [String] klass_name Gecko::Record subclass name

@return [Gecko::Record::BaseAdapter]

@api private

# File lib/gecko/client.rb, line 112
def adapter_for(klass_name)
  public_send(klass_name.to_sym)
end
authorize_from_existing(access_token, refresh_token, expires_at) click to toggle source

Authorize client from existing access and refresh token

@example

client.authorize_from_existing("ABC", "DEF", "1393982563")

@param [String] access_token @param [String] refresh_token @param [String] expires_at

@return [OAuth2::AccessToken]

@api public

# File lib/gecko/client.rb, line 79
def authorize_from_existing(access_token, refresh_token, expires_at)
  @access_token = OAuth2::AccessToken.new(oauth_client, access_token, {
    refresh_token: refresh_token,
    expires_at:    expires_at
  })
end
authorize_from_refresh_token(refresh_token) click to toggle source

Authorize client by fetching a new access_token via refresh token

@example

client.authorize_from_refresh_token("DEF")

@param [String] refresh_token

@return [OAuth2::AccessToken]

@api public

# File lib/gecko/client.rb, line 96
def authorize_from_refresh_token(refresh_token)
  @access_token = oauth_client.get_token({
    client_id:     oauth_client.id,
    client_secret: oauth_client.secret,
    refresh_token: refresh_token,
    grant_type:    'refresh_token'
  })
end

Private Instance Methods

setup_oauth_client(client_id, client_secret, options) click to toggle source
# File lib/gecko/client.rb, line 127
def setup_oauth_client(client_id, client_secret, options)
  defaults = {
    site:            'https://api.tradegecko.com',
    authorize_url:   'https://commerce.intuit.com/oauth/authorize',
    connection_opts: {
      headers: self.class.default_headers
    }
  }
  @oauth_client = OAuth2::Client.new(client_id, client_secret, defaults.merge(options))
end