okcoin-ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/lib/okcoin.rb +9 -0
- data/lib/okcoin/rest.rb +121 -0
- data/lib/okcoin/version.rb +3 -0
- data/lib/okcoin/websocket.rb +76 -0
- data/okcoin-ruby.gemspec +25 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a745d632c5ad6e54ae5e7e4c0b08d07a5dc102d2
|
4
|
+
data.tar.gz: 2893e45453b5bea6863295972612bbaaa076499d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90ff1b0e76ae975f320b522d42c3045791c3bad6216f3cb15b8699b7eb511bbfa3a147b1641650bb17e45c20eeb08dab31bcfe43a16ca75c3c2756aa0eb7fe23
|
7
|
+
data.tar.gz: a604283720aaab06f4b0e05b65b59b577b46a399e9f4e5e38922bbca03de9914130ed38b3a23105f07e006d46d9cacbe2cd3fecf382d90b0c9c920ae74aac959
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Ilya Cherevkov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Okcoin API Ruby Wrapper
|
2
|
+
Supports REST and Websocket protocols
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'okcoin-ruby'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install okcoin-ruby
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Use it with REST and Websocket protocols
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it ( https://github.com/[my-github-username]/okcoin-rest-ruby/fork )
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/okcoin.rb
ADDED
data/lib/okcoin/rest.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
module 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
|
10
|
+
|
11
|
+
public
|
12
|
+
|
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
|
17
|
+
|
18
|
+
def futures_user_info
|
19
|
+
post_data = initial_post_data
|
20
|
+
post_request post_data: post_data, action: "/v1/future_userinfo.do"
|
21
|
+
end
|
22
|
+
|
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
|
27
|
+
|
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
|
32
|
+
|
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
|
42
|
+
|
43
|
+
post_data["price"] = price if price
|
44
|
+
|
45
|
+
post_request post_data: post_data, action: "/v1/future_trade.do"
|
46
|
+
end
|
47
|
+
|
48
|
+
def future_cancel(pair:, contract_type:, order_id:)
|
49
|
+
post_data = initial_post_data
|
50
|
+
|
51
|
+
post_data["symbol"] = pair
|
52
|
+
post_data["contract_type"] = contract_type
|
53
|
+
post_data["order_id"] = order_id
|
54
|
+
|
55
|
+
post_request post_data: post_data, action: "/v1/future_cancel.do"
|
56
|
+
end
|
57
|
+
|
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
|
63
|
+
|
64
|
+
post_request post_data: post_data, action: "/v1/future_orders_info.do"
|
65
|
+
end
|
66
|
+
|
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
|
73
|
+
|
74
|
+
|
75
|
+
def user_info
|
76
|
+
post_data = initial_post_data
|
77
|
+
post_request post_data: post_data, action: "/v1/userinfo.do"
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
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
|
90
|
+
end
|
91
|
+
|
92
|
+
def initial_post_data
|
93
|
+
post_data = {}
|
94
|
+
post_data["api_key"] = @api_key
|
95
|
+
post_data
|
96
|
+
end
|
97
|
+
|
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)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
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
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
@@ -0,0 +1,76 @@
|
|
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
|
data/okcoin-ruby.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'okcoin/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "okcoin-ruby"
|
8
|
+
spec.version = Okcoin::VERSION
|
9
|
+
spec.authors = ["Ilya Cherevkov"]
|
10
|
+
spec.email = ["icherevkov@gmail.com"]
|
11
|
+
spec.summary = %q{Simple Okcoin REST and Websocket API wrapper.}
|
12
|
+
spec.description = %q{Okcoin Ruby REST and Websockets API wrapper. REST is handled with Curb and Websocket with CelluloidIO}
|
13
|
+
spec.homepage = "https://github.com/ilyacherevkov/okcoin-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_dependency "curb", "0.8.8"
|
24
|
+
spec.add_dependency "celluloid-websocket-client"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: okcoin-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ilya Cherevkov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: curb
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.8
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.8
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: celluloid-websocket-client
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Okcoin Ruby REST and Websockets API wrapper. REST is handled with Curb
|
70
|
+
and Websocket with CelluloidIO
|
71
|
+
email:
|
72
|
+
- icherevkov@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/okcoin.rb
|
83
|
+
- lib/okcoin/rest.rb
|
84
|
+
- lib/okcoin/version.rb
|
85
|
+
- lib/okcoin/websocket.rb
|
86
|
+
- okcoin-ruby.gemspec
|
87
|
+
homepage: https://github.com/ilyacherevkov/okcoin-ruby
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Simple Okcoin REST and Websocket API wrapper.
|
111
|
+
test_files: []
|