bibox 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +15 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +5 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +67 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +67 -0
  11. data/Rakefile +6 -0
  12. data/bibox.gemspec +36 -0
  13. data/bin/console +31 -0
  14. data/bin/setup +8 -0
  15. data/bin/websocket +71 -0
  16. data/credentials.yml.example +7 -0
  17. data/lib/.DS_Store +0 -0
  18. data/lib/bibox.rb +74 -0
  19. data/lib/bibox/.DS_Store +0 -0
  20. data/lib/bibox/configuration.rb +21 -0
  21. data/lib/bibox/constants.rb +6 -0
  22. data/lib/bibox/errors.rb +17 -0
  23. data/lib/bibox/extensions/string.rb +13 -0
  24. data/lib/bibox/models/asset.rb +29 -0
  25. data/lib/bibox/models/base.rb +26 -0
  26. data/lib/bibox/models/bill.rb +26 -0
  27. data/lib/bibox/models/ohlcv.rb +16 -0
  28. data/lib/bibox/models/order.rb +38 -0
  29. data/lib/bibox/models/order_book.rb +29 -0
  30. data/lib/bibox/models/pair.rb +24 -0
  31. data/lib/bibox/models/ticker.rb +18 -0
  32. data/lib/bibox/models/trade.rb +13 -0
  33. data/lib/bibox/models/transfer.rb +26 -0
  34. data/lib/bibox/models/user_assets.rb +23 -0
  35. data/lib/bibox/rest/client.rb +97 -0
  36. data/lib/bibox/rest/errors.rb +16 -0
  37. data/lib/bibox/rest/private/assets.rb +33 -0
  38. data/lib/bibox/rest/private/bills.rb +31 -0
  39. data/lib/bibox/rest/private/order_book.rb +21 -0
  40. data/lib/bibox/rest/private/orders.rb +131 -0
  41. data/lib/bibox/rest/private/transfers.rb +49 -0
  42. data/lib/bibox/rest/public/klines.rb +22 -0
  43. data/lib/bibox/rest/public/order_book.rb +16 -0
  44. data/lib/bibox/rest/public/pairs.rb +23 -0
  45. data/lib/bibox/rest/public/ticker.rb +16 -0
  46. data/lib/bibox/rest/public/trades.rb +16 -0
  47. data/lib/bibox/utilities.rb +68 -0
  48. data/lib/bibox/version.rb +3 -0
  49. data/lib/bibox/websocket/client.rb +149 -0
  50. metadata +232 -0
