RubyBtce 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 208fae72b827a09af8c551f40897c4f664a52706
4
+ data.tar.gz: 763a8910cfd6a9f35b2363a1193a743df47cc1de
5
+ SHA512:
6
+ metadata.gz: 85ec9360ea21de1e9023f5d1b3f7e4d8296640377e1a5fdd02de0c4455160535dc1144f55eefd3c7aa98ac007e071727227c439347ccf785776d2591dfc40772
7
+ data.tar.gz: f3ce0bfbf049e1b21fc967c65b5133ca30c1c6fa55f3a99a4344d01db0bc9067948350a8ce0a84464b2686d1ee5b1cd56c984c387cd4959928d50fc0bcefe9ea
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in RubyBtce.gemspec
4
+ gem 'hashie'
5
+ gemspec
6
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brock Harris
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,116 @@
1
+ # RubyBtce
2
+
3
+ RubyBtce provides a simple and clean API wrapper for interfacing with the BTC-e API in a Rails app or CLI.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'RubyBtce'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install RubyBtce
18
+
19
+ Add the following file to `/config`, and name it `btce_api.yml`.
20
+
21
+ # /{Rails.root}/config/btce_api.yml
22
+
23
+ key: 'your_api_key'
24
+ secret: 'your_api_secret'
25
+
26
+ ## Usage
27
+
28
+ This gem provides class methods for all public/private API methods offered by BTC-e.
29
+ All responses will be returned in a ruby hash format, closely following the structure shown in the BTC-e API [documentation](https://btc-e.com/api/documentation).
30
+
31
+ ## Public API Examples
32
+
33
+ ### Ticker `ticker`
34
+ RubyBtce.ticker.btc_usd.last
35
+ => 555.127
36
+
37
+ ### Pair Info `pair_info`
38
+ RubyBtce.pair_info.btc_usd.fee
39
+ => 0.2
40
+
41
+ ### Depth `depth(limit)`
42
+ RubyBtce.depth(2).btc_usd.asks
43
+ => [[554.32, 0.25], [554.329, 0.034]]
44
+
45
+ ### Order Book `order_book(limit)`
46
+ RubyBtce.order_book(2).btc_usd.first.price
47
+ => 553
48
+
49
+ ## Private API
50
+
51
+ ### Account Info `account`
52
+ RubyBtce.account.funds.btc
53
+ => 0.02199302
54
+
55
+ ### New Trade `new_trade(opts={})`
56
+ This method will take up the values for `rate` and `amount` in the format of a string, integer, or float. You can pass these parameters with any number of decimal places, which will automatically be cut off (not rounded) to the maximum number of places for the specific currency.
57
+
58
+ @trade = RubyBtce.new_trade("pair" => "btc_usd", "type" => "sell", "rate"=>"600", "amount"=>"0.02")
59
+
60
+ @trade.funds.btc
61
+ => 0.00199302
62
+
63
+ @trade.order_id
64
+ => 242304103
65
+
66
+ ### Cancel Trade `cancel(opts={})`
67
+ @trade = RubyBtce.cancel("order_id"=>242304103)
68
+
69
+ @trade.funds.usd
70
+ => 50.35772684
71
+
72
+ @trade.order_id
73
+ => 242304103
74
+
75
+ ### Active Orders `orders(opts={})`
76
+ @orders = RubyBtce.orders("pair" => "btc_usd")
77
+ # or for all currency pairs: #
78
+ @orders = RubyBtce.orders
79
+
80
+ @orders.each do |id, order|
81
+ id
82
+ => 242304103
83
+
84
+ order.status
85
+ => 0
86
+ end
87
+
88
+ ### Trade History `trades(opts={})`
89
+ @trades = RubyBtce.trades("pair" => "btc_usd")
90
+
91
+ @trades.each do |id, trade|
92
+ id
93
+ => 35308202
94
+
95
+ trade.order_id
96
+ => 242304103
97
+ end
98
+
99
+ ### Transaction History `transactions(opts={})`
100
+ @transactions = RubyBtce.transactions("from_id" => "242304103", "end_id" => "242304103", "order" => "ASC")
101
+
102
+ @transactions.each do |id, transaction|
103
+ id
104
+ => 56116202
105
+
106
+ transaction.desc
107
+ => Bought 0.02 BTC from your order :order:242304103: by price 600 USD total 12 USD (-0.2%)
108
+ end
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it ( http://github.com/<my-github-username>/RubyBtce/fork )
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'RubyBtce/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "RubyBtce"
8
+ spec.version = RubyBtce::VERSION
9
+ spec.authors = ["Brock Harris"]
10
+ spec.email = ["btharris781@gmail.com"]
11
+ spec.summary = "RubyBtce provides a simple and clean API wrapper for interfacing with the BTC-e API in a Rails app or CLI."
12
+ spec.description = "Supports all API methods and currency pairs."
13
+ spec.homepage = "https://github.com/BrockHarris/RubyBtce"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'json'
22
+ spec.add_dependency 'hashie'
23
+ spec.add_dependency 'httparty'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.5"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,178 @@
1
+ require "RubyBtce/version"
2
+ require 'json'
3
+ require 'open-uri'
4
+ require 'httparty'
5
+ require 'hashie'
6
+
7
+
8
+ module RubyBtce
9
+ def self.return_error
10
+ puts "BTC-e error: you may have sent invalid parameters/credentials, or made requests to frequently."
11
+ return false
12
+ end
13
+
14
+ def self.round_off(value, places)
15
+ result = (value.to_f * (10 ** places)).floor / (10.0 ** places)
16
+ return result
17
+ end
18
+
19
+ def self.cleanup_trade_params(opts)
20
+ price = opts["rate"]
21
+ amount = opts["amount"]
22
+
23
+ pairs = {
24
+ "btc_usd" => 3,
25
+ "btc_eur" => 3,
26
+ "btc_rur" => 4,
27
+ "eur_usd" => 4,
28
+ "eur_rur" => 4,
29
+ "ftc_btc" => 4,
30
+ "nvc_btc" => 4,
31
+ "nvc_usd" => 4,
32
+ "ppc_btc" => 4,
33
+ "usd_rur" => 4,
34
+ "ppc_usd" => 4,
35
+ "ltc_rur" => 4,
36
+ "nmc_btc" => 4,
37
+ "ltc_btc" => 5,
38
+ "ltc_eur" => 6,
39
+ "ltc_usd" => 6,
40
+ "nmc_usd" => 6,
41
+ "trc_btc" => 6,
42
+ "xpm_btc" => 6
43
+ }
44
+
45
+ pairs.each do |key, val|
46
+ if opts["pair"] == key
47
+ opts["rate"] = round_off(price, val)
48
+ opts["amount"] = round_off(amount, 8)
49
+ end
50
+ end
51
+ return opts
52
+ end
53
+
54
+ def self.api(method, opts)
55
+ path = File.join(File.dirname(__FILE__), '..', 'config', 'btce_api.yml')
56
+ if File.exists?(path)
57
+ config = YAML.load_file(path)
58
+ else
59
+ config = YAML.load_file(File.join(Rails.root, "config", "btce_api.yml"))
60
+ end
61
+
62
+ key = config['key']
63
+ secret = config['secret']
64
+
65
+ params = {"method" => method, "nonce" => nonce}
66
+
67
+ unless opts.empty?
68
+ if method == "Trade"
69
+ opts = cleanup_trade_params(opts)
70
+ end
71
+ opts.each do |key, val|
72
+ params[key.to_s] = val
73
+ end
74
+ end
75
+
76
+ payload = params.collect do |key, val|
77
+ "#{key}=#{CGI::escape(val.to_s)}"
78
+ end.join('&')
79
+
80
+ signature = OpenSSL::HMAC.hexdigest('sha512', secret, payload)
81
+
82
+ Net::HTTP.start('btc-e.com', 443, :use_ssl => true) do |http|
83
+ headers = {'Sign' => signature, 'Key' => key}
84
+ response = http.post('/tapi', payload, headers).body
85
+ return JSON.parse(response)
86
+ end
87
+ end
88
+
89
+ def self.nonce
90
+ while result = Time.now.to_i and @nonce and @nonce >= result
91
+ sleep 1
92
+ end
93
+ return @nonce = result
94
+ end
95
+
96
+ def self.all_currencies
97
+ currencies = 'btc_usd-btc_rur-btc_eur-btc_cnh-btc_gbp-ltc_btc-ltc_usd-ltc_rur-ltc_eur-ltc_cnh-ltc_gbp-nmc_btc-nmc_usd-nvc_btc-nvc_usd-usd_rur-eur_usd-eur_rur-usd_cnh-gbp_usd-trc_btc-ppc_btc-ppc_usd-ftc_btc-xpm_btc'
98
+ end
99
+
100
+ def self.account
101
+ request = RubyBtce.api("getInfo", opts={})
102
+ if request['success'] == 1
103
+ return response = Hashie::Mash.new(request['return'])
104
+ else
105
+ return request['error']
106
+ end
107
+ end
108
+
109
+ def self.new_trade(opts={})
110
+ request = RubyBtce.api("Trade", opts)
111
+ if request['success'] == 1
112
+ return response = Hashie::Mash.new(request['return'])
113
+ else
114
+ return request['error']
115
+ end
116
+ end
117
+
118
+ def self.cancel(opts={})
119
+ request = RubyBtce.api("CancelOrder", opts)
120
+ if request['success'] == 1
121
+ return response = Hashie::Mash.new(request['return'])
122
+ else
123
+ return request['error']
124
+ end
125
+ end
126
+
127
+ def self.orders(opts={})
128
+ request = RubyBtce.api("ActiveOrders", opts)
129
+ if request['success'] == 1
130
+ return response = Hashie::Mash.new(request['return'])
131
+ else
132
+ return request['error']
133
+ end
134
+ end
135
+
136
+ def self.transactions(opts={})
137
+ request = RubyBtce.api("TransHistory", opts)
138
+ if request['success'] == 1
139
+ return response = Hashie::Mash.new(request['return'])
140
+ else
141
+ return request['error']
142
+ end
143
+ end
144
+
145
+ def self.trades(opts={})
146
+ request = RubyBtce.api("TradeHistory", opts)
147
+ if request['success'] == 1
148
+ return response = Hashie::Mash.new(request['return'])
149
+ else
150
+ return request['error']
151
+ end
152
+ end
153
+
154
+ def self.ticker
155
+ request = JSON.parse(open("https://btc-e.com/api/3/ticker/#{self.all_currencies}").read)
156
+ if request['success'] == 0
157
+ self.return_error
158
+ else
159
+ return response = Hashie::Mash.new(request)
160
+ end
161
+ end
162
+
163
+ def self.pair_info
164
+ request = JSON.parse(open("https://btc-e.com/api/3/info").read)
165
+ return response = Hashie::Mash.new(request['pairs'])
166
+ end
167
+
168
+ def self.depth(limit)
169
+ request = JSON.parse(open("https://btc-e.com/api/3/depth/#{self.all_currencies}?limit=#{limit}").read)
170
+ return response = Hashie::Mash.new(request)
171
+ end
172
+
173
+ def self.order_book(limit)
174
+ request = JSON.parse(open("https://btc-e.com/api/3/trades/#{self.all_currencies}?limit=#{limit}").read)
175
+ return response = Hashie::Mash.new(request)
176
+ end
177
+ end
178
+ #require 'RubyBtce/operations/balances'
@@ -0,0 +1,3 @@
1
+ module RubyBtce
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: RubyBtce
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brock Harris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-30 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: '0'
20
+ type: :runtime
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: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Supports all API methods and currency pairs.
84
+ email:
85
+ - btharris781@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - RubyBtce.gemspec
96
+ - lib/RubyBtce.rb
97
+ - lib/RubyBtce/version.rb
98
+ homepage: https://github.com/BrockHarris/RubyBtce
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.2.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: RubyBtce provides a simple and clean API wrapper for interfacing with the
122
+ BTC-e API in a Rails app or CLI.
123
+ test_files: []