Class: Cwallet::Wallet::OAuthClient

Inherits:
NetHTTPClient show all
Defined in:
lib/cwallet/wallet/client.rb

Constant Summary

Constants inherited from APIClient

APIClient::CALLBACK_DIGEST

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from APIClient

#account, #accounts, #address, #address_transactions, #addresses, #auth_info, #buy, #buy_price, #callback_signing_public_key, callback_signing_public_key, #cancel_request, #checkout, #checkout_orders, #checkouts, #commit_buy, #commit_deposit, #commit_sell, #commit_withdrawal, #complete_request, #create_account, #create_address, #create_checkout, #create_checkout_order, #create_order, create_user, #currencies, #current_user, #delete, #delete_account, #deposit, #exchange_rates, #get, #historic_prices, #list_buy, #list_buys, #list_deposit, #list_deposits, #list_sell, #list_sells, #list_withdrawal, #list_withdrawals, #merchant, #notification, #notifications, #order, #orders, #payment_method, #payment_methods, #post, #primary_account, #put, #refund_order, #request, #resend_request, #sell, #sell_price, #send, #set_primary_account, #spot_price, #time, #transaction, #transactions, #transfer, #update_account, #update_current_user, #user, verify_callback, #verify_callback, #withdraw

Constructor Details

#initialize(options = {}) ⇒ OAuthClient

Returns a new instance of OAuthClient.



37
38
39
40
41
42
43
# File 'lib/cwallet/wallet/client.rb', line 37

def initialize(options={})
  raise unless options.has_key? :access_token
  @access_token = options[:access_token]
  @refresh_token = options[:refresh_token]
  @oauth_uri = URI.parse(options[:api_url] || BASE_API_URL)
  super(@oauth_uri, options)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



35
36
37
# File 'lib/cwallet/wallet/client.rb', line 35

def access_token
  @access_token
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



35
36
37
# File 'lib/cwallet/wallet/client.rb', line 35

def refresh_token
  @refresh_token
end

Instance Method Details

#auth_headers(method, path, body) ⇒ Object



45
46
47
48
# File 'lib/cwallet/wallet/client.rb', line 45

def auth_headers(method, path, body)
  { 'Authorization' => "Bearer #{@access_token}",
    'CB-VERSION' => API_VERSION }
end

#authorize!(redirect_url, params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/cwallet/wallet/client.rb', line 50

def authorize!(redirect_url, params = {})
  raise NotImplementedError
end

#refresh!(params = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cwallet/wallet/client.rb', line 65

def refresh!(params = {})
  params[:grant_type] = 'refresh_token'
  params[:refresh_token] ||= @refresh_token

  raise "Missing Parameter: refresh_token" unless params.has_key?(:refresh_token)

  out = nil
  post("/oauth/token", params) do |resp|
    out = APIObject.new(self, resp.body)
    # Update tokens to current instance
    # Developer should always persist them
    @access_token = out.access_token
    @refresh_token = out.refresh_token
    yield(out, resp) if block_given?
  end
  out
end

#revoke!(params = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/cwallet/wallet/client.rb', line 54

def revoke!(params = {})
  params[:token] ||= @access_token

  out = nil
  post("/oauth/revoke", params) do |resp|
    out = APIObject.new(self, resp.body)
    yield(out, resp) if block_given?
  end
  out
end