Class: BetterCoinbase::OAuthClient

Inherits:
Client
  • Object
show all
Defined in:
lib/better-coinbase/oauth_client.rb

Constant Summary collapse

AUTHORIZE_URL =
'https://coinbase.com/oauth/authorize'
TOKEN_URL =
'https://coinbase.com/oauth/token'

Constants inherited from Client

Client::BASE_URI

Instance Method Summary collapse

Methods inherited from Client

#accounts, #addresses, #balance, build_whitelisted_cert_store, #buy!, #buy_price, #cancel_request, #complete_request, #create_button, #create_order_for_button, #create_user, #currencies_long, #currencies_short, #delete, #exchange_rates, #generate_receive_address, #get, #post, #put, #receive_address, #request_money, #resend_request, #sell!, #sell_price, #send_money, #spot_price, #ssl_options, #transaction, #transactions, #transfers, whitelisted_cert_store

Constructor Details

#initialize(client_id, client_secret, user_credentials, options = {}) ⇒ OAuthClient

Initializes a Coinbase Client using OAuth 2.0 credentials

Please note access tokens will be automatically refreshed when expired Use the credentials method when finished with the client to retrieve up-to-date credentials

Parameters:

  • client_id (String)

    this application's Coinbase OAuth2 CLIENT_ID

  • client_secret (String)

    this application's Coinbase OAuth2 CLIENT_SECRET

  • user_credentials (Hash)

    OAuth 2.0 credentials to use

Options Hash (user_credentials):

  • access_token (String)

    Must pass either this or token

  • token (String)

    Must pass either this or access_token

  • refresh_token (String)

    Optional

  • expires_at (Integer)

    Optional

  • expires_in (Integer)

    Optional



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/better-coinbase/oauth_client.rb', line 22

def initialize(client_id, client_secret, user_credentials, options={})
  client_opts = {
    :site          => options[:base_uri] || BASE_URI,
    :authorize_url => options[:authorize_url] || AUTHORIZE_URL,
    :token_url     => options[:token_url] || TOKEN_URL,
    :ssl           => {
                        :verify => true,
                        :cert_store => ::BetterCoinbase::Client.whitelisted_cert_store
                      }
  }
  @oauth_client = OAuth2::Client.new(client_id, client_secret, client_opts)
  token_hash = user_credentials.dup
  token_hash[:access_token] ||= token_hash[:token]
  token_hash.delete :expires
  raise "No access token provided" unless token_hash[:access_token]
  @oauth_token = OAuth2::AccessToken.from_hash(@oauth_client, token_hash)
end

Instance Method Details

#credentialsObject



67
68
69
# File 'lib/better-coinbase/oauth_client.rb', line 67

def credentials
  @oauth_token.to_hash
end

#http_verb(verb, path, options = {}) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/better-coinbase/oauth_client.rb', line 40

def http_verb(verb, path, options={})
  path = remove_leading_slash(path)

  if [:get, :delete].include? verb
    request_options = {params: options}
  else
    request_options = {headers: {"Content-Type" => "application/json"}, body: options.to_json}
  end
  response = oauth_token.request(verb, path, request_options)

  hash = Hashie::Mash.new(JSON.parse(response.body))
  raise Error.new(hash.error) if hash.error
  raise Error.new(hash.errors.join(", ")) if hash.errors
  hash
end

#oauth_tokenObject



61
62
63
64
65
# File 'lib/better-coinbase/oauth_client.rb', line 61

def oauth_token
  raise "Access token not initialized." unless @oauth_token
  refresh! if @oauth_token.expired?
  @oauth_token
end

#refresh!Object



56
57
58
59
# File 'lib/better-coinbase/oauth_client.rb', line 56

def refresh!
  raise "Access token not initialized." unless @oauth_token
  @oauth_token = @oauth_token.refresh!
end