bter-ruby 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: 35aefbbd778c55cb32b44d98afb9c570a7c7a79c
4
+ data.tar.gz: 6590117558cca8af1081bbcd28d12dcd921c128e
5
+ SHA512:
6
+ metadata.gz: f5a9080d6ac0d0d10aba144347e5e7c81a72e8f101dbfff3761b9ddfb900e5c181a193a84a006387acaad8ec7e0ab0a2ee9e7a7811a565b0c29f27e0975db848
7
+ data.tar.gz: 2a2f58facc364d190fceef3d379bca7f9ecb6bc27018cae9329cf1399e2682219bda787daa99bad3171e8c33d026d2e65fd3d5b9831c23de161c9b97e62a7a33
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem 'json'
3
+ gem 'typhoeus'
4
+ gem 'logger'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) <2014> <Zisis Maras>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ Ruby api for the bter.com cryptocurrency exchange
2
+
3
+ Install with
4
+ ```ruby
5
+ gem install bter-ruby
6
+ ```
7
+ then run
8
+ ```ruby
9
+ bundle install
10
+ ```
11
+ and finally require in your project
12
+ ```ruby
13
+ require 'bter'
14
+ ```
15
+
16
+ Check the two examples in the examples folder on how to use it.
data/lib/bter.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ require 'json'
3
+ require 'typhoeus'
4
+ require 'openssl'
5
+
6
+ require_relative 'bter/public.rb'
7
+ require_relative 'bter/trading.rb'
8
+ require_relative 'bter/request_logger.rb'
9
+
10
+ module Bter
11
+
12
+ end
@@ -0,0 +1,32 @@
1
+
2
+ module Bter
3
+ class Public
4
+
5
+ def public_request(method, pair)
6
+ request = Typhoeus::Request.new("https://bter.com/api/1/#{method}/#{pair}")
7
+ Request_logger.new.error_log(request)
8
+ hydra = Typhoeus::Hydra.hydra
9
+ hydra.queue(request)
10
+ hydra.run
11
+ response = request.response
12
+ Request_logger.new.info_log(response.code, response.total_time, response.headers_hash)
13
+ response.body
14
+ end
15
+
16
+ def ticker(pair)
17
+ query = public_request "ticker", pair
18
+ info = JSON.parse query, {:symbolize_names => true}
19
+ end
20
+
21
+ def depth(pair)
22
+ query = public_request "depth", pair
23
+ info = JSON.parse query, {:symbolize_names => true}
24
+ end
25
+
26
+ def trades(pair)
27
+ query = public_request "trade", pair
28
+ info = JSON.parse query, {:symbolize_names => true}
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ require 'logger'
2
+
3
+ module Bter
4
+ class Request_logger
5
+
6
+ def error_log(request)
7
+ logger = Logger.new('logfile.log')
8
+ logger.level = Logger::INFO
9
+
10
+ request.on_complete do |response|
11
+ if response.success?
12
+ logger.info("Success")
13
+ true
14
+ elsif response.timed_out?
15
+ logger.error("Got a time out")
16
+ false
17
+ elsif response.code == 0
18
+ logger.error(response.return_message)
19
+ false
20
+ else
21
+ logger.error("HTTP request failed: " + response.code.to_s)
22
+ false
23
+ end
24
+ end
25
+ end
26
+
27
+ def info_log(code, total_time, header_hash)
28
+ logger = Logger.new('logfile.log')
29
+ logger.level = Logger::INFO
30
+ logger.info("response code : #{code}")
31
+ logger.info("Total_time: #{total_time}")
32
+ logger.info("Header_hash: #{header_hash}")
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,79 @@
1
+
2
+ module Bter
3
+ class Trade
4
+
5
+ attr_accessor :key, :secret
6
+
7
+ def trade_request(method, params=nil)
8
+ if params.nil?
9
+ @params = {:method => method}
10
+ else
11
+ @params = {:method => method}
12
+ params.each do |param|
13
+ @params.merge!(param)
14
+ end
15
+ end
16
+ request = Typhoeus::Request.new(
17
+ "https://bter.com/api/1/private/#{method}",
18
+ method: :post,
19
+ body: @params,
20
+ headers: { Key: @key, Sign: sign }
21
+ )
22
+ Request_logger.new.error_log(request)
23
+ hydra = Typhoeus::Hydra.hydra
24
+ hydra.queue(request)
25
+ hydra.run
26
+ response = request.response
27
+ Request_logger.new.info_log(response.code, response.total_time, response.headers_hash)
28
+ response.body
29
+ end
30
+
31
+ def sign
32
+ hmac = OpenSSL::HMAC.new(@secret,OpenSSL::Digest::SHA512.new)
33
+ @params = @params.collect {|k,v| "#{k}=#{v}"}.join('&')
34
+ signed = hmac.update @params
35
+ end
36
+
37
+ def get_info
38
+ query = trade_request "getfunds"
39
+ info = JSON.parse query, {:symbolize_names => true}
40
+ end
41
+
42
+ def active_orders
43
+ query = trade_request "orderlist"
44
+ info = JSON.parse query, {:symbolize_names => true}
45
+ end
46
+
47
+ def trade(*params)
48
+ query = trade_request "placeorder", params
49
+ info = JSON.parse query, {:symbolize_names => true}
50
+ end
51
+
52
+ #abstract the trade to buy and sell
53
+ def buy(pair, amount)
54
+ pair_rate = get_rate(pair)
55
+ trade({:pair => pair, :type => "BUY", :rate => pair_rate, :amount => amount})
56
+ end
57
+
58
+ def sell(pair, amount)
59
+ pair_rate = get_rate(pair)
60
+ trade({:pair => pair, :type => "SELL", :rate => pair_rate, :amount => amount})
61
+ end
62
+
63
+ def order_status(order_id)
64
+ query = trade_request "getorder", [{:id => order_id}]
65
+ info = JSON.parse query, {:symbolize_names => true}
66
+ end
67
+
68
+ def cancel_order(order_id)
69
+ query = trade_request "cancelorder", [{:id => order_id}]
70
+ info = JSON.parse query, {:symbolize_names => true}
71
+ end
72
+
73
+ def get_rate(pair)
74
+ ticker(pair).values_at(:last).flatten
75
+ end
76
+
77
+ end
78
+ end
79
+
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bter-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zisis Maras
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby api for the bter.com cryptocurrency exchange
14
+ email: zisismaras@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/bter.rb
20
+ - lib/bter/public.rb
21
+ - lib/bter/trading.rb
22
+ - lib/bter/request_logger.rb
23
+ - LICENSE
24
+ - README.md
25
+ - Gemfile
26
+ homepage: https://github.com/zisismaras/bter-ruby
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: bter.com ruby api
50
+ test_files: []