Class: MaiCoin::OAuthClient

Inherits:
Client
  • Object
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

Parameters:

  • client_id (String)

    this application’s MaiCoin OAuth2 CLIENT_ID

  • client_secret (String)

    this application’s MaiCoin 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/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

#credentialsObject



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_tokenObject



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