Class: CoinbaseExchange

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase_exchange.rb

Constant Summary collapse

API_URL =
'https://api.exchange.coinbase.com/'

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, passphrase) ⇒ CoinbaseExchange

Returns a new instance of CoinbaseExchange.



28
29
30
31
32
33
# File 'lib/coinbase_exchange.rb', line 28

def initialize(key, secret, passphrase)
  @key = key
  @secret = secret
  @passphrase = passphrase
  @coinbaseExchange = CoinbaseExchangeSignatureMaker.new(key, secret, passphrase)
end

Instance Method Details

#get(uri, json = nil, &block) ⇒ Object



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

def get(uri, json=nil, &block)
    request(:get, uri, json, &block)
end

#post(uri, json = nil, &block) ⇒ Object



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

def post(uri, json=nil, &block)
    request(:post, uri, json, &block)
end

#request(method, uri, json = nil) {|r.body| ... } ⇒ Object

Yields:

  • (r.body)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/coinbase_exchange.rb', line 35

def request(method, uri, json=nil)
	params = json.to_json if json
  headers = { 
        'CB-ACCESS-SIGN'=> @coinbaseExchange.signature('/'+uri, params, nil, method),
        'CB-ACCESS-TIMESTAMP'=> Time.now.to_i,
        'CB-ACCESS-KEY'=> @key,
        'CB-ACCESS-PASSPHRASE'=> @passphrase,
        'Content-Type' => 'application/json'
      }
  if method == :get
      r=Unirest.get(API_URL + uri, headers: headers)
  elsif method == :post
      r=Unirest.post(API_URL + uri, headers: headers, parameters: params)
  end
  yield r.body if block_given?
  return r.body
end