Class: CBX

Inherits:
Object
  • Object
show all
Includes:
MarketData
Defined in:
lib/cbx.rb,
lib/cbx/feed.rb,
lib/cbx/trading.rb,
lib/cbx/version.rb,
lib/cbx/pagination.rb,
lib/cbx/market_data.rb

Defined Under Namespace

Modules: MarketData, Pagination, Trading Classes: Feed

Constant Summary collapse

API_URL =
'https://api.exchange.coinbase.com/'
VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MarketData

#orderbook, #products, #ticker, #trades

Constructor Details

#initialize(key = nil, secret = nil, passphrase = nil) ⇒ CBX

Returns a new instance of CBX.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cbx.rb', line 19

def initialize(key=nil, secret=nil, passphrase=nil)
  if key && secret && passphrase
    @key = key
    @secret = secret
    @passphrase = passphrase
    @coinbaseExchange = CBXSignatureMaker.new(key, secret, passphrase)
    @authenticated = true
    extend Trading
  else 
    @authenticated = false
  end
end

Instance Attribute Details

#authenticatedObject (readonly)

Returns the value of attribute authenticated.



15
16
17
# File 'lib/cbx.rb', line 15

def authenticated
  @authenticated
end

Instance Method Details

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



65
66
67
# File 'lib/cbx.rb', line 65

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

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



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

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

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



61
62
63
# File 'lib/cbx.rb', line 61

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

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

Yields:

  • (r.body)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cbx.rb', line 32

def request(method, uri, json=nil)
  params = json.to_json if json
  if authenticated
    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'
    }
  else
    headers = { 'Content-Type' => 'application/json' }
  end

  if method == :get
    r=Unirest.get(API_URL + uri, headers: headers)
  elsif method == :post
    r=Unirest.post(API_URL + uri, headers: headers, parameters: params)
  elsif method == :delete
    r=Unirest.delete(API_URL + uri, headers: headers, parameters: params)
  end
  yield r.body if block_given?
  return r.body
end