bitstamp-client 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68718e31ee993310d025891b8900eb9eab314ac7
4
- data.tar.gz: 3f0cc7a9f8ec7b939951f8de45bde8357a696ea9
3
+ metadata.gz: d3d492215e09b3c436dbc34f194cb53e3da584ad
4
+ data.tar.gz: c52480edde3169a738e47bf7616c3ce0005ba9ac
5
5
  SHA512:
6
- metadata.gz: f0fae35f3bb75f5bbde75dd5e391df478446b6cdf7b5597fdbd3331ce9a51c732c868653818cfbbc015db5ba4e6d74e64125e87610e12296ac931ec609440138
7
- data.tar.gz: 65b74db6cc22aa81c52dae6f24b8b027f92bece7d65e7136f2efaab609ae5549fddbdc652149bb9dab57d5499651d6d5fdd2f02afecef9b2726852022c8141fe
6
+ metadata.gz: 5b2f78be1babf0ce6f0dea0d2371690e20df25fcb0ada76139bb89a27717f9ea1d7d92966aa2950f1ce09c8019f5332e9480ccb474bc7921ddc6ee1499be7877
7
+ data.tar.gz: f40c6c6ae885dd7f7f51c58be9801e55fe598233e125091013e80a7209b6c4d8f532cdfb4974740f0051e6ca9588bf8b314a4e9601dead2d51fe8d9cb2c27137
@@ -3,18 +3,8 @@ require 'json'
3
3
  require 'openssl'
4
4
  require 'typhoeus'
5
5
 
6
- require_relative './account_balance'
7
- require_relative './deposit'
8
- require_relative './conversion_rates'
9
- require_relative './exception'
10
- require_relative './order_book'
11
- require_relative './orders'
12
- require_relative './subaccount_transfer'
13
- require_relative './ticker'
14
- require_relative './trading_pairs'
15
- require_relative './transactions'
16
- require_relative './user_transactions'
17
- require_relative './withdrawal'
6
+ require_relative './handler'
7
+ require_relative './http'
18
8
 
19
9
  module Bitstamp
20
10
  class Client
@@ -22,21 +12,23 @@ module Bitstamp
22
12
 
23
13
  BASE_URI = 'https://www.bitstamp.net/api'
24
14
 
15
+ CONNECTTIMEOUT = 1
16
+ TIMEOUT = 10
17
+
18
+
25
19
  def initialize(customer_id:, api_key:, secret:)
26
20
  @customer_id = customer_id
27
21
  @api_key = api_key
28
22
  @secret = secret
29
-
30
- @connecttimeout = 1
31
- @timeout = 10
32
23
  end
33
24
 
34
25
  class << self
35
- include ::Bitstamp::ConversionRates
36
- include ::Bitstamp::OrderBook
37
- include ::Bitstamp::Ticker
38
- include ::Bitstamp::TradingPairs
39
- include ::Bitstamp::Transactions
26
+ include ::Bitstamp::Handler
27
+ include ::Bitstamp::HTTP::ConversionRates
28
+ include ::Bitstamp::HTTP::OrderBook
29
+ include ::Bitstamp::HTTP::Ticker
30
+ include ::Bitstamp::HTTP::TradingPairs
31
+ include ::Bitstamp::HTTP::Transactions
40
32
 
41
33
  def request_uri(*parts)
42
34
  uri = BASE_URI
@@ -56,37 +48,25 @@ module Bitstamp
56
48
  headers: {
57
49
  'User-Agent' => "Bitstamp::Client Ruby v#{::Bitstamp::VERSION}"
58
50
  },
59
- connecttimeout: @connecttimeout,
60
- timeout: @timeout,
51
+ connecttimeout: CONNECTTIMEOUT,
52
+ timeout: TIMEOUT
61
53
  }
62
54
 
63
55
  request = ::Typhoeus::Request.new(request_uri, request_hash)
64
56
  response = request.run
65
57
 
66
- return handle_response(response)
67
- end
68
-
69
- def handle_response(response)
70
- body = JSON.parse(response.body)
71
-
72
- if body.kind_of?(Hash) && body.has_key?('error')
73
- raise ::Bitstamp::Exception::ServiceError(body)
74
- end
75
-
76
- return body
77
- rescue JSON::ParserError
78
- raise ::Bitstamp::Exception::InvalidContent.new(response.body)
58
+ return handle_body(response.body)
79
59
  end
80
60
  end
81
61
 
