okcoin-ruby 0.0.3 → 0.0.4

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: 29b9ddcb048174c66dfe139614dad5cc16a579cd
4
- data.tar.gz: d2f1dd779b0b1e8b8df576d922175e2a303a85ed
3
+ metadata.gz: 7fde5fbf15d7d1456c2e71b7c082d50027e61b2d
4
+ data.tar.gz: e3df5c33d806fe76fe3bb2342160e14f28d83dfb
5
5
  SHA512:
6
- metadata.gz: ffc68b948ae4045634288d77331033b2d716a23962cff020e13f689ccb3a287738450b592f17ce65687719ac55cfaa1faf2615eca4ac314e2e147c318041c3c9
7
- data.tar.gz: db614e11a9aade87684733d50e991152567d1b87fb63e788637f6fe31e12e434d0998c28376182c932543379852aeba6380bc1456bc1a04c0e68a633124af2af
6
+ metadata.gz: acf2b40dbf2faf83ebabd4aaf3a67735e19087dff98323373288b5fb0248b8dc2dd0c5dcb0443b256a87863c1f30fcf972f42f2ef4c9f3a6949de8225d724155
7
+ data.tar.gz: e1439a1322dbc48dbc6395a779186aa6f6f55c28525f76c5156a90a62de929d7894230f422ba7e0cf722ea837dd0ebb865c9174ca6329ab591e436182937f365
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'celluloid-websocket-client', :github => 'ilyacherevkov/celluloid-websocket-client'
4
-
5
3
  # Specify your gem's dependencies in okcoin-rest-ruby.gemspec
6
4
  gemspec
data/README.md CHANGED
@@ -19,7 +19,8 @@ And then execute:
19
19
  ### 1. REST Example
20
20
  ```
21
21
  okcoin = Okcoin::Rest.new api_key: ENV['OKCOIN_APIKEY'], secret_key: ENV['OKCOIN_SECRET']
22
- puts okcoin.user_info
22
+ puts okcoin.userinfo
23
+ puts okcoin.orderbook(pair: "btcusd", items_no: 50)
23
24
  ```
24
25
 
25
26
  ### 2. WebSocket Example
@@ -27,7 +28,9 @@ puts okcoin.user_info
27
28
  ```
28
29
  okcoin = Okcoin::WS.new api_key: ENV['OKCOIN_APIKEY'], secret_key: ENV['OKCOIN_SECRET']
29
30
  okcoin.userinfo
30
- sleep 10
31
+ okcoin.pingpong
32
+ okcoin.price_api("{'event':'addChannel','channel':'ok_btcusd_ticker'}")
33
+ while true; sleep 1; end
31
34
  okcoin.close
32
35
  ```
33
36
 
data/lib/okcoin/rest.rb CHANGED
@@ -1,118 +1,120 @@
1
- class Okcoin::Rest
2
- BASE_URI = "https://www.okcoin.com/api"
3
- TIMEOUT = 0.5
4
-
5
- def initialize(api_key: nil, secret_key: nil)
6
- @api_key = api_key
7
- @secret_key = secret_key
8
- end
1
+ class Okcoin
2
+ class Rest
3
+ BASE_URI = "https://www.okcoin.com/api"
4
+ TIMEOUT = 0.5
5
+
6
+ def initialize(api_key: nil, secret_key: nil)
7
+ @api_key = api_key
8
+ @secret_key = secret_key
9
+ end
9
10
 
10
- public
11
+ public
11
12
 
12
- def orderbook(pair:, items_no: 50)
13
- query = { "ok" => 1, "symbol" => pair, "size" => items_no }
14
- get_request(url: "/depth.do", query: query)
15
- end
13
+ def orderbook(pair:, items_no: 50)
14
+ query = { "ok" => 1, "symbol" => pair, "size" => items_no }
15
+ get_request(url: "/depth.do", query: query)
16
+ end
16
17
 
17
- def futures_user_info
18
- post_data = initial_post_data
19
- post_request post_data: post_data, action: "/v1/future_userinfo.do"
20
- end
18
+ def futures_userinfo
19
+ post_data = initial_post_data
20
+ post_request post_data: post_data, action: "/v1/future_userinfo.do"
21
+ end
21
22
 
22
- def equity
23
- post_data = initial_post_data
24
- post_request(post_data: post_data, action: "/v1/future_userinfo.do")["info"]["btc"]["account_rights"]
25
- end
23
+ def equity
24
+ post_data = initial_post_data
25
+ post_request(post_data: post_data, action: "/v1/future_userinfo.do")["info"]["btc"]["account_rights"]
26
+ end
26
27
 
27
- def futures_orderbook(pair:, contract:, items_no: 50)
28
- query = { "symbol" => pair, "contractType" => contract, "size" => items_no }
29
- get_request(url: "/future_depth.do", query: query)
30
- end
28
+ def futures_orderbook(pair:, contract:, items_no: 50)
29
+ query = { "symbol" => pair, "contractType" => contract, "size" => items_no }
30
+ get_request(url: "/future_depth.do", query: query)
31
+ end
31
32
 
32
- def trade_futures(pair:, amount:, type:, contract_type:, match_price:, price: nil, lever_rate: 10)
33
- post_data = initial_post_data
34
-
35
- post_data["symbol"] = pair
36
- post_data["contract_type"] = contract_type
37
- post_data["amount"] = amount
38
- post_data["type"] = type
39
- post_data["match_price"] = match_price
40
- post_data["lever_rate"] = lever_rate
33
+ def trade_futures(pair:, amount:, type:, contract_type:, match_price:, price: nil, lever_rate: 10)
34
+ post_data = initial_post_data
35
+
36
+ post_data["symbol"] = pair
37
+ post_data["contract_type"] = contract_type
38
+ post_data["amount"] = amount
39
+ post_data["type"] = type
40
+ post_data["match_price"] = match_price
41
+ post_data["lever_rate"] = lever_rate
41
42
 
42
- post_data["price"] = price if price
43
+ post_data["price"] = price if price
43
44
 
44
- post_request post_data: post_data, action: "/v1/future_trade.do"
45
- end
45
+ post_request post_data: post_data, action: "/v1/future_trade.do"
46
+ end
46
47
 
47
- def future_cancel(pair:, contract_type:, order_id:)
48
- post_data = initial_post_data
48
+ def future_cancel(pair:, contract_type:, order_id:)
49
+ post_data = initial_post_data
49
50
 
50
- post_data["symbol"] = pair
51
- post_data["contract_type"] = contract_type
52
- post_data["order_id"] = order_id
51
+ post_data["symbol"] = pair
52
+ post_data["contract_type"] = contract_type
53
+ post_data["order_id"] = order_id
53
54
 
54
- post_request post_data: post_data, action: "/v1/future_cancel.do"
55
- end
55
+ post_request post_data: post_data, action: "/v1/future_cancel.do"
56
+ end
56
57
 
57
- def futures_orders_info(order_id:, symbol:, contract_type:)
58
- post_data = initial_post_data
59
- post_data["symbol"] = symbol
60
- post_data["contract_type"] = contract_type
61
- post_data["order_id"] = order_id
58
+ def futures_orders_info(order_id:, symbol:, contract_type:)
59
+ post_data = initial_post_data
60
+ post_data["symbol"] = symbol
61
+ post_data["contract_type"] = contract_type
62
+ post_data["order_id"] = order_id
62
63
 
63
- post_request post_data: post_data, action: "/v1/future_orders_info.do"
64
- end
64
+ post_request post_data: post_data, action: "/v1/future_orders_info.do"
65
+ end
65
66
 
66
- def futures_position(pair:, contract_type:)
67
- post_data = initial_post_data
68
- post_data["symbol"] = pair
69
- post_data["contract_type"] = contract_type
70
- post_request post_data: post_data, action: "/v1/future_position.do"
71
- end
67
+ def futures_position(pair:, contract_type:)
68
+ post_data = initial_post_data
69
+ post_data["symbol"] = pair
70
+ post_data["contract_type"] = contract_type
71
+ post_request post_data: post_data, action: "/v1/future_position.do"
72
+ end
72
73
 
73
74
 
74
- def user_info
75
- post_data = initial_post_data
76
- post_request post_data: post_data, action: "/v1/userinfo.do"
77
- end
75
+ def userinfo
76
+ post_data = initial_post_data
77
+ post_request post_data: post_data, action: "/v1/userinfo.do"
78
+ end
78
79
 
79
- private
80
+ private
80
81
 
81
- def handle_timeouts
82
- begin
83
- yield
84
- rescue => ex
85
- Rails.logger.info "Okcoin: An error of type #{ex.class} happened, message is #{ex.message}. Retrying..."
86
- sleep TIMEOUT
87
- retry
82
+ def handle_timeouts
83
+ begin
84
+ yield
85
+ rescue => ex
86
+ Rails.logger.info "Okcoin: An error of type #{ex.class} happened, message is #{ex.message}. Retrying..."
87
+ sleep TIMEOUT
88
+ retry
89
+ end
88
90
  end
89
- end
90
91
 
91
- def initial_post_data
92
- post_data = {}
93
- post_data["api_key"] = @api_key
94
- post_data
95
- end
92
+ def initial_post_data
93
+ post_data = {}
94
+ post_data["api_key"] = @api_key
95
+ post_data
96
+ end
96
97
 
97
- def post_request(post_data:, action:)
98
- params_string = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
99
- hashed_string = params_string + "&secret_key=#{@secret_key}"
100
- signature = OpenSSL::Digest.new("md5", hashed_string).to_s.upcase
101
- post_data["sign"] = signature
102
-
103
- payload = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
104
-
105
- handle_timeouts do
106
- request = Curl.post(BASE_URI + action, payload) do |request|
107
- request.headers['Content-type'] = 'application/x-www-form-urlencoded'
98
+ def post_request(post_data:, action:)
99
+ params_string = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
100
+ hashed_string = params_string + "&secret_key=#{@secret_key}"
101
+ signature = OpenSSL::Digest.new("md5", hashed_string).to_s.upcase
102
+ post_data["sign"] = signature
103
+
104
+ payload = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
105
+
106
+ handle_timeouts do
107
+ request = Curl.post(BASE_URI + action, payload) do |request|
108
+ request.headers['Content-type'] = 'application/x-www-form-urlencoded'
109
+ end
110
+ JSON.parse(request.body_str)
108
111
  end
109
- JSON.parse(request.body_str)
110
112
  end
111
- end
112
113
 
113
- def get_request(url:, query: nil)
114
- handle_timeouts do
115
- query ? JSON.parse(Curl.get(BASE_URI + url, query).body_str) : JSON.parse(Curl.get(BASE_URI + url).body_str)
114
+ def get_request(url:, query: nil)
115
+ handle_timeouts do
116
+ query ? JSON.parse(Curl.get(BASE_URI + url, query).body_str) : JSON.parse(Curl.get(BASE_URI + url).body_str)
117
+ end
116
118
  end
117
- end
119
+ end
118
120
  end
@@ -1,3 +1,3 @@
1
1
  class Okcoin
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/okcoin/ws.rb CHANGED
@@ -1,74 +1,76 @@
1
- class Okcoin::WS
2
- include Celluloid
3
- BASE_URI = 'wss://real.okcoin.com:10440/websocket/okcoinapi'
1
+ class Okcoin
2
+ class WS
3
+ include Celluloid
4
+ BASE_URI = 'wss://real.okcoin.com:10440/websocket/okcoinapi'
4
5
 
5
- def initialize(api_key: nil, secret_key: nil)
6
- @driver = Celluloid::WebSocket::Client.new(BASE_URI, Celluloid::Actor.current)
7
- @api_key = api_key
8
- @secret_key = secret_key
9
- end
10
-
11
- public
12
- # When WebSocket is opened, register callbacks
13
- def on_open
14
- puts "Websocket connection to #{BASE_URI} established succesfully"
6
+ def initialize(api_key: nil, secret_key: nil)
7
+ @driver = Celluloid::WebSocket::Client.new(BASE_URI, Celluloid::Actor.current)
8
+ @api_key = api_key
9
+ @secret_key = secret_key
15
10
  end
16
11
 
17
- # When raw WebSocket message is received
18
- def on_message(msg)
19
- puts "Message received: #{msg}"
20
- end
12
+ public
13
+ # When WebSocket is opened, register callbacks
14
+ def on_open
15
+ puts "Websocket connection to #{BASE_URI} established succesfully"
16
+ end
21
17
 
22
- # When WebSocket is closed
23
- def on_close(code, reason)
24
- puts "WebSocket connection closed: #{code.inspect}, #{reason.inspect}"
25
- @driver.terminate
26
- terminate
27
- end
18
+ # When raw WebSocket message is received
19
+ def on_message(msg)
20
+ puts "Message received: #{msg}"
21
+ end
28
22
 
29
- # close WebSocket connection
30
- def close
31
- @driver.close
32
- end
23
+ # When WebSocket is closed
24
+ def on_close(code, reason)
25
+ puts "WebSocket connection closed: #{code.inspect}, #{reason.inspect}"
26
+ @driver.terminate
27
+ terminate
28
+ end
33
29
 
34
- def pingpong
35
- every(5){ @driver.text "{'event':'ping'}" }
36
- end
30
+ # close WebSocket connection
31
+ def close
32
+ @driver.close
33
+ end
37
34
 
38
- def userinfo
39
- post_data = initial_post_data
40
- emit(event: 'addChannel', channel: 'ok_spotusd_userinfo', post_data: post_data)
41
- end
35
+ def pingpong
36
+ every(5){ @driver.text "{'event':'ping'}" }
37
+ end
42
38
 
43
- def futures_userinfo
44
- post_data = initial_post_data
45
- emit(event: 'addChannel', channel: 'ok_futureusd_userinfo', post_data: post_data)
46
- end
39
+ def userinfo
40
+ post_data = initial_post_data
41
+ emit(event: 'addChannel', channel: 'ok_spotusd_userinfo', post_data: post_data)
42
+ end
47
43
 
48
- def price_api(data)
49
- @driver.text data
50
- end
44
+ def futures_userinfo
45
+ post_data = initial_post_data
46
+ emit(event: 'addChannel', channel: 'ok_futureusd_userinfo', post_data: post_data)
47
+ end
51
48
 
52
- private
49
+ def price_api(data)
50
+ @driver.text data
51
+ end
53
52
 
54
- def initial_post_data
55
- post_data = {}
56
- post_data['api_key'] = @api_key
57
- post_data
58
- end
53
+ private
59
54
 
60
- def sign(post_data:)
61
- params_string = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
62
- hashed_string = params_string + "&secret_key=#{@secret_key}"
63
- signature = OpenSSL::Digest.new('md5', hashed_string).to_s.upcase
64
- post_data['sign'] = signature
65
- post_data = post_data.sort
66
- post_data.collect! { |item| "'#{item[0]}':'#{item[1]}'" }
67
- post_data = post_data.join(",")
68
- end
55
+ def initial_post_data
56
+ post_data = {}
57
+ post_data['api_key'] = @api_key
58
+ post_data
59
+ end
69
60
 
70
- def emit(event:, channel:, post_data: nil)
71
- post_data = sign(post_data: post_data)
72
- @driver.text "{'event':'#{event}', 'channel':'#{channel}', 'parameters': {#{post_data}}}"
73
- end
61
+ def sign(post_data:)
62
+ params_string = post_data.sort.collect{|k, v| "#{k}=#{v}"} * '&'
63
+ hashed_string = params_string + "&secret_key=#{@secret_key}"
64
+ signature = OpenSSL::Digest.new('md5', hashed_string).to_s.upcase
65
+ post_data['sign'] = signature
66
+ post_data = post_data.sort
67
+ post_data.collect! { |item| "'#{item[0]}':'#{item[1]}'" }
68
+ post_data = post_data.join(",")
69
+ end
70
+
71
+ def emit(event:, channel:, post_data: nil)
72
+ post_data = sign(post_data: post_data)
73
+ @driver.text "{'event':'#{event}', 'channel':'#{channel}', 'parameters': {#{post_data}}}"
74
+ end
75
+ end
74
76
  end
data/okcoin-ruby.gemspec CHANGED
@@ -5,6 +5,7 @@ require 'okcoin/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "okcoin-ruby"
8
+ spec.required_ruby_version = '~> 2.0'
8
9
  spec.version = Okcoin::VERSION
9
10
  spec.authors = ["Ilya Cherevkov"]
10
11
  spec.email = ["icherevkov@gmail.com"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okcoin-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Cherevkov
@@ -93,9 +93,9 @@ require_paths:
93
93
  - lib
94
94
  required_ruby_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
- - - ">="
96
+ - - "~>"
97
97
  - !ruby/object:Gem::Version
98
- version: '0'
98
+ version: '2.0'
99
99
  required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="