Class: MaiCoin::OAuthClient
- Inherits:
-
Client
- Object
- Client
- MaiCoin::OAuthClient
show all
- Defined in:
- lib/maicoin/oauth_client.rb
Constant Summary
collapse
- AUTHORIZE_URL =
'https://www.maicoin.com/oauth/authorize'
- TOKEN_URL =
'https://www.maicoin.com/oauth/token'
Constants inherited
from Client
Client::BASE_URI
Instance Method Summary
collapse
Methods inherited from Client
#addresses, #approve_requested_transaction, #balance, build_whitelisted_cert_store, #buy_btc, #buy_order, #cancel_request_btc, #cancel_request_transaction, #checkout, #checkouts, #create_account_pin, #create_checkout, #create_order, #currencies, #delete, #generate_receive_address, #get, #order, #orders, #post, #prices, #put, #receive_address, #request_btc, #request_transaction, #sell_btc, #send_transaction, #ssl_options, #transaction, #transactions, #update_account_pin, #user, whitelisted_cert_store
Constructor Details
#initialize(client_id, client_secret, user_credentials, options = {}) ⇒ OAuthClient
Initializes a MaiCoin 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/maicoin/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 => ::MaiCoin::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
68
69
70
|
# File 'lib/maicoin/oauth_client.rb', line 68
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
55
|
# File 'lib/maicoin/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 = nil
begin
response = oauth_token.request(verb, path, request_options)
rescue Exception => e
response = e.response
end
Hashie::Mash.new(JSON.parse(response.body))
end
|
#oauth_token ⇒ Object
62
63
64
65
66
|
# File 'lib/maicoin/oauth_client.rb', line 62
def oauth_token
raise "Access token not initialized." unless @oauth_token
refresh! if @oauth_token.expired?
@oauth_token
end
|
#refresh! ⇒ Object
57
58
59
60
|
# File 'lib/maicoin/oauth_client.rb', line 57
def refresh!
raise "Access token not initialized." unless @oauth_token
@oauth_token = @oauth_token.refresh!
end
|