kublai 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a69a7cc814a4e8e4006e19be8c3886b769f14e0b
4
+ data.tar.gz: 660915f65724a1c7ef67285f6b17cc1ad08a274f
5
+ SHA512:
6
+ metadata.gz: 1862bd0d5cf727f543acd2b352e26e4e6b06d680e8b90878f4c452cd904b40d598605104ca733395ef34a9e250d37542978625fecef04700595ebb19811799ab
7
+ data.tar.gz: 4c2e3d34fa60022be5eda104a56fe3f174efdcba50cbdefbbb1ccea78356ce28ab821a2902087471d93b080112dafc83b6568c6c1cdeaff4787f99fa7420c879
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ Gemfile.lock
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ # OSX Gubbins
22
+ .DS_Store
23
+ .AppleDouble
24
+ .LSOverride
25
+
26
+ # Icon must ends with two \r.
27
+ Icon
28
+
29
+ # Thumbnails
30
+ ._*
31
+
32
+ # Files that might appear on external disk
33
+ .Spotlight-V100
34
+ .Trashes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kublai.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Lewis Clayton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ Kublai - BTCChina API Wrapper
2
+ ======
3
+
4
+ What
5
+ ==========
6
+ Kublai is a lightweight API Wrapper for BTCChina written in Ruby.
7
+
8
+ The API is a lot more basic than MtGox's. Even so not all API methods are implemented currently in Kublai. However, you can add in more like this:
9
+
10
+ JSON Request
11
+ {"method":"requestWithdrawal","params":["BTC",0.1],"id":1}
12
+
13
+ def request_withdrawal(amount)
14
+ amount = amount.to_f.round(8)
15
+ post_data = initial_post_data
16
+ post_data['method']='requestWithdrawal'
17
+ post_data['params']=['BTC', amount]
18
+ request(post_data)
19
+ end
20
+
21
+ Why
22
+ ==========
23
+ Python, Java, PHP and C++ all have documented wrappers on the BTCChina site. Ruby is not well supported at this time this is an effort to chage that.
24
+
25
+ http://btcchina.org/api-trade-documentation-en
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ gem 'kublai'
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install kublai
40
+
41
+ ## Usage
42
+
43
+ access_key = "20c0bc14-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
44
+ secret_key = "66d218e5-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
45
+ kublai = Kublai::BTCChina.new(access_key, secret_key )
46
+
47
+ kublai.ticker
48
+ => {"high"=>"5999.00", "low"=>"3821.13", "buy"=>"4978.96", "sell"=>"4990.00", "last"=>"4978.96", "vol"=>"149135.77100000"}
49
+
50
+ kublai.current_price
51
+ => 4855.995
52
+
53
+ kublai.buy(4856, 6)
54
+ Error Code: -32003
55
+ Error Message: Insufficient CNY balance
56
+ => false
57
+
58
+ kublai.buy(4856, 1.001339102)
59
+ => true
60
+
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
69
+
70
+ Who
71
+ ==========
72
+ Lewis Clayton mail@l.ew.is
73
+
74
+ Licence
75
+ ==========
76
+
77
+ MIT License
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/kublai.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kublai/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kublai"
8
+ spec.version = Kublai::VERSION
9
+ spec.authors = ["Lewis Clayton"]
10
+ spec.email = ["mail@l.ew.is"]
11
+ spec.date = '2013-12-07'
12
+ spec.description = %q{Ruby wrapper for the BTCChina Trade API. BTCChina allows you to trade Chinese Yuan (CNY) for Bitcoins (BTC) or Bitcoins for Chinese Yuan.}
13
+ spec.summary = %q{Ruby wrapper for the BTCChina Trade API.}
14
+ spec.homepage = 'http://l.ew.is/kublai_btcchina_api_ruby/'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency('json', '~> 1.8.1')
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+
26
+ spec.has_rdoc = false
27
+ end
@@ -0,0 +1,3 @@
1
+ module Kublai
2
+ VERSION = "0.0.1"
3
+ end
data/lib/kublai.rb ADDED
@@ -0,0 +1,153 @@
1
+ require 'kublai/version'
2
+ require 'base64'
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'json'
6
+
7
+
8
+ module Kublai
9
+ class BTCChina
10
+
11
+ def initialize(access='', secret='')
12
+ @access_key = access
13
+ @secret_key = secret
14
+ end
15
+
16
+ public
17
+
18
+ def get_account_info
19
+ post_data = initial_post_data
20
+ post_data['method'] = 'getAccountInfo'
21
+ post_data['params'] = []
22
+ post_request(post_data)
23
+ end
24
+
25
+ def get_deposit_address
26
+ get_account_info['profile']['btc_deposit_address']
27
+ end
28
+
29
+ def get_market_depth
30
+ post_data = initial_post_data
31
+ post_data['method'] = 'getMarketDepth2'
32
+ post_data['params'] = []
33
+ post_request(post_data)["market_depth"]
34
+ end
35
+
36
+ def buy(price, amount)
37
+ price = cut_off(price, 5)
38
+ amount = cut_off(amount, 5)
39
+ post_data = initial_post_data
40
+ post_data['method']='buyOrder'
41
+ post_data['params']=[price, amount]
42
+ post_request(post_data)
43
+ end
44
+
45
+ def sell(price, amount)
46
+ price = cut_off(price, 5)
47
+ amount = cut_off(amount, 5)
48
+ post_data = initial_post_data
49
+ post_data['method']='sellOrder'
50
+ post_data['params']=[price, amount]
51
+ post_request(post_data)
52
+ end
53
+
54
+ def cancel(order_id)
55
+ post_data = initial_post_data
56
+ post_data['method']='cancelOrder'
57
+ post_data['params']=[order_id]
58
+ post_request(post_data)
59
+ end
60
+
61
+ def current_price
62
+ market_depth = get_market_depth
63
+ ask = market_depth['ask'][0]['price']
64
+ bid = market_depth['bid'][0]['price']
65
+ (ask + bid) / 2
66
+ end
67
+
68
+ def ticker
69
+ get_request("https://data.btcchina.com/data/ticker")
70
+ end
71
+
72
+ private
73
+
74
+ def cut_off(num, exp=0)
75
+ multiplier = 10 ** exp
76
+ cut = ((num * multiplier).floor).to_f/multiplier.to_f
77
+ return cut.floor if cut == cut.floor
78
+ cut
79
+ end
80
+
81
+ def sign(params_string)
82
+ signiture = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), @secret_key, params_string)
83
+ 'Basic ' + Base64.strict_encode64(@access_key + ':' + signiture)
84
+ end
85
+
86
+ def get_request(url)
87
+ uri = URI.parse(url)
88
+ request = Net::HTTP::Get.new(uri.request_uri)
89
+ connection(uri, request)
90
+ end
91
+
92
+ def initial_post_data
93
+ post_data = {}
94
+ post_data['tonce'] = (Time.now.to_f * 1000000).to_i.to_s
95
+ post_data
96
+ end
97
+
98
+ def post_request(post_data)
99
+ uri = URI.parse("https://api.btcchina.com/api_trade_v1.php")
100
+ payload = params_hash(post_data)
101
+ signiture_string = sign(params_string(payload.clone))
102
+ request = Net::HTTP::Post.new(uri.request_uri)
103
+ request.body = payload.to_json
104
+ request.initialize_http_header({"Accept-Encoding" => "identity", 'Json-Rpc-Tonce' => post_data['tonce'], 'Authorization' => signiture_string, 'Content-Type' => 'application/json', "User-Agent" => "Kublai"})
105
+ connection(uri, request)
106
+ end
107
+
108
+ def connection(uri, request)
109
+ http = Net::HTTP.new(uri.host, uri.port)
110
+ # http.set_debug_output($stderr)
111
+ http.use_ssl = true
112
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
113
+ http.read_timeout = 20
114
+ http.open_timeout = 5
115
+ response(http.request(request))
116
+ end
117
+
118
+ def response(response_data)
119
+ if response_data.code == '200' && response_data.body['result']
120
+ JSON.parse(response_data.body)['result']
121
+ elsif response_data.code == '200' && response_data.body['ticker']
122
+ JSON.parse(response_data.body)['ticker']
123
+ elsif response_data.code == '200' && response_data.body['error']
124
+ error = JSON.parse(response_data.body)
125
+ warn("Error Code: #{error['error']['code']}")
126
+ warn("Error Message: #{error['error']['message']}")
127
+ false
128
+ else
129
+ warn("Error Code: #{response_data.code}")
130
+ warn("Error Message: #{response_data.message}")
131
+ warn("check your accesskey/privatekey") if response_data.code == '401'
132
+ false
133
+ end
134
+ end
135
+
136
+ def params_string(post_data)
137
+ post_data['params'] = post_data['params'].join(',')
138
+ params_hash(post_data).collect{|k, v| "#{k}=#{v}"} * '&'
139
+ end
140
+
141
+ def params_hash(post_data)
142
+ post_data['accesskey'] = @access_key
143
+ post_data['requestmethod'] = 'post'
144
+ post_data['id'] = post_data['tonce'] unless post_data.keys.include?('id')
145
+ fields=['tonce','accesskey','requestmethod','id','method','params']
146
+ ordered_data = {}
147
+ fields.each do |field|
148
+ ordered_data[field] = post_data[field]
149
+ end
150
+ ordered_data
151
+ end
152
+ end
153
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kublai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lewis Clayton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ruby wrapper for the BTCChina Trade API. BTCChina allows you to trade
56
+ Chinese Yuan (CNY) for Bitcoins (BTC) or Bitcoins for Chinese Yuan.
57
+ email:
58
+ - mail@l.ew.is
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - kublai.gemspec
69
+ - lib/kublai.rb
70
+ - lib/kublai/version.rb
71
+ homepage: http://l.ew.is/kublai_btcchina_api_ruby/
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.1.11
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Ruby wrapper for the BTCChina Trade API.
95
+ test_files: []