@@ -0,0 +1,3 @@
1
+ module Bibox
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,149 @@
1
+ module Bibox
2
+ module Websocket
3
+ # Websocket client for Coinbase Exchange
4
+ class Client
5
+ attr_accessor :url, :prefix, :keepalive, :socket, :reactor_owner, :matchers, :callbacks
6
+
7
+ def initialize(options: {})
8
+ self.url = options.fetch(:url, "wss://push.bibox.com")
9
+ self.prefix = options.fetch(:prefix, "bibox_sub_spot_")
10
+ self.keepalive = options.fetch(:keepalive, false)
11
+
12
+ self.matchers = {
13
+ trading_pairs: /[A-Z]{3}_[A-Z]{3}_market/,
14
+ order_book: /[A-Z]{3}_[A-Z]{3}_depth$/,
15
+ trades: /[A-Z]{3}_[A-Z]{3}_deals$/,
16
+ ticker: /[A-Z]{3}_[A-Z]{3}_ticker$/,
17
+ klines: /[A-Z]{3}_[A-Z]{3}_kline_(?<period>.*)$/
18
+ }
19
+
20
+ self.callbacks = {
21
+ subscribe: -> { subscribe! },
22
+ message: -> (data) { message(data) },
23
+ trading_pairs: -> (data) { nil },
24
+ order_book: -> (data) { nil },
25
+ trades: -> (data) { nil },
26
+ ticker: -> (data) { nil },
27
+ klines: -> (data) { nil },
28
+ }
29
+ end
30
+
31
+ def start!
32
+ if EventMachine.reactor_running?
33
+ self.reactor_owner = false
34
+ refresh!
35
+ else
36
+ self.reactor_owner = true
37
+ EM.run { refresh! }
38
+ end
39
+ end
40
+
41
+ def stop!
42
+ if self.reactor_owner == true
43
+ self.socket.onclose = -> (_event) { EM.stop }
44
+ else
45
+ self.socket.onclose = -> (_event) { nil }
46
+ end
47
+
48
+ self.socket.close
49
+ end
50
+
51
+ def refresh!
52
+ self.socket = Faye::WebSocket::Client.new(self.url)
53
+ self.socket.onopen = method(:on_open)
54
+ self.socket.onmessage = method(:on_message)
55
+ self.socket.onclose = method(:on_close)
56
+ self.socket.onerror = method(:on_error)
57
+ end
58
+
59
+ def subscribe!
60
+ subscribe_to_trading_pairs!
61
+ subscribe_to_order_book!(pairs: ["BIX_BTC", "BIX_ETH"])
62
+ subscribe_to_trades!(pair: ["BIX_BTC", "BIX_ETH"])
63
+ subscribe_to_ticker!(pair: ["BIX_BTC", "BIX_ETH"])
64
+ subscribe_to_klines!(pair: ["BIX_BTC", "BIX_ETH"], periods: ["5min"])
65
+ end
66
+
67
+ def subscribe_to_trading_pairs!
68
+ subscribe_to! channel_name("ALL_ALL_market")
69
+ end
70
+
71
+ def subscribe_to_order_book!(pairs: [])
72
+ pairs&.each { |pair| subscribe_to! channel_name("#{pair}_depth") }
73
+ end
74
+
75
+ def subscribe_to_trades!(pairs: [])
76
+ pairs&.each { |pair| subscribe_to! channel_name("#{pair}_deals") }
77
+ end
78
+
79
+ def subscribe_to_ticker!(pairs: [])
80
+ pairs&.each { |pair| subscribe_to! channel_name("#{pair}_ticker") }
81
+ end
82
+
83
+ def subscribe_to_klines!(pairs: [], periods: ["5min"])
84
+ pairs&.each do |pair|
85
+ periods&.each do |period|
86
+ subscribe_to! channel_name("#{pair}_kline_#{period}")
87
+ end
88
+ end
89
+ end
90
+
91
+ def subscribe_to!(channel, binary: 0)
92
+ message = { event: "addChannel", channel: channel, binary: binary }
93
+ self.socket.send(message.to_json)
94
+ end
95
+
96
+ def channel_name(channel)
97
+ "#{self.prefix}#{channel}"
98
+ end
99
+
100
+ def message(data)
101
+ channel = data.fetch("channel", nil)&.gsub(self.prefix, "")
102
+
103
+ case channel
104
+ when self.matchers[:trading_pairs]
105
+ self.callbacks[:trading_pairs].call(data)
106
+ when self.matchers[:order_book]
107
+ self.callbacks[:order_book].call(data)
108
+ when self.matchers[:trades]
109
+ self.callbacks[:trades].call(data)
110
+ when self.matchers[:ticker]
111
+ self.callbacks[:ticker].call(data)
112
+ when self.matchers[:klines]
113
+ period = channel.match(self.matchers[:klines])&.[](:period)
114
+ data.merge!("period" => period) if !period.to_s.empty?
115
+ self.callbacks[:klines].call(data)
116
+ end
117
+ end
118
+
119
+ private
120
+ def parse(data)
121
+ return data if data.is_a? Hash
122
+ return JSON.parse(data)
123
+ rescue => err
124
+ return data
125
+ end
126
+
127
+ def on_open(_event)
128
+ self.callbacks[:subscribe].call
129
+ end
130
+
131
+ def on_message(event)
132
+ self.callbacks[:message].call(parse(event.data)&.first)
133
+ end
134
+
135
+ def on_close(_event)
136
+ if self.keepalive
137
+ refresh!
138
+ else
139
+ EM.stop
140
+ end
141
+ end
142
+
143
+ def on_error(event)
144
+ raise ::Bibox::Errors::WebsocketError.new(event.message)
145
+ end
146
+
147
+ end
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bibox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Johnsson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: faye-websocket
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: eventmachine
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rdoc
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '6.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '6.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: vcr
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '4.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '4.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.1'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.1'
153
+ description: Ruby REST client for crypto currency exchange Bibox
154
+ email:
155
+ - sebastian.johnsson@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".DS_Store"
161
+ - ".gitignore"
162
+ - ".rspec"
163
+ - ".travis.yml"
164
+ - CODE_OF_CONDUCT.md
165
+ - Gemfile
166
+ - Gemfile.lock
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bibox.gemspec
171
+ - bin/console
172
+ - bin/setup
173
+ - bin/websocket
174
+ - credentials.yml.example
175
+ - lib/.DS_Store
176
+ - lib/bibox.rb
177
+ - lib/bibox/.DS_Store
178
+ - lib/bibox/configuration.rb
179
+ - lib/bibox/constants.rb
180
+ - lib/bibox/errors.rb
181
+ - lib/bibox/extensions/string.rb
182
+ - lib/bibox/models/asset.rb
183
+ - lib/bibox/models/base.rb
184
+ - lib/bibox/models/bill.rb
185
+ - lib/bibox/models/ohlcv.rb
186
+ - lib/bibox/models/order.rb
187
+ - lib/bibox/models/order_book.rb
188
+ - lib/bibox/models/pair.rb
189
+ - lib/bibox/models/ticker.rb
190
+ - lib/bibox/models/trade.rb
191
+ - lib/bibox/models/transfer.rb
192
+ - lib/bibox/models/user_assets.rb
193
+ - lib/bibox/rest/client.rb
194
+ - lib/bibox/rest/errors.rb
195
+ - lib/bibox/rest/private/assets.rb
196
+ - lib/bibox/rest/private/bills.rb
197
+ - lib/bibox/rest/private/order_book.rb
198
+ - lib/bibox/rest/private/orders.rb
199
+ - lib/bibox/rest/private/transfers.rb
200
+ - lib/bibox/rest/public/klines.rb
201
+ - lib/bibox/rest/public/order_book.rb
202
+ - lib/bibox/rest/public/pairs.rb
203
+ - lib/bibox/rest/public/ticker.rb
204
+ - lib/bibox/rest/public/trades.rb
205
+ - lib/bibox/utilities.rb
206
+ - lib/bibox/version.rb
207
+ - lib/bibox/websocket/client.rb
208
+ homepage: https://github.com/SebastianJ/bibox
209
+ licenses:
210
+ - MIT
211
+ metadata: {}
212
+ post_install_message:
213
+ rdoc_options: []
214
+ require_paths:
215
+ - lib
216
+ required_ruby_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ required_rubygems_version: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - ">="
224
+ - !ruby/object:Gem::Version
225
+ version: '0'
226
+ requirements: []
227
+ rubyforge_project:
228
+ rubygems_version: 2.7.4
229
+ signing_key:
230
+ specification_version: 4
231
+ summary: Ruby client for Bibox's API'
232
+ test_files: []