Class: Buttercoin::Client

Inherits:
Object
  • Object
show all
Includes:
AccountQueryMethods, OrderMethods, TransactionMethods, UnauthMethods, HTTParty
Defined in:
lib/buttercoin/client.rb,
lib/buttercoin/client/order_methods.rb,
lib/buttercoin/client/unauth_methods.rb,
lib/buttercoin/client/transaction_methods.rb,
lib/buttercoin/client/account_query_methods.rb

Defined Under Namespace

Modules: AccountQueryMethods, OrderMethods, TransactionMethods, UnauthMethods

Constant Summary collapse

CONFIG =
{
  :mode => "production"
}
PRODUCTION_URI =
'https://api.buttercoin.com/v1'
SANDBOX_URI =
'https://sandbox.buttercoin.com/v1'
CA_CERT =
'cert/ca-cert.crt'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TransactionMethods

#cancel_transaction, #create_deposit, #create_withdrawal, #get_transaction_by_id, #get_transaction_by_url, #get_transactions, #send_bitcoin

Methods included from OrderMethods

#cancel_order, #create_order, #get_order_by_id, #get_order_by_url, #get_orders

Methods included from AccountQueryMethods

#get_balances, #get_deposit_address, #get_key

Methods included from UnauthMethods

#get_order_book, #get_ticker, #get_trade_history

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/buttercoin/client.rb', line 30

def initialize(*args)
  options = args[0]
  unless options.is_a?(Hash)
    # deprecated, pass a hash of options instead
    options = {
      :public_key => args[0],
      :secret_key => args[1],
      :mode => args[2]
    }
  end

  self.public_key, self.secret_key = options.values_at(:public_key, :secret_key)
  self.mode = options[:mode] || CONFIG[:mode]
  self.class.base_uri (self.mode == 'production') ? PRODUCTION_URI : SANDBOX_URI
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



19
20
21
# File 'lib/buttercoin/client.rb', line 19

def mode
  @mode
end

#public_keyObject

Returns the value of attribute public_key.



19
20
21
# File 'lib/buttercoin/client.rb', line 19

def public_key
  @public_key
end

#secret_keyObject

Returns the value of attribute secret_key.



19
20
21
# File 'lib/buttercoin/client.rb', line 19

def secret_key
  @secret_key
end

Instance Method Details

#delete(path, timestamp = nil, options = {}, authenticate = true) ⇒ Object



57
58
59
# File 'lib/buttercoin/client.rb', line 57

def delete(path, timestamp=nil, options={}, authenticate=true)
  http_request :delete, path, timestamp, options, authenticate
end

#get(path, timestamp = nil, options = {}, authenticate = true) ⇒ Object

Wrappers for the main HTTP verbs



48
49
50
51
# File 'lib/buttercoin/client.rb', line 48

def get(path, timestamp=nil, options={}, authenticate=true)
  path = "#{path}?#{URI.encode_www_form(options)}" if !options.empty?
  http_request :get, path, timestamp, options, authenticate
end

#http_request(verb, path, timestamp = nil, options = {}, authenticate = true) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/buttercoin/client.rb', line 61

def http_request(verb, path, timestamp=nil, options={}, authenticate=true)
  request_options = {}
  if (authenticate)
    timestamp ||= ((Time.now.to_f * 1e3).round).to_i
    message = build_message(verb, path, timestamp, options)
    signature = get_signature(message)
    request_options = {body: options.to_json} if [:post].include? verb
    request_options[:headers] = get_headers(signature, timestamp)
  end
  set_cert()
  r = self.class.send(verb, path, request_options)
  process_response(r.code, r.body, r.headers)
end

#post(path, timestamp = nil, options = {}, authenticate = true) ⇒ Object



53
54
55
# File 'lib/buttercoin/client.rb', line 53

def post(path, timestamp=nil, options={}, authenticate=true)
  http_request :post, path, timestamp, options, authenticate
end