btce 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/btce.rb +178 -0
  2. metadata +45 -0
@@ -0,0 +1,178 @@
1
+ # Copyright (c) 2013, Christopher Mark Gore,
2
+ # Soli Deo Gloria,
3
+ # All rights reserved.
4
+ #
5
+ # 8729 Lower Marine Road, Saint Jacob, Illinois 62281 USA.
6
+ # Web: http://cgore.com
7
+ # Email: cgore@cgore.com
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are met:
11
+ #
12
+ # * Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ #
15
+ # * Redistributions in binary form must reproduce the above copyright
16
+ # notice, this list of conditions and the following disclaimer in the
17
+ # documentation and/or other materials provided with the distribution.
18
+ #
19
+ # * Neither the name of Christopher Mark Gore nor the names of other
20
+ # contributors may be used to endorse or promote products derived from
21
+ # this software without specific prior written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ # POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ require 'json'
36
+ require 'net/http'
37
+ require 'net/https'
38
+ require 'openssl'
39
+ require 'uri'
40
+ require 'yaml'
41
+
42
+ class String
43
+ def camelcase_to_snakecase
44
+ self.gsub(/::/, '/')
45
+ .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
46
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
47
+ .tr("-", "_")
48
+ .downcase
49
+ end
50
+ end
51
+
52
+ module Btce
53
+ class API
54
+ BTCE_DOMAIN = "btc-e.com"
55
+ CURRENCIES = %w(btc
56
+ usd
57
+ rur
58
+ ltc
59
+ nmc
60
+ eur
61
+ nvc)
62
+ CURRENCY_PAIRS = %w(btc_usd
63
+ btc_rur
64
+ ltc_btc
65
+ ltc_usd
66
+ ltc_rur
67
+ nmc_btc
68
+ usd_rur
69
+ eur_usd
70
+ nvc_btc)
71
+ MAX_DIGITS = {
72
+ "btc_usd" => 3,
73
+ "btc_rur" => 4,
74
+ "ltc_btc" => 5,
75
+ "ltc_usd" => 6,
76
+ "ltc_rur" => 4,
77
+ "nmc_btc" => 4,
78
+ "usd_rur" => 4,
79
+ "eur_usd" => 4,
80
+ "nvc_btc" => 4
81
+ }
82
+ API_KEY = YAML::load(File.open('btce-api-key.yml'))
83
+
84
+
85
+ class << self
86
+ def get_https(url,params=nil,sign=nil)
87
+ raise ArgumentError if not url.is_a? String
88
+ uri = URI.parse url
89
+ http = Net::HTTP.new uri.host, uri.port
90
+ http.use_ssl = true
91
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
92
+ #if sending params then we want a post request(for authentication)
93
+ if(params==nil)
94
+ request = Net::HTTP::Get.new uri.request_uri
95
+ else
96
+ request = Net::HTTP::Post.new uri.request_uri
97
+ request.add_field("Key",API::API_KEY['key'])
98
+ request.add_field("Sign",sign)
99
+ request.set_form_data(params)
100
+
101
+ end
102
+ response = http.request request
103
+ response.body
104
+ end
105
+
106
+ def get_json(url,params=nil,sign=nil)
107
+ JSON.load get_https url, params, sign
108
+ end
109
+ end
110
+ end
111
+
112
+ class PublicAPI < API
113
+ OPERATIONS = %w(fee ticker trades depth)
114
+
115
+ class << self
116
+
117
+ def get_pair_operation_json(pair, operation)
118
+ raise ArgumentError if not API::CURRENCY_PAIRS.include? pair
119
+ raise ArgumentError if not OPERATIONS.include? operation
120
+ get_json "https://#{API::BTCE_DOMAIN}/api/2/#{pair}/#{operation}"
121
+ end
122
+
123
+ OPERATIONS.each do |operation|
124
+ class_eval %{
125
+ def get_pair_#{operation}_json(pair)
126
+ get_pair_operation_json pair, "#{operation}"
127
+ end
128
+ }
129
+
130
+ API::CURRENCY_PAIRS.each do |pair|
131
+ class_eval %{
132
+ def get_#{pair}_#{operation}_json
133
+ get_pair_#{operation}_json "#{pair}"
134
+ end
135
+ }
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ class TradeAPI < API
142
+ OPERATIONS = %w(getInfo TransHistory TradeHistory OrderList Trade CancelOrder)
143
+
144
+ class << self
145
+ def sign(params)
146
+ #digest needs to be created
147
+ hmac = OpenSSL::HMAC.new(API::API_KEY['secret'], OpenSSL::Digest::SHA512.new)
148
+ params = params.collect{|k,v| "#{k}=#{v}"}.join('&')
149
+ signed = hmac.update(params)
150
+ end
151
+
152
+ def trade_api_call(method, extra)
153
+ params = {"method"=>method, "nonce"=>nonce}
154
+ if !extra.empty?
155
+
156
+ extra.each do |a|
157
+ params["#{a.to_s}"] = a
158
+ end
159
+ end
160
+ puts params
161
+ signed = sign(params)
162
+ get_json "https://#{API::BTCE_DOMAIN}/tapi", params, signed
163
+ end
164
+
165
+ def nonce
166
+ Time.now.to_i
167
+ end
168
+
169
+ OPERATIONS.each do |operation|
170
+ class_eval %{
171
+ def #{operation.camelcase_to_snakecase} extra={}
172
+ trade_api_call "#{operation}", extra
173
+ end
174
+ }
175
+ end
176
+ end
177
+ end
178
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: btce
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Mark Gore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple library to interface with the API for btc-e.com in Ruby.
15
+ email: cgore@cgore.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/btce.rb
21
+ homepage: https://github.com/cgore/ruby-btce
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.23
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: A simple library to interface with the API for btc-e.com in Ruby.
45
+ test_files: []