82
- def_delegators "Bitstamp::Client", :request_uri, :handle_response
62
+ def_delegators "Bitstamp::Client", :request_uri
83
63
 
84
- include ::Bitstamp::AccountBalance
85
- include ::Bitstamp::Deposit
86
- include ::Bitstamp::Orders
87
- include ::Bitstamp::SubaccountTransfer
88
- include ::Bitstamp::UserTransactions
89
- include ::Bitstamp::Withdrawal
64
+ include ::Bitstamp::HTTP::AccountBalance
65
+ include ::Bitstamp::HTTP::Deposit
66
+ include ::Bitstamp::HTTP::Orders
67
+ include ::Bitstamp::HTTP::SubaccountTransfer
68
+ include ::Bitstamp::HTTP::UserTransactions
69
+ include ::Bitstamp::HTTP::Withdrawal
90
70
 
91
71
  def call(request_uri, method, body)
92
72
  body = params_with_signature(body)
@@ -110,33 +90,5 @@ module Bitstamp
110
90
 
111
91
  return OpenSSL::HMAC.hexdigest("SHA256", @secret, message).upcase
112
92
  end
113
-
114
- private
115
-
116
- def run_request(path:, nonce:, request_params: {})
117
- body = request_params.merge(
118
- {
119
- key: key,
120
- signature: signature(nonce),
121
- nonce: nonce
122
- }
123
- )
124
-
125
- request_uri = BASE_URI + path
126
-
127
- request_hash = {
128
- method: 'POST',
129
- body: body
130
- }
131
-
132
- request = ::Typhoeus::Request.new(request_uri, request_hash)
133
- response = request.run
134
- end
135
-
136
- def signature(nonce)
137
- message = nonce + @customer_id + @api_key
138
-
139
- return OpenSSL::HMAC.hexdigest("SHA256", @secret, message)
140
- end
141
93
  end
142
94
  end
@@ -1,14 +1,10 @@
1
1
  module Bitstamp
2
2
  module Exception
3
3
  class ServiceError < StandardError
4
- attr_reader :body
5
-
6
- def initialize(body)
7
- @body = body
8
- end
4
+ attr_reader :message
9
5
 
10
- def message
11
- @body.fetch('error')
6
+ def initialize(message)
7
+ @message = message
12
8
  end
13
9
 
14
10
  alias_method :to_s, :message
@@ -17,12 +13,12 @@ module Bitstamp
17
13
  class InvalidContent < StandardError
18
14
  attr_reader :body
19
15
 
20
- def initialize(body)
21
- @body = body
16
+ def initialize(raw_body)
17
+ @raw_body = raw_body
22
18
  end
23
19
 
24
20
  def message
25
- "Failed to parse body as 'json': '#{@body}'"
21
+ "Failed to parse body as 'json': '#{@raw_body}'"
26
22
  end
27
23
 
28
24
  def inspect
@@ -0,0 +1,21 @@
1
+ require_relative './exception'
2
+
3
+ module Bitstamp
4
+ module Handler
5
+ def handle_body(raw_body)
6
+ body = JSON.parse(raw_body)
7
+
8
+ if body.kind_of?(Hash)
9
+ if body.has_key?('error')
10
+ raise ::Bitstamp::Exception::ServiceError.new(body.fetch('error'))
11
+ elsif body.has_key?('status') && body.fetch('status') == 'error'
12
+ raise ::Bitstamp::Exception::ServiceError.new(body.fetch('reason'))
13
+ end
14
+ end
15
+
16
+ return body
17
+ rescue JSON::ParserError
18
+ raise ::Bitstamp::Exception::InvalidContent.new(raw_body)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ module Bitstamp
2
+ module HTTP
3
+ end
4
+ end
5
+
6
+ require_relative './http/account_balance'
7
+ require_relative './http/deposit'
8
+ require_relative './http/conversion_rates'
9
+ require_relative './http/order_book'
10
+ require_relative './http/orders'
11
+ require_relative './http/subaccount_transfer'
12
+ require_relative './http/ticker'
13
+ require_relative './http/trading_pairs'
14
+ require_relative './http/transactions'
15
+ require_relative './http/user_transactions'
16
+ require_relative './http/withdrawal'
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module AccountBalance
3
3
  def account_balance(nonce: nil, currency_pair: nil)
4
4
  params = { nonce: nonce }
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module ConversionRates
3
3
  def eur_usd_conversion_rate
