chinashop 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Njk0YWIxYzc3YzhkYjNjZjdlMTMwOTFhNTZmMjc3OWNiMjg5NzVhYg==
5
+ data.tar.gz: !binary |-
6
+ YWJiMWNiZWFhZWY1YzliNjJjODMxZjQ3NzVmMGJkNzE4MjIwMzUzMw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YmExYjc2MzhkYzFlYzcxYmRmYWYyNzI5NDk1NDllY2IwN2QwZjhmMTgxODVm
10
+ YjQ5NjMzNjFhYWU1YTE5OTY1OTZhYjE3MmU3NjRkYWRjNzJkMTMxYWFiYTRj
11
+ OWNkYmI3Mjg1NTFhYjEzNDlkMmZlZjk3ODQ2N2YzODFmYmViYTk=
12
+ data.tar.gz: !binary |-
13
+ ODgwOTNhNzNkYmQxMDY1OGM0NWIyNzY3Mjc5NTA2ZjBhNGVmMjZlMDBjYzRi
14
+ OWFkZDgzNzgyZDE1MzYwODY4Nzg2YTk2NTRkMDE0OWQ3NDJkODFjZGI0NmEy
15
+ YTczZjUzMzRhOGU5NGI3ZjA3M2EwOGY4MzA0NzM2ZTMwZmVjNDg=
data/README.md ADDED
@@ -0,0 +1,165 @@
1
+ The start of a btcchina library. The btcchina api is currently very basic so don't expect any magic.
2
+
3
+ Usage
4
+ --------
5
+ ````ruby
6
+ require 'chinashop'
7
+
8
+ ChinaShop.configure do |config|
9
+ config.key = 'your-key-here'
10
+ config.secret = 'your-secret-here'
11
+ end
12
+
13
+ puts ChinaShop.ticker.high
14
+ puts ChinaShop.account.balance.btc
15
+ ````
16
+
17
+ ## Configuration
18
+ Apply a key and secret to use the account and trading features.
19
+
20
+ ````ruby
21
+ ChinaShop.configure do |config|
22
+ config.key = 'your-key-here'
23
+ config.secret = 'your-secret-here'
24
+ end
25
+ ````
26
+
27
+ ## Ticker
28
+
29
+ Returns the ticker information. Accessed via `ChinaShop.ticker`. The following methods are available:
30
+
31
+ * `all`
32
+ * `high`
33
+ * `low`
34
+ * `buy`
35
+ * `sell`
36
+ * `last`
37
+ * `vol`
38
+
39
+ ````ruby
40
+ puts ChinaShop.ticker.high
41
+ ````
42
+
43
+ ## Account
44
+
45
+ Returns the user account information. Configuration is required. Accessed via `ChinaShop.account`. The following methods are available:
46
+ * `all`
47
+ * `username`
48
+ * `balance`
49
+ * `frozen`
50
+ * `deposit_address`
51
+
52
+ ````ruby
53
+ puts ChinaShop.account.balance.btc
54
+ ````
55
+ ## Market Depth
56
+
57
+ Returns the Market Depth. Accessed via `ChinaShop.market_depth`. The following methods are available:
58
+ * `all`
59
+ * `bids`
60
+ * `asks`
61
+
62
+ ````ruby
63
+ ChinaShop.market_depth.bids
64
+ ````
65
+
66
+ ## Buy
67
+
68
+ Places a Bid order for the btcchina exchange. Accessed via `ChinaShop.buy`. The following methods are available:
69
+ * `all`
70
+ * `result`
71
+
72
+ ````ruby
73
+ b = ChinaShop.buy(:price => 6030, :amount => 0.009)
74
+ puts b.result
75
+ ````
76
+
77
+ ## Sell
78
+
79
+ Places a Ask order for the btcchina exchange. Accessed via `ChinaShop.sell`. The following methods are available:
80
+ * `all`
81
+ * `result`
82
+
83
+ ````ruby
84
+ s = ChinaShop.sell(:price => 6030, :amount => 0.009)
85
+ puts s.result
86
+ ````
87
+
88
+ ## Orders
89
+
90
+ Lists all the open orders. Accessed via `ChinaShop.orders`. The following methods are available:
91
+ * `all`
92
+
93
+ ````ruby
94
+ puts ChinaShop.orders.all
95
+ ````
96
+
97
+ ## Order
98
+
99
+ Get a single order. Accessed via `ChinaShop.order`. The following methods are available:
100
+ * `all`
101
+ * `id`
102
+ * `type`
103
+ * `price`
104
+ * `currency`
105
+ * `amount`
106
+ * `amount_original`
107
+ * `date`
108
+ * `status`
109
+
110
+ ````ruby
111
+ puts ChinaShop.order(823841).status
112
+ ````
113
+
114
+ ## Transactions
115
+
116
+ Lists recent transactions. Accessed via `ChinaShop.transactions`. The following methods are available:
117
+ * `all`
118
+
119
+ ````ruby
120
+ puts ChinaShop.transactions.all
121
+ ````
122
+
123
+ ## Deposits
124
+
125
+ Lists recent deposits. Accessed via `ChinaShop.deposits`. The following methods are available:
126
+ * `all`
127
+
128
+ ````ruby
129
+ puts ChinaShop.deposits.all
130
+ ````
131
+
132
+ ## Trades
133
+
134
+ Returns recent trades. Accessed via `ChinaShop.trades`.
135
+
136
+ ````ruby
137
+ puts ChinaShop.trades
138
+ ````
139
+
140
+ ## Order Book
141
+
142
+ Returns the current order book. Accessed via `ChinaShop.order_book`.
143
+
144
+ ````ruby
145
+ puts ChinaShop.order_book
146
+ ````
147
+
148
+
149
+ License and Author
150
+ ==================
151
+ Author:: Bryan Brandau <agent462@gmail.com>
152
+
153
+ Copyright:: 2013, Bryan Brandau
154
+
155
+ Licensed under the Apache License, Version 2.0 (the "License");
156
+ you may not use this file except in compliance with the License.
157
+ You may obtain a copy of the License at
158
+
159
+ http://www.apache.org/licenses/LICENSE-2.0
160
+
161
+ Unless required by applicable law or agreed to in writing, software
162
+ distributed under the License is distributed on an "AS IS" BASIS,
163
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
164
+ See the License for the specific language governing permissions and
165
+ limitations under the License.
data/chinashop.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'chinashop', 'version')
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'chinashop'
5
+ s.version = ChinaShop::VERSION
6
+ s.platform = Gem::Platform::RUBY
7
+ s.summary = "A library for the btcchina exchange"
8
+ s.description = "A library for the btcchina exchange"
9
+ s.authors = ["Bryan Brandau"]
10
+ s.email = 'agent462@gmail.com'
11
+ s.has_rdoc = false
12
+ s.licenses = ['APACHE']
13
+ s.homepage ='http://github.com/agent462/chinashop'
14
+
15
+ s.add_development_dependency('rspec')
16
+ s.add_development_dependency('rubocop')
17
+
18
+ s.files = Dir.glob('{lib}/**/*') + %w[chinashop.gemspec README.md]
19
+ s.require_paths = ['lib']
20
+ end
@@ -0,0 +1,16 @@
1
+ require 'ostruct'
2
+
3
+ module ChinaShop
4
+ class Account
5
+ attr_accessor :username, :all, :balance, :frozen, :deposit_address
6
+
7
+ def initialize(account = {})
8
+ self.all = account['result']
9
+ self.username = account['result']['profile']['username']
10
+ self.balance = OpenStruct.new(:btc => account['result']['balance']['btc']['amount'], :cny => account['result']['balance']['cny']['amount'])
11
+ self.frozen = OpenStruct.new(:btc => account['result']['frozen']['btc']['amount'], :cny => account['result']['frozen']['cny']['amount'])
12
+ self.deposit_address = account['result']['profile']['btc_deposit_address']
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,48 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'json'
4
+ require 'net/https'
5
+ require 'uri'
6
+
7
+ module ChinaShop
8
+ module Api
9
+ attr_reader :api_uri
10
+
11
+ def initialize
12
+ @api_uri = 'https://vip.btcchina.com/api_trade_v1.php'
13
+ end
14
+
15
+ def post(opts = {})
16
+ method = opts[:method]
17
+ params = opts[:params] || []
18
+ tonce = (Time.now.to_f * 1000000).to_i
19
+ query_string = "tonce=#{tonce}&accesskey=#{key}&requestmethod=post&id=#{tonce}&method=#{method}&params=#{params.join(',')}"
20
+ hash = OpenSSL::HMAC.hexdigest('sha1', secret, query_string)
21
+ auth = Base64.encode64("#{key}:#{hash}").gsub("\n", '')
22
+ uri = URI.parse(api_uri)
23
+ http = Net::HTTP.new(uri.host, uri.port)
24
+ http.read_timeout = 15
25
+ http.open_timeout = 5
26
+ http.use_ssl = true
27
+ headers = {
28
+ 'Content-Type' => 'application/json-rpc',
29
+ 'Authorization' => "Basic #{auth}",
30
+ 'Json-Rpc-Tonce' => "#{tonce}"
31
+ }
32
+ req = Net::HTTP::Post.new(api_uri, initheader = headers)
33
+ req.body = JSON.generate('method' => method, 'params' => params, 'id' => tonce)
34
+ JSON.parse(http.request(req).body)
35
+ end
36
+
37
+ def get(uri)
38
+ uri = URI.parse(uri)
39
+ http = Net::HTTP.new(uri.host, uri.port)
40
+ http.read_timeout = 15
41
+ http.open_timeout = 5
42
+ http.use_ssl = true
43
+ req = Net::HTTP::Get.new(uri.request_uri)
44
+ JSON.parse(http.request(req).body)
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,11 @@
1
+ module ChinaShop
2
+ class Buy
3
+ attr_accessor :all, :result
4
+
5
+ def initialize(buy = {})
6
+ self.all = buy
7
+ self.result = buy['result']
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module ChinaShop
2
+ class CancelOrder
3
+ attr_accessor :all
4
+
5
+ def initialize(cancel = {})
6
+ self.all = cancel['result']
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,67 @@
1
+ require 'chinashop/config'
2
+ require 'chinashop/api'
3
+ require 'chinashop/account'
4
+ require 'chinashop/ticker'
5
+ require 'chinashop/orders'
6
+ require 'chinashop/order'
7
+ require 'chinashop/cancel_order'
8
+ require 'chinashop/buy'
9
+ require 'chinashop/sell'
10
+ require 'chinashop/deposits'
11
+ require 'chinashop/transactions'
12
+ require 'chinashop/market_depth'
13
+
14
+ module ChinaShop
15
+ class Client
16
+ include ChinaShop::Config
17
+ include ChinaShop::Api
18
+
19
+ def account
20
+ Account.new(post(:method => 'getAccountInfo'))
21
+ end
22
+
23
+ def orders
24
+ Orders.new(post(:method => 'getOrders'))
25
+ end
26
+
27
+ def order(id)
28
+ Order.new(post(:method => 'getOrder', :params => [id]))
29
+ end
30
+
31
+ def market_depth
32
+ MarketDepth.new(post(:method => 'getMarketDepth2'))
33
+ end
34
+
35
+ def buy(h = {})
36
+ Buy.new(post(:method => 'buyOrder', :params => ["#{h[:price].to_f.round(5)}", "#{h[:amount].to_f.round(8)}"]))
37
+ end
38
+
39
+ def sell(h = {})
40
+ Sell.new(post(:method => 'sellOrder', :params => ["#{h[:price].to_f.round(5)}", "#{h[:amount].to_f.round(8)}"]))
41
+ end
42
+
43
+ def cancel_order(id)
44
+ CancelOrder.new(post(:method => 'cancelOrder', :params => [id]))
45
+ end
46
+
47
+ def transactions
48
+ Transactions.new(post(:method => 'getTransactions'))
49
+ end
50
+
51
+ def deposits
52
+ Deposits.new(post(:method => 'getDeposits', :params => ['BTC']))
53
+ end
54
+
55
+ def ticker
56
+ Ticker.new(get('https://data.btcchina.com/data/ticker'))
57
+ end
58
+
59
+ def order_book
60
+ get('https://data.btcchina.com/data/orderbook')
61
+ end
62
+
63
+ def trades
64
+ get('https://data.btcchina.com/data/trades')
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,10 @@
1
+ module ChinaShop
2
+ module Config
3
+ attr_accessor :key, :secret
4
+
5
+ def configure
6
+ yield self
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module ChinaShop
2
+ class Deposits
3
+ attr_accessor :all
4
+
5
+ def initialize(deposits = {})
6
+ self.all = deposits['result']['deposit']
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module ChinaShop
2
+ class MarketDepth
3
+ attr_accessor :all, :bids, :asks
4
+
5
+ def initialize(depth = {})
6
+ self.all = depth['result']['market_depth']
7
+ self.bids = depth['result']['market_depth']['bid']
8
+ self.asks = depth['result']['market_depth']['ask']
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ module ChinaShop
2
+ class Order
3
+ attr_accessor :all, :id, :type, :price, :currency, :amount, :amount_original, :date, :status
4
+
5
+ def initialize(order = {})
6
+ self.all = order['result']['order']
7
+ self.id = order['result']['order']['id']
8
+ self.type = order['result']['order']['type']
9
+ self.price = order['result']['order']['price']
10
+ self.currency = order['result']['order']['currency']
11
+ self.amount = order['result']['order']['amount']
12
+ self.amount_original = order['result']['order']['amount_original']
13
+ self.date = order['result']['order']['date']
14
+ self.status = order['result']['order']['status']
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module ChinaShop
2
+ class OrderBook
3
+ attr_accessor :all
4
+
5
+ def initialize(order_book = {})
6
+ self.all = order_book
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module ChinaShop
2
+ class Orders
3
+ attr_accessor :all
4
+
5
+ def initialize(orders = {})
6
+ self.all = orders['result']['order']
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module ChinaShop
2
+ class Sell
3
+ attr_accessor :all, :result
4
+
5
+ def initialize(buy = {})
6
+ self.all = buy
7
+ self.result = buy['result']
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module ChinaShop
2
+ class Ticker
3
+ attr_accessor :all, :high, :low, :buy, :sell, :last, :vol
4
+
5
+ def initialize(ticker = {})
6
+ self.all = ticker['ticker']
7
+ self.high = ticker['ticker']['high']
8
+ self.low = ticker['ticker']['low']
9
+ self.buy = ticker['ticker']['buy']
10
+ self.sell = ticker['ticker']['sell']
11
+ self.last = ticker['ticker']['last']
12
+ self.vol = ticker['ticker']['vol']
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ module ChinaShop
2
+ class Transactions
3
+ attr_accessor :all
4
+
5
+ def initialize(trans = {})
6
+ self.all = trans['result']['transaction']
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module ChinaShop
2
+ VERSION = '0.1.0'
3
+ end
data/lib/chinashop.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'chinashop/client'
2
+
3
+ module ChinaShop
4
+ class << self
5
+
6
+ def new
7
+ @client ||= ChinaShop::Client.new
8
+ end
9
+
10
+ def method_missing(method, *args, &block)
11
+ return super unless new.respond_to?(method)
12
+ new.send(method, *args, &block)
13
+ end
14
+
15
+ def respond_to?(method, include_private = false)
16
+ new.respond_to?(method, include_private) || super(method, include_private)
17
+ end
18
+
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinashop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Brandau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A library for the btcchina exchange
42
+ email: agent462@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/chinashop/account.rb
48
+ - lib/chinashop/api.rb
49
+ - lib/chinashop/buy.rb
50
+ - lib/chinashop/cancel_order.rb
51
+ - lib/chinashop/client.rb
52
+ - lib/chinashop/config.rb
53
+ - lib/chinashop/deposits.rb
54
+ - lib/chinashop/market_depth.rb
55
+ - lib/chinashop/order.rb
56
+ - lib/chinashop/order_book.rb
57
+ - lib/chinashop/orders.rb
58
+ - lib/chinashop/sell.rb
59
+ - lib/chinashop/ticker.rb
60
+ - lib/chinashop/transactions.rb
61
+ - lib/chinashop/version.rb
62
+ - lib/chinashop.rb
63
+ - chinashop.gemspec
64
+ - README.md
65
+ homepage: http://github.com/agent462/chinashop
66
+ licenses:
67
+ - APACHE
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.0.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A library for the btcchina exchange
89
+ test_files: []