Class: BetterCoinbase::OAuthClient
- Inherits:
-
Client
- Object
- Client
- BetterCoinbase::OAuthClient
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
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
#credentials ⇒ Object
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
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_token ⇒ Object
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
|