4
4
  return call(request_uri('eur_usd'), 'GET', nil)
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module Deposit
3
3
  def bitcoin_deposit_address(nonce: nil)
4
4
  params = { nonce: nonce }
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module OrderBook
3
3
  def order_book(currency_pair: nil)
4
4
  if currency_pair == nil
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module Orders
3
3
  def open_orders(nonce: nil, currency_pair: nil)
4
4
  params = { nonce: nonce }
@@ -28,18 +28,21 @@ module Bitstamp
28
28
  call(request_uri('cancel_all_orders'), 'POST', params)
29
29
  end
30
30
 
31
- def buy_limit_order(nonce: nil, amount:, price:, limit_price:, currency_pair:)
31
+ def buy_limit_order(nonce: nil, amount:, price:, limit_price: nil, currency_pair:)
32
32
  params = {
33
- nonce: nonce,
34
- amount: amount,
35
- price: price,
36
- limit_price: limit_price
33
+ nonce: nonce,
34
+ amount: amount,
35
+ price: price
37
36
  }
38
37
 
38
+ if limit_price != nil
39
+ params[:limit_price] = limit_price
40
+ end
41
+
39
42
  call(request_uri('v2', 'buy', currency_pair), 'POST', params)
40
43
  end
41
44
 
42
- def buy_market_order(nonce: nil, amount:)
45
+ def buy_market_order(nonce: nil, amount:, currency_pair:)
43
46
  params = {
44
47
  nonce: nonce,
45
48
  amount: amount,
@@ -48,18 +51,21 @@ module Bitstamp
48
51
  call(request_uri('v2', 'buy', 'market', currency_pair), 'POST', params)
49
52
  end
50
53
 
51
- def sell_limit_order(nonce: nil, amount:, price:, limit_price:, currency_pair:)
54
+ def sell_limit_order(nonce: nil, amount:, price:, limit_price: nil, currency_pair:)
52
55
  params = {
53
56
  nonce: nonce,
54
57
  amount: amount,
55
- price: price,
56
- limit_price: limit_price
58
+ price: price
57
59
  }
58
60
 
61
+ if limit_price != nil
62
+ params[:limit_price] = limit_price
63
+ end
64
+
59
65
  call(request_uri('v2', 'sell', currency_pair), 'POST', params)
60
66
  end
61
67
 
62
- def sell_market_order(nonce: nil, amount:)
68
+ def sell_market_order(nonce: nil, amount:, currency_pair:)
63
69
  params = {
64
70
  nonce: nonce,
65
71
  amount: amount,
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module SubaccountTransfer
3
3
  def transfer_to_main(nonce: nil, amount:, currency:, sub_account:)
4
4
  params = {
@@ -0,0 +1,11 @@
1
+ module Bitstamp::HTTP
2
+ module Ticker
3
+ def ticker(currency_pair:)
4
+ return call(request_uri('v2', 'ticker', currency_pair), 'GET', nil)
5
+ end
6
+
7
+ def hourly_ticker(currency_pair:)
8
+ return call(request_uri('v2', 'ticker_hour', currency_pair), 'GET', nil)
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module TradingPairs
3
3
  def trading_pair_info
4
4
  return call(request_uri('v2', 'trading-pair-info'), 'GET', nil)
@@ -0,0 +1,7 @@
1
+ module Bitstamp::HTTP
2
+ module Transactions
3
+ def transactions(currency_pair:)
4
+ return call(request_uri('v2', 'transactions', currency_pair), 'GET', nil)
5
+ end
6
+ end
7
+ end
@@ -1,7 +1,12 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module UserTransactions
3
3
  def user_transactions(nonce: nil, offset: 0, limit: 100, sort: 'desc', currency_pair: nil)
4
- params = { nonce: nonce }
4
+ params = {
5
+ nonce: nonce,
6
+ offset: offset,
7
+ limit: limit,
8
+ sort: sort
9
+ }
5
10
 
6
11
  if currency_pair == nil
7
12
  call(request_uri('v2', 'user_transactions'), 'POST', params)
@@ -1,4 +1,4 @@
1
- module Bitstamp
1
+ module Bitstamp::HTTP
2
2
  module Withdrawal
3
3
  def withdrawal_requests(nonce: nil, timedelta: 86400)
4
4
  params = { nonce: nonce, timedelta: timedelta }
@@ -10,7 +10,7 @@ module Bitstamp
10
10
  params = {
11
11
  nonce: nonce,
12
12
  amount: amount,
13
- address: address,
13
+ address: address
14
14
  }
15
15
 
16
16
  call(request_uri('bitcoin_withdrawal'), 'POST', params)
@@ -0,0 +1,7 @@
1
+ module Bitstamp
2
+ module Socket
3
+ end
4
+ end
5
+
6
+ require_relative './socket/orders.rb'
7
+ require_relative './socket/trades.rb'
@@ -0,0 +1,38 @@
1
+ module Bitstamp::Socket
2
+ module Orders
3
+ def live_order_book(currency_pair:, &block)
4
+ channel = "order_book_#{currency_pair}"
5
+ event = 'data'
6
+
7
+ listen(channel, event, block)
8
+ end
9
+
10
+ def live_orders_created(currency_pair:, &block)
11
+ channel = "live_orders_#{currency_pair}"
12
+ event = 'order_created'
13
+
14
+ listen(channel, event, block)
15
+ end
16
+
17
+ def live_orders_changed(currency_pair:, &block)
18
+ channel = "live_orders_#{currency_pair}"
19
+ event = 'order_changed'
20
+
21
+ listen(channel, event, block)
22
+ end
23
+
24
+ def live_orders_deleted(currency_pair:, &block)
25
+ channel = "live_orders_#{currency_pair}"
26
+ event = 'order_deleted'
27
+
28
+ listen(channel, event, block)
29
+ end
30
+
31
+ def live_full_order_book(currency_pair:, &block)
32
+ channel = "diff_order_book_#{currency_pair}"
33
+ event = 'data'
34
+
35
+ listen(channel, event, block)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ module Bitstamp::Socket
2
+ module Trades
3
+ def live_trades(currency_pair:, &block)
4
+ channel = "live_trades_#{currency_pair}"
5
+ event = 'trade'
6
+
7
+ listen(channel, event, block)
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Bitstamp
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -2,62 +2,64 @@ require 'faye/websocket'
2
2
  require 'eventmachine'
3
3
  require 'json'
4
4
 
5
+ require_relative './handler'
6
+ require_relative './socket'
7
+
5
8
  module Bitstamp
6
9
  class Websocket
10
+ include ::Bitstamp::Handler
11
+ include ::Bitstamp::Socket::Orders
12
+ include ::Bitstamp::Socket::Trades
13
+
7
14
  BASE_URI = 'wss://ws.pusherapp.com/app/de504dc5763aeef9ff52'
8
15
  CLIENT_ID = 'bitstamp-client'
9
16
  PROTOCOL = 7
10
- PARAMS = "?#{CLIENT_ID}&version=#{Bitstamp::VERSION}&protocol=#{PROTOCOL}"
17
+ PARAMS = "?client=#{CLIENT_ID}&version=#{Bitstamp::VERSION}&protocol=#{PROTOCOL}"
11
18
 
12
19
  def initialize(logger)
13
20
  @logger = logger
14
21
  end
15
22
 
16
- def live_trades(currency_pair:, &block)
17
- channel = "live_trades_#{currency_pair}"
18
- event = 'trade'
19
-
20
- listen(channel, event, block)
21
- end
22
-
23
- private
24
-
25
23
  def listen(channel, event, block)
26
24
  EM.run do
27
- websocket = Faye::WebSocket::Client.new(BASE_URI + PARAMS)
25
+ setup_socket(channel, event, block)
26
+ end
27
+ end
28
28
 
29
- websocket.on(:open) do |message|
30
- subscribe(websocket, channel)
31
- @logger.debug("Opened connection and subscribed to #{channel}")
32
- end
29
+ def setup_socket(channel, event, block)
30
+ websocket = Faye::WebSocket::Client.new(BASE_URI + PARAMS)
31
+
32
+ websocket.on(:open) do |message|
33
+ subscribe(websocket, channel)
34
+ @logger.debug("Opened connection and subscribed to '#{channel}'")
35
+ end
33
36
 
34
- websocket.on(:message) do |message|
35
- parsed_message = JSON.parse(message.data)
37
+ websocket.on(:message) do |message|
38
+ parsed_message = handle_body(message.data)
36
39
 
37
- case parsed_message.fetch('event')
38
- when event
39
- data = JSON.parse(parsed_message.fetch('data'))
40
- block.call(data)
41
- when 'pusher:connection_established'
42
- @logger.debug('Connection established')
43
- when 'pusher_internal:subscription_succeeded'
44
- @logger.debug("Subscription to channel '#{channel}' succeeded")
45
- else
46
- @logger.debug("Received unhandled message: #{message.data.to_s}")
47
- end
40
+ case parsed_message.fetch('event')
41
+ when event
42
+ data = handle_body(parsed_message.fetch('data'))
43
+ block.call(data)
44
+ when 'pusher:connection_established'
45
+ @logger.debug('Connection established')
46
+ when 'pusher_internal:subscription_succeeded'
47
+ @logger.debug("Subscription to channel '#{channel}' succeeded")
48
+ else
49
+ @logger.debug("Received unhandled message: #{message.data.to_s}")
48
50
  end
51
+ end
49
52
 
50
- websocket.on(:close) do |message|
51
- @logger.debug("Closed websocket connection: #{message.data.to_s}")
53
+ websocket.on(:close) do |message|
54
+ @logger.debug("Closed websocket connection: #{message.data.to_s}")
52
55
 
53
- return
54
- end
56
+ return
55
57
  end
56
58
  end
57
59
 
58
60
  def subscribe(websocket, channel)
59
61
  subscribe_message = {
60
- event: "pusher:subscribe",
62
+ event: 'pusher:subscribe',
61
63
  data: { channel: channel }
62
64
  }
63
65
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitstamp-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Schoknecht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-01 00:00:00.000000000 Z
11
+ date: 2017-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -172,21 +172,26 @@ extensions: []
172
172
  extra_rdoc_files: []
173
173
  files:
174
174
  - lib/bitstamp.rb
175
- - lib/bitstamp/account_balance.rb
176
175
  - lib/bitstamp/client.rb
177
- - lib/bitstamp/conversion_rates.rb
178
- - lib/bitstamp/deposit.rb
179
176
  - lib/bitstamp/exception.rb
180
- - lib/bitstamp/order_book.rb
181
- - lib/bitstamp/orders.rb
182
- - lib/bitstamp/subaccount_transfer.rb
183
- - lib/bitstamp/ticker.rb
184
- - lib/bitstamp/trading_pairs.rb
185
- - lib/bitstamp/transactions.rb
186
- - lib/bitstamp/user_transactions.rb
177
+ - lib/bitstamp/handler.rb
178
+ - lib/bitstamp/http.rb
179
+ - lib/bitstamp/http/account_balance.rb
180
+ - lib/bitstamp/http/conversion_rates.rb
181
+ - lib/bitstamp/http/deposit.rb
182
+ - lib/bitstamp/http/order_book.rb
183
+ - lib/bitstamp/http/orders.rb
184
+ - lib/bitstamp/http/subaccount_transfer.rb
185
+ - lib/bitstamp/http/ticker.rb
186
+ - lib/bitstamp/http/trading_pairs.rb
187
+ - lib/bitstamp/http/transactions.rb
188
+ - lib/bitstamp/http/user_transactions.rb
189
+ - lib/bitstamp/http/withdrawal.rb
190
+ - lib/bitstamp/socket.rb
191
+ - lib/bitstamp/socket/orders.rb
192
+ - lib/bitstamp/socket/trades.rb
187
193
  - lib/bitstamp/version.rb
188
194
  - lib/bitstamp/websocket.rb
189
- - lib/bitstamp/withdrawal.rb
190
195
  homepage: https://github.com/tobischo/bitstamp
191
196
  licenses:
192
197
  - MIT
@@ -1,19 +0,0 @@
1
- module Bitstamp
2
- module Ticker
3
- def ticker(currency_pair: nil)
4
- if currency_pair == nil
5
- return call(request_uri('ticker'), 'GET', nil)
6
- else
7
- return call(request_uri('v2', 'ticker', currency_pair), 'GET', nil)
8
- end
9
- end
10
-
11
- def hourly_ticker(currency_pair: nil)
12
- if currency_pair == nil
13
- return call(request_uri('ticker_hour'), 'GET', nil)
14
- else
15
- return call(request_uri('v2', 'ticker_hour', currency_pair), 'GET', nil)
16
- end
17
- end
18
- end
19
- end
@@ -1,11 +0,0 @@
1
- module Bitstamp
2
- module Transactions
3
- def transactions(currency_pair: nil)
4
- if currency_pair == nil
5
- return call(request_uri('transactions'), 'GET', nil)
6
- else
7
- return call(request_uri('v2', 'transactions', currency_pair, ''), 'GET', nil)
8
- end
9
- end
10
- end
11
- end