gdax v0.1.2
📈 GDAX
A Crystal library for interacting with GDAX's REST and WebSocket API's.
Visit the API Documentation for a more in-depth look at the library's functionality.
Installation
Add this to your application's shard.yml
:
dependencies:
gdax:
github: mccallofthewild/gdax
Usage
Begin by requiring "gdax"
.
require "gdax"
GDAX::Client
Interact with the GDAX REST API
GDAX::Client
is derived from HTTP::Client. Use the inherited instance methods, #get
, #post
, #put
and #delete
to interact with GDAX's API. Responses are instances of HTTP::Client::Response.
Basic
To authenticate, pass named argument, auth
to the GDAX::WebSocket
with a GDAX::Auth
instance. See Authentication for help getting your key
, secret
, and passphrase
.
NOTE: For security purposes, it is recommended that you store your key
, secret
, and passphrase
as environment variables.
e.g.
auth = GDAX::Auth.new ENV["CB-ACCESS-KEY"], ENV["API-SECRET"], ENV["PASSPHRASE"]
client = GDAX::Client.new auth
client.get "/products/BTC-USD/trades" do |response|
puts response.body_io.gets_to_end
end
Unauthenticated
To instantiate an unauthenticated Client
, simply don't pass the auth
argument.
e.g.
client = GDAX::Client.new
client.get "/products" do |response|
puts response.body_io.gets_to_end
end
See the API Documentation for more information on GDAX::WebSocket
.
GDAX::WebSocket
Interact with the GDAX WebSocket Feed
Basic
It's recommended that you spawn a Fiber around each GDAX::WebSocket
you instantiate in order to achieve concurrency.
The following setup will give you access to GDAX's public ticker stream.
spawn do
ws = GDAX::WebSocket.new production: true, subscription: {
"type" => "subscribe",
"channels" => [{ "name" => "ticker", "product_ids" => ["ETH-EUR"] }]
}
ws.run
end
Fiber.yield
GDAX::WebSocket#on
Use the #on
method to add event listeners to a GDAX::WebSocket
.
Events are based on GDAX's message type
's .
#on
takes in a String
of the event to listen for and a block to call when the event is fired. The block is passed two arguments: the first being the JSON::Any
response data from GDAX, and the second being the event itself.
ws.on "subscriptions" do |data, event|
puts "subscribed!"
end
Though event listeners can be added dynamically on runtime, to avoid missing events, it is recommended that all listeners be added prior to invoking GDAX::WebSocket#run
.
e.g.
spawn do
ws = GDAX::WebSocket.new production: true, subscription: {
"type" => "subscribe",
"channels" => [{ "name" => "ticker", "product_ids" => ["ETH-EUR"] }]
}
ws.on "subscriptions" do |data, event|
puts "subscribed!"
end
ws.on "ticker" do |data, event|
puts data["price"] #=> e.g. 264.10000000
end
ws.run
end
Fiber.yield
Authenticating
It is possible to authenticate yourself when subscribing to the websocket feed. See the GDAX documentation on the subject.
To authenticate, pass named argument, auth
to the GDAX::WebSocket
with a GDAX::Auth
instance. See Authentication for help getting your key
, secret
, and passphrase
.
NOTE: For security purposes, it is recommended that you store your key
, secret
, and passphrase
as environment variables.
spawn do
auth = GDAX::Auth.new key: ENV["CB-ACCESS-KEY"], secret: ENV["API-SECRET"], passphrase: ENV["PASSPHRASE"]
ws = GDAX::WebSocket.new production: true, subscription: {
"type" => "subscribe",
"channels" => [{ "name" => "ticker", "product_ids" => ["ETH-EUR"] }]
}, auth: auth
ws.on "subscriptions" do |data, event|
puts "subscribed!"
end
ws.run
end
See the API Documentation for more information on GDAX::WebSocket
.
Contributing
- Fork it ( https://github.com/mccallofthewild/gdax/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- McCall Alexander mccallofthewild - creator, maintainer
gdax
- 7
- 3
- 1
- 0
- 1
- about 5 years ago
- October 10, 2017
MIT License
Fri, 22 Nov 2024 04:10:16 GMT