Class: CBX
- Inherits:
-
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
Overview
A CBX object exposes all of the functionality of the API through methods that return JSON objects.
Defined Under Namespace
Modules: MarketData, Pagination, Trading
Classes: Feed
Constant Summary
collapse
- API_URL =
'https://api.exchange.coinbase.com/'
- VERSION =
'0.1.2'
Instance Method Summary
collapse
-
#authenticated? ⇒ Boolean
-
#delete(uri, json = nil, &block) ⇒ Object
-
#get(uri, json = nil, &block) ⇒ Object
-
#initialize(key = nil, secret = nil, passphrase = nil) ⇒ CBX
constructor
-
#post(uri, json = nil, &block) ⇒ Object
-
#request(method, uri, json = nil) {|r.body| ... } ⇒ Object
-
#sign(request_url = '', body = '', timestamp = nil, method = 'GET') ⇒ Object
Methods included from MarketData
#candles, #currencies, #orderbook, #products, #stats, #ticker, #time, #trades
Constructor Details
#initialize(key = nil, secret = nil, passphrase = nil) ⇒ CBX
Returns a new instance of CBX.
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/cbx.rb', line 20
def initialize(key = nil, secret = nil, passphrase = nil)
if key && secret && passphrase
@key = key
@secret = secret
@passphrase = passphrase
@authenticated = true
extend Trading
else
@authenticated = false
end
end
|
Instance Method Details
#authenticated? ⇒ Boolean
32
33
34
|
# File 'lib/cbx.rb', line 32
def authenticated?
@authenticated
end
|
#delete(uri, json = nil, &block) ⇒ Object
80
81
82
|
# File 'lib/cbx.rb', line 80
def delete(uri, json = nil, &block)
request(:delete, uri, json, &block)
end
|
#get(uri, json = nil, &block) ⇒ Object
72
73
74
|
# File 'lib/cbx.rb', line 72
def get(uri, json = nil, &block)
request(:get, uri, json, &block)
end
|
#post(uri, json = nil, &block) ⇒ Object
76
77
78
|
# File 'lib/cbx.rb', line 76
def post(uri, json = nil, &block)
request(:post, uri, json, &block)
end
|
#request(method, uri, json = nil) {|r.body| ... } ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/cbx.rb', line 47
def request(method, uri, json = nil)
params = json.to_json if json
if authenticated?
= {
'CB-ACCESS-SIGN' => sign('/' + uri, params, nil, method),
'CB-ACCESS-TIMESTAMP' => Time.now.to_i,
'CB-ACCESS-KEY' => @key,
'CB-ACCESS-PASSPHRASE' => @passphrase,
'Content-Type' => 'application/json'
}
else
= { 'Content-Type' => 'application/json' }
end
if method == :get
r = Unirest.get(API_URL + uri, headers: )
elsif method == :post
r = Unirest.post(API_URL + uri, headers: , parameters: params)
elsif method == :delete
r = Unirest.delete(API_URL + uri, headers: , parameters: params)
end
yield r.body if block_given?
r.body
end
|
#sign(request_url = '', body = '', timestamp = nil, method = 'GET') ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/cbx.rb', line 36
def sign(request_url = '', body = '', timestamp = nil, method = 'GET')
body = body.to_json if body.is_a?(Hash)
timestamp = Time.now.to_i unless timestamp
what = "#{timestamp}#{method.upcase}#{request_url}#{body}"
secret = Base64.decode64(@secret)
hash = OpenSSL::HMAC.digest('sha256', secret, what)
Base64.strict_encode64(hash)
end
|