okcoin-ruby 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -6
- data/lib/okcoin-ruby.rb +4 -0
- data/lib/okcoin/rest.rb +1 -4
- data/lib/okcoin/version.rb +2 -2
- data/lib/okcoin/ws.rb +74 -0
- data/okcoin-ruby.gemspec +2 -2
- metadata +5 -6
- data/lib/okcoin.rb +0 -9
- data/lib/okcoin/websocket.rb +0 -76
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29b9ddcb048174c66dfe139614dad5cc16a579cd
|
4
|
+
data.tar.gz: d2f1dd779b0b1e8b8df576d922175e2a303a85ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffc68b948ae4045634288d77331033b2d716a23962cff020e13f689ccb3a287738450b592f17ce65687719ac55cfaa1faf2615eca4ac314e2e147c318041c3c9
|
7
|
+
data.tar.gz: db614e11a9aade87684733d50e991152567d1b87fb63e788637f6fe31e12e434d0998c28376182c932543379852aeba6380bc1456bc1a04c0e68a633124af2af
|
data/README.md
CHANGED
@@ -3,23 +3,33 @@ Supports REST and Websocket protocols
|
|
3
3
|
|
4
4
|
## Installation
|
5
5
|
|
6
|
-
Add
|
6
|
+
Add these gems into your Gemfile:
|
7
7
|
|
8
8
|
```ruby
|
9
9
|
gem 'okcoin-ruby'
|
10
|
+
gem 'celluloid-websocket-client', :github => 'ilyacherevkov/celluloid-websocket-client'
|
10
11
|
```
|
11
12
|
|
12
13
|
And then execute:
|
13
14
|
|
14
|
-
$ bundle
|
15
|
+
$ bundle install
|
15
16
|
|
16
|
-
|
17
|
+
## Usage
|
17
18
|
|
18
|
-
|
19
|
+
### 1. REST Example
|
20
|
+
```
|
21
|
+
okcoin = Okcoin::Rest.new api_key: ENV['OKCOIN_APIKEY'], secret_key: ENV['OKCOIN_SECRET']
|
22
|
+
puts okcoin.user_info
|
23
|
+
```
|
19
24
|
|
20
|
-
|
25
|
+
### 2. WebSocket Example
|
21
26
|
|
22
|
-
|
27
|
+
```
|
28
|
+
okcoin = Okcoin::WS.new api_key: ENV['OKCOIN_APIKEY'], secret_key: ENV['OKCOIN_SECRET']
|
29
|
+
okcoin.userinfo
|
30
|
+
sleep 10
|
31
|
+
okcoin.close
|
32
|
+
```
|
23
33
|
|
24
34
|
## Contributing
|
25
35
|
|
data/lib/okcoin-ruby.rb
ADDED
data/lib/okcoin/rest.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
class Rest
|
1
|
+
class Okcoin::Rest
|
3
2
|
BASE_URI = "https://www.okcoin.com/api"
|
4
3
|
TIMEOUT = 0.5
|
5
4
|
|
@@ -116,6 +115,4 @@ module Okcoin
|
|
116
115
|
query ? JSON.parse(Curl.get(BASE_URI + url, query).body_str) : JSON.parse(Curl.get(BASE_URI + url).body_str)
|
117
116
|
end
|
118
117
|
end
|
119
|
-
end
|
120
|
-
|
121
118
|
end
|
data/lib/okcoin/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0.
|
1
|
+
class Okcoin
|
2
|
+
VERSION = "0.0.3"
|
3
3
|
end
|
data/lib/okcoin/ws.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
class Okcoin::WS
|
2
|
+
include Celluloid
|
3
|
+
BASE_URI = 'wss://real.okcoin.com:10440/websocket/okcoinapi'
|
4
|
+
|
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"
|
15
|
+
end
|
16
|
+
|
17
|
+
# When raw WebSocket message is received
|
18
|
+
def on_message(msg)
|
19
|
+
puts "Message received: #{msg}"
|
20
|
+
end
|
21
|
+
|
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
|
28
|
+
|
29
|
+
# close WebSocket connection
|
30
|
+
def close
|
31
|
+
@driver.close
|
32
|
+
end
|
33
|
+
|
34
|
+
def pingpong
|
35
|
+
every(5){ @driver.text "{'event':'ping'}" }
|
36
|
+
end
|
37
|
+
|
38
|
+
def userinfo
|
39
|
+
post_data = initial_post_data
|
40
|
+
emit(event: 'addChannel', channel: 'ok_spotusd_userinfo', post_data: post_data)
|
41
|
+
end
|
42
|
+
|
43
|
+
def futures_userinfo
|
44
|
+
post_data = initial_post_data
|
45
|
+
emit(event: 'addChannel', channel: 'ok_futureusd_userinfo', post_data: post_data)
|
46
|
+
end
|
47
|
+
|
48
|
+
def price_api(data)
|
49
|
+
@driver.text data
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def initial_post_data
|
55
|
+
post_data = {}
|
56
|
+
post_data['api_key'] = @api_key
|
57
|
+
post_data
|
58
|
+
end
|
59
|
+
|
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
|
69
|
+
|
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
|
74
|
+
end
|
data/okcoin-ruby.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Okcoin::VERSION
|
9
9
|
spec.authors = ["Ilya Cherevkov"]
|
10
10
|
spec.email = ["icherevkov@gmail.com"]
|
11
|
-
spec.summary = %q{
|
12
|
-
spec.description = %q{
|
11
|
+
spec.summary = %q{A ruby library for okcoin.com}
|
12
|
+
spec.description = %q{A ruby library for okcoin.com}
|
13
13
|
spec.homepage = "https://github.com/ilyacherevkov/okcoin-ruby"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
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.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Cherevkov
|
@@ -66,8 +66,7 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description:
|
70
|
-
and Websocket with CelluloidIO
|
69
|
+
description: A ruby library for okcoin.com
|
71
70
|
email:
|
72
71
|
- icherevkov@gmail.com
|
73
72
|
executables: []
|
@@ -79,10 +78,10 @@ files:
|
|
79
78
|
- LICENSE.txt
|
80
79
|
- README.md
|
81
80
|
- Rakefile
|
82
|
-
- lib/okcoin.rb
|
81
|
+
- lib/okcoin-ruby.rb
|
83
82
|
- lib/okcoin/rest.rb
|
84
83
|
- lib/okcoin/version.rb
|
85
|
-
- lib/okcoin/
|
84
|
+
- lib/okcoin/ws.rb
|
86
85
|
- okcoin-ruby.gemspec
|
87
86
|
homepage: https://github.com/ilyacherevkov/okcoin-ruby
|
88
87
|
licenses:
|
@@ -107,5 +106,5 @@ rubyforge_project:
|
|
107
106
|
rubygems_version: 2.2.2
|
108
107
|
signing_key:
|
109
108
|
specification_version: 4
|
110
|
-
summary:
|
109
|
+
summary: A ruby library for okcoin.com
|
111
110
|
test_files: []
|
data/lib/okcoin.rb
DELETED
data/lib/okcoin/websocket.rb
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
module Okcoin
|
2
|
-
class Websocket
|
3
|
-
include Celluloid
|
4
|
-
BASE_URI = 'wss://real.okcoin.com:10440/websocket/okcoinapi'
|
5
|
-
|
6
|
-
def initialize(api_key: nil, secret_key: nil)
|
7
|
-
@driver = Celluloid::WebSocket::Client.new(URL, Celluloid::Actor.current)
|
8
|
-
@api_key = api_key
|
9
|
-
@secret_key = secret_key
|
10
|
-
end
|
11
|
-
|
12
|
-
# When WebSocket is opened, register callbacks
|
13
|
-
def on_open
|
14
|
-
puts "Websocket connection to #{BASE_URI} established succesfully"
|
15
|
-
end
|
16
|
-
|
17
|
-
# When raw WebSocket message is received
|
18
|
-
def on_message(msg)
|
19
|
-
puts "Message received: #{msg}"
|
20
|
-
end
|
21
|
-
|
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
|
28
|
-
|
29
|
-
# close WebSocket connection
|
30
|
-
def close
|
31
|
-
@driver.close
|
32
|
-
end
|
33
|
-
|
34
|
-
def pingpong
|
35
|
-
every(5){ @driver.text "{'event':'ping'}" }
|
36
|
-
end
|
37
|
-
|
38
|
-
def userinfo
|
39
|
-
post_data = initial_post_data
|
40
|
-
emit(event: 'addChannel', channel: 'ok_spotusd_userinfo', post_data: post_data)
|
41
|
-
end
|
42
|
-
|
43
|
-
def futures_userinfo
|
44
|
-
post_data = initial_post_data
|
45
|
-
emit(event: 'addChannel', channel: 'ok_futureusd_userinfo', post_data: post_data)
|
46
|
-
end
|
47
|
-
|
48
|
-
def price_api(data)
|
49
|
-
@driver.text data
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def initial_post_data
|
55
|
-
post_data = {}
|
56
|
-
post_data['api_key'] = @api_key
|
57
|
-
post_data
|
58
|
-
end
|
59
|
-
|
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
|
69
|
-
|
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
|
74
|
-
|
75
|
-
end
|
76
|
-
end
|