btcchina 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/lib/btcchina.rb +138 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1139eadb46ef92fb5cdc0f90b8db733b0946db2c
|
4
|
+
data.tar.gz: 5ed28af117ca47f8586ee46dae4e0c143c0ca940
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 73495ba1d7f2c871413edaae536c1d9057bb45dcbeebcbbe93f96d7aa8dd7d89dc030fc18e6b51527bdba547bd146de338f610faf82b545db7ffdabe18a36309
|
7
|
+
data.tar.gz: 5d50ea3813dc2621217dd01d0b64be6c7db313007acf34056e384d7749ba3a29a416972c912e49faa8ab9cebdb3c8bc00d293e8ae9fb3546eb99c96d4e6e7ae0
|
data/lib/btcchina.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require "net/https"
|
3
|
+
require "uri"
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
|
7
|
+
class BTCChina
|
8
|
+
|
9
|
+
def initialize(access='', secret='')
|
10
|
+
@access_key = access
|
11
|
+
@secret_key = secret
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_account_info
|
15
|
+
post_data = initial_post_data
|
16
|
+
post_data['method'] = 'getAccountInfo'
|
17
|
+
post_data['params'] = []
|
18
|
+
post_request(post_data)
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_deposit_address
|
22
|
+
get_account_info['profile']['btc_deposit_address']
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_market_depth
|
26
|
+
post_data = initial_post_data
|
27
|
+
post_data['method'] = 'getMarketDepth2'
|
28
|
+
post_data['params'] = []
|
29
|
+
post_request(post_data)["market_depth"]
|
30
|
+
end
|
31
|
+
|
32
|
+
def buy(price, amount)
|
33
|
+
price = price.to_f.round(5)
|
34
|
+
amount = amount.to_f.round(8)
|
35
|
+
post_data = initial_post_data
|
36
|
+
post_data['method']='buyOrder'
|
37
|
+
post_data['params']=[price, amount]
|
38
|
+
post_request(post_data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def sell(price, amount)
|
42
|
+
price = price.to_f.round(5)
|
43
|
+
amount = amount.to_f.round(8)
|
44
|
+
post_data = initial_post_data
|
45
|
+
post_data['method']='sellOrder'
|
46
|
+
post_data['params']=[price, amount]
|
47
|
+
post_request(post_data)
|
48
|
+
end
|
49
|
+
|
50
|
+
def cancel(order_id)
|
51
|
+
post_data = initial_post_data
|
52
|
+
post_data['method']='cancelOrder'
|
53
|
+
post_data['params']=[order_id]
|
54
|
+
post_request(post_data)
|
55
|
+
end
|
56
|
+
|
57
|
+
def current_price
|
58
|
+
market_depth = get_market_depth
|
59
|
+
ask = market_depth['ask'][0]['price']
|
60
|
+
bid = market_depth['bid'][0]['price']
|
61
|
+
(ask + bid) / 2
|
62
|
+
end
|
63
|
+
|
64
|
+
def ticker
|
65
|
+
get_request("https://data.btcchina.com/data/ticker")
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def sign(params_string)
|
71
|
+
signiture = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), @secret_key, params_string)
|
72
|
+
'Basic ' + Base64.strict_encode64(@access_key + ':' + signiture)
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_request(url)
|
76
|
+
uri = URI.parse(url)
|
77
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
78
|
+
connection(uri, request)
|
79
|
+
end
|
80
|
+
|
81
|
+
def initial_post_data
|
82
|
+
post_data = {}
|
83
|
+
post_data['tonce'] = (Time.now.to_f * 1000000).to_i.to_s
|
84
|
+
post_data
|
85
|
+
end
|
86
|
+
|
87
|
+
def post_request(post_data)
|
88
|
+
uri = URI.parse("https://api.btcchina.com/api_trade_v1.php")
|
89
|
+
payload = params_hash(post_data)
|
90
|
+
signiture_string = sign(params_string(payload.clone))
|
91
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
92
|
+
request.body = payload.to_json
|
93
|
+
request.initialize_http_header({"Accept-Encoding" => "identity", 'Json-Rpc-Tonce' => post_data['tonce'], 'Authorization' => signiture_string, 'Content-Type' => 'application/json', "User-Agent" => "Ruby BTCChina Gem"})
|
94
|
+
connection(uri, request)
|
95
|
+
end
|
96
|
+
|
97
|
+
def connection(uri, request)
|
98
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
99
|
+
# http.set_debug_output($stderr)
|
100
|
+
http.use_ssl = true
|
101
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
102
|
+
http.read_timeout = 20
|
103
|
+
http.open_timeout = 5
|
104
|
+
response(http.request(request))
|
105
|
+
end
|
106
|
+
|
107
|
+
def response(response_data)
|
108
|
+
if response_data.code == '200' && response_data.body['result']
|
109
|
+
JSON.parse(response_data.body)['result']
|
110
|
+
elsif response_data.code == '200' && response_data.body['ticker']
|
111
|
+
JSON.parse(response_data.body)['ticker']
|
112
|
+
elsif response_data.code == '200' && response_data.body['error']
|
113
|
+
error = JSON.parse(response_data.body)
|
114
|
+
warn("Error Code: #{error['error']['code']}")
|
115
|
+
warn("Error Message: #{error['error']['message']}")
|
116
|
+
false
|
117
|
+
else
|
118
|
+
raise
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def params_string(post_data)
|
123
|
+
post_data['params'] = post_data['params'].join(',')
|
124
|
+
params_hash(post_data).collect{|k, v| "#{k}=#{v}"} * '&'
|
125
|
+
end
|
126
|
+
|
127
|
+
def params_hash(post_data)
|
128
|
+
post_data['accesskey'] = @access_key
|
129
|
+
post_data['requestmethod'] = 'post'
|
130
|
+
post_data['id'] = post_data['tonce'] unless post_data.keys.include?('id')
|
131
|
+
fields=['tonce','accesskey','requestmethod','id','method','params']
|
132
|
+
ordered_data = {}
|
133
|
+
fields.each do |field|
|
134
|
+
ordered_data[field] = post_data[field]
|
135
|
+
end
|
136
|
+
ordered_data
|
137
|
+
end
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: btcchina
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby Gem wrapping the API for BTCChina Bitcoin exchange
|
14
|
+
email: s@bosh.me
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/btcchina.rb
|
20
|
+
homepage:
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Gem wrapper for BTCChina API
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|