okcoin 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/lib/okcoin/api_define_tool.rb +17 -15
- data/lib/okcoin/http_utils.rb +31 -31
- data/lib/okcoin/version.rb +1 -1
- data/lib/okcoin.rb +7 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52ceb78aea2b8a2c40806cd6c1c71306d2a060c4
|
4
|
+
data.tar.gz: 01faf344026e9f3a924cf1940082fe2a9860be3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ca6eac2e7b68629fc60b55495d004402f984549a0156f1b4b0aca9cca42757d7bcbba85c199ae13894a6eb00c8f1f06f4b8219028a0e04d4088bc5ca1c7a4a1
|
7
|
+
data.tar.gz: 4e9f5c3e55535643b2087cd7eec006b6bce0e366732310cd5b7f3247e37bfe340bc1ed6f36af267f24cd3467f2a05e421bc7bc7efe018937c054becbfce26003
|
data/README.md
CHANGED
@@ -27,8 +27,14 @@ gem 'okcoin'
|
|
27
27
|
## 使用
|
28
28
|
|
29
29
|
```ruby
|
30
|
+
require 'okcoin'
|
31
|
+
|
32
|
+
# 如果要看详细的访问情况,把日志级别设置为debug
|
33
|
+
Okcoin::logger.level = Logger::DEBUG
|
34
|
+
|
30
35
|
# url可选,默认就是https://www.okcoin.cn/api/,后面两个必选
|
31
36
|
client = Okcoin::Client.new url: 'https://www.okcoin.cn/api/', api_key: 'YOUR_OKCOIN_APIKEY', secret_key: 'YOUR_OKCOIN_SECRET'
|
37
|
+
|
32
38
|
client.ticker(symbol: "btc_cny")
|
33
39
|
client.trades(symbol: 'btc_cny', since: nil)
|
34
40
|
```
|
@@ -1,23 +1,25 @@
|
|
1
|
-
module
|
1
|
+
module Okcoin
|
2
|
+
module ApiDefineTool
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def define_get_api(*api_names)
|
5
|
+
api_names.each do |api_name|
|
6
|
+
define_method(api_name) do |**params|
|
7
|
+
params.reject! do |k, v| v.blank? end
|
8
|
+
http_get(@url+"v1/#{api_name}.do", params)
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
def define_post_api(*api_names)
|
14
|
+
api_names.each do |api_name|
|
15
|
+
define_method(api_name) do |**params|
|
16
|
+
params.reject! do |k, v| v.blank? end
|
17
|
+
params[:api_key] = @api_key
|
18
|
+
params[:sign] = sign(params)
|
19
|
+
http_post(@url+"v1/#{api_name}.do", params)
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
21
|
-
end
|
22
23
|
|
24
|
+
end
|
23
25
|
end
|
data/lib/okcoin/http_utils.rb
CHANGED
@@ -1,39 +1,39 @@
|
|
1
|
-
module
|
1
|
+
module Okcoin
|
2
|
+
module HttpUtils
|
3
|
+
# result = http_get("https://www.okcoin.cn/api/v1/trades.do", { symbol: 'btc_usd', since: nil })
|
4
|
+
def http_get(url, params={})
|
5
|
+
uri = URI(url)
|
6
|
+
uri.query = URI.encode_www_form(params)
|
7
|
+
Okcoin::logger.debug( 'GET: ' + (uri.query.blank? ? url : "#{url}?#{uri.query}") )
|
2
8
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
logger.debug( 'GET: ' + (uri.query.blank? ? url : "#{url}?#{uri.query}") )
|
9
|
+
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
|
10
|
+
request = Net::HTTP::Get.new uri
|
11
|
+
http.request request
|
12
|
+
end
|
8
13
|
|
9
|
-
|
10
|
-
|
11
|
-
|
14
|
+
if response.is_a?(Net::HTTPOK)
|
15
|
+
JSON.parse(response.body)
|
16
|
+
else
|
17
|
+
Okcoin::logger.error('response code is not 200!')
|
18
|
+
nil # todo: 报个异常
|
19
|
+
end
|
12
20
|
end
|
13
21
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
22
|
+
def http_post(url, params={})
|
23
|
+
uri = URI(url)
|
24
|
+
Okcoin::logger.debug( "POST: #{url}?#{URI.encode_www_form(params)}" )
|
25
|
+
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
|
26
|
+
request = Net::HTTP::Post.new(uri)
|
27
|
+
request.set_form_data(params)
|
28
|
+
http.request request
|
29
|
+
end
|
21
30
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
http.request request
|
29
|
-
end
|
30
|
-
|
31
|
-
if response.is_a?(Net::HTTPOK)
|
32
|
-
JSON.parse(response.body)
|
33
|
-
else
|
34
|
-
logger.error('response code is not 200!')
|
35
|
-
nil # todo: 报个异常
|
31
|
+
if response.is_a?(Net::HTTPOK)
|
32
|
+
JSON.parse(response.body)
|
33
|
+
else
|
34
|
+
Okcoin::logger.error('response code is not 200!')
|
35
|
+
nil # todo: 报个异常
|
36
|
+
end
|
36
37
|
end
|
37
38
|
end
|
38
39
|
end
|
39
|
-
|
data/lib/okcoin/version.rb
CHANGED
data/lib/okcoin.rb
CHANGED
@@ -9,15 +9,15 @@ require "okcoin/api_define_tool"
|
|
9
9
|
require 'okcoin/base'
|
10
10
|
require "okcoin/client"
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
$logger
|
15
|
-
|
12
|
+
module Okcoin
|
13
|
+
def self.logger
|
14
|
+
if $logger.nil?
|
15
|
+
$logger = Logger.new(STDOUT)
|
16
|
+
$logger.level = Logger::WARN
|
17
|
+
end
|
18
|
+
return $logger
|
16
19
|
end
|
17
|
-
return $logger
|
18
|
-
end
|
19
20
|
|
20
|
-
module Okcoin
|
21
21
|
class Object
|
22
22
|
def blank?
|
23
23
|
respond_to?(:empty?) ? !!empty? : !self
|