etwings 0.0.1

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 946d3547b530f4f78fff00f9063c0bc2e762241d
4
+ data.tar.gz: 81accc7e57fa671bfddcb2324963997076ccf9f0
5
+ SHA512:
6
+ metadata.gz: d175367407865e4950a97c92cbbc55a9bad62529bc5f31839898ae995d7adca3c0a9c30b4628edd695bae9ee773e0e62e03e98980f26ee7afa8607760cdfb070
7
+ data.tar.gz: 2cd8e8fe9d88d2b341ad76eaa5d61188bdb6c1b7629b55208864ccf1c97035793369540a27d647e2062091b12a525c80acadef8c1462020fff939e19e2bcfa79
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *~
2
+ *.sh
3
+ pkg/*
4
+ doc/*
5
+ Gemfile.lock
6
+ *.gem
7
+ bin
8
+ docs/_build
9
+ config.rb
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Palon
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,39 @@
1
+ # Etwings
2
+
3
+ Etwings API wrapper for ruby.
4
+
5
+ ## Installation
6
+
7
+ *Note: This gem is not yet published to rubygems.*
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'etwings'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install etwings
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'etwings'
25
+
26
+ api = Etwings::API.new(:api_key => ETWINGS_KEY, :api_secret => ETWINGS_SECRET)
27
+ api.bid("btc", 30760, 0.0001)
28
+ api.ask("btc", 30320, 0.0001)
29
+
30
+ api.get_info
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/palon7/etwings/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/etwings.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'etwings/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "etwings"
8
+ spec.version = Etwings::VERSION
9
+ spec.authors = ["Palon"]
10
+ spec.email = ["palon7@gmail.com"]
11
+ spec.summary = %q{Etwings API wrapper.}
12
+ spec.description = %q{Etwings API wrapper for monacoin/bitcoin trade.}
13
+ spec.homepage = "https://github.com/palon7/etwings-ruby"
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_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~>0"
23
+ spec.add_development_dependency "rspec", "~>0"
24
+ end
@@ -0,0 +1,3 @@
1
+ # You can obtain from https://exchange.etwings.com/api_keys
2
+ ETWINGS_KEY = ""
3
+ ETWINGS_SECRET = ""
data/examples/test.rb ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require "etwings"
4
+ require "pp"
5
+ api = Etwings::API.new
6
+
7
+ puts "MONA/JPY: " + api.get_last_price("mona").to_s
8
+ puts "BTC/JPY : " + api.get_last_price("btc").to_s
9
+ p api.get_ticker("mona")
10
+ p api.get_trades("mona")
11
+ p api.get_depth("mona")
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require "etwings.rb"
4
+ require "./config.rb"
5
+ require "pp"
6
+ api = Etwings::API.new(:api_key => ETWINGS_KEY, :api_secret => ETWINGS_SECRET)
7
+ api.bid("btc", 30760, 0.0001)
8
+ api.ask("btc", 30320, 0.0001)
9
+
10
+ api.get_info
data/lib/etwings.rb ADDED
@@ -0,0 +1,221 @@
1
+ # coding: utf-8
2
+ require 'pp'
3
+ require 'json'
4
+ require 'openssl'
5
+ require 'uri'
6
+ require 'net/http'
7
+ require 'time'
8
+
9
+ require "etwings/version"
10
+ require "etwings/exceptions"
11
+
12
+ module Etwings
13
+ class API
14
+ def initialize(opt = {})
15
+ @cool_down = opt[:cool_down] || true
16
+ @cool_down_time = opt[:cool_down_time] || 2
17
+ @cert_path = opt[:cert_path] || nil
18
+ @api_key = opt[:api_key] || nil
19
+ @api_secret = opt[:api_secret] || nil
20
+ @etwings_public_url = "https://exchange.etwings.com/api/1/"
21
+ @etwings_trade_url = "https://exchange.etwings.com/tapi"
22
+ end
23
+
24
+ def set_api_key(api_key, api_secret)
25
+ @api_key = api_key
26
+ @api_secret = api_secret
27
+ end
28
+
29
+ #
30
+ # Public API
31
+ #
32
+
33
+ # Get last price of *currency_code* / *counter_currency_code*.
34
+ def get_last_price(currency_code, counter_currency_code = "jpy")
35
+ json = get_ssl(@etwings_public_url + "last_price/" + currency_code + "_" + counter_currency_code)
36
+ return json["last_price"]
37
+ end
38
+
39
+ # Get ticker of *currency_code* / *counter_currency_code*.
40
+ def get_ticker(currency_code, counter_currency_code = "jpy")
41
+ json = get_ssl(@etwings_public_url + "ticker/" + currency_code + "_" + counter_currency_code)
42
+ return json
43
+ end
44
+
45
+ # Get trades of *currency_code* / *counter_currency_code*.
46
+ def get_trades(currency_code, counter_currency_code = "jpy")
47
+ json = get_ssl(@etwings_public_url + "trades/" + currency_code + "_" + counter_currency_code)
48
+ return json
49
+ end
50
+
51
+ # Get depth of *currency_code* / *counter_currency_code*.
52
+ def get_depth(currency_code, counter_currency_code = "jpy")
53
+ json = get_ssl(@etwings_public_url + "depth/" + currency_code + "_" + counter_currency_code)
54
+ return json
55
+ end
56
+
57
+ #
58
+ # Trade API
59
+ #
60
+
61
+ # Get user infomation.
62
+ # Need api key.
63
+ def get_info
64
+ json = post_ssl(@etwings_trade_url, "get_info", {})
65
+ return json
66
+ end
67
+
68
+ # Get your trade history.
69
+ # Avalible options: from. count, from_id, end_id, order, since, end, currency_pair
70
+ # Need api key.
71
+ def get_my_trades(option = {})
72
+ json = post_ssl(@etwings_trade_url, "trade_history", option)
73
+ # Convert to datetime
74
+ json.each do|k, v|
75
+ v["datetime"] = Time.at(v["timestamp"].to_i)
76
+ end
77
+
78
+ return json
79
+ end
80
+
81
+ # Get your active orders.
82
+ # Avalible options: currency_pair
83
+ # Need api key.
84
+ def get_active_orders(option = {})
85
+ json = post_ssl(@etwings_trade_url, "active_orders", option)
86
+ # Convert to datetime
87
+ json.each do|k, v|
88
+ v["datetime"] = Time.at(v["timestamp"].to_i)
89
+ end
90
+
91
+ return json
92
+ end
93
+ # Issue trade.
94
+ # Need api key.
95
+ def trade(currency_code, price, amount, action, counter_currency_code = "jpy")
96
+ currency_pair = currency_code + "_" + counter_currency_code
97
+ json = post_ssl(@etwings_trade_url, "trade", {:currency_pair => currency_pair, :action => action, :price => price, :amount => amount})
98
+ return json
99
+ end
100
+
101
+ # Issue bid order.
102
+ # Need api key.
103
+ def bid(currency_code, price, amount, counter_currency_code = "jpy")
104
+ return trade(currency_code, price, amount, "bid", counter_currency_code)
105
+ end
106
+
107
+ # Issue ask order.
108
+ # Need api key.
109
+ def ask(currency_code, price, amount, counter_currency_code = "jpy")
110
+ return trade(currency_code, price, amount, "ask", counter_currency_code)
111
+ end
112
+
113
+ # Cancel order.
114
+ # Need api key.
115
+ def cancel(order_id)
116
+ json = post_ssl(@etwings_trade_url, "cancel_order", {:order_id => order_id})
117
+ return json
118
+ end
119
+
120
+ # Withdraw funds.
121
+ # Need api key.
122
+ def withdraw(currency_code, address, amount, option = {})
123
+ option["currency"] = currency_code
124
+ option["address"] = address
125
+ option["amount"] = amount
126
+ json = post_ssl(@etwings_trade_url, "withdraw", option)
127
+ return json
128
+ end
129
+
130
+ #
131
+ # Class private method
132
+ #
133
+
134
+ private
135
+
136
+ def check_key
137
+ if @api_key.nil? or @api_secret.nil?
138
+ raise "You need to set a API key and secret"
139
+ end
140
+ end
141
+
142
+ # Connect to address via https, and return json reponse.
143
+ def get_ssl(address)
144
+ uri = URI.parse(address)
145
+ begin
146
+ https = Net::HTTP.new(uri.host, uri.port)
147
+ https.use_ssl = true
148
+ https.open_timeout = 5
149
+ https.read_timeout = 15
150
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
151
+ https.verify_depth = 5
152
+
153
+ https.start {|w|
154
+ response = w.get(uri.request_uri)
155
+ case response
156
+ when Net::HTTPSuccess
157
+ json = JSON.parse(response.body)
158
+ raise JSONException, response.body if json == nil
159
+ raise APIErrorException, json["error"] if json.is_a?(Hash) && json.has_key?("error")
160
+ get_cool_down
161
+ return json
162
+ else
163
+ raise ConnectionFailedException, "Failed to connect to etwings."
164
+ end
165
+ }
166
+ rescue
167
+ raise
168
+ end
169
+ end
170
+
171
+ # Connect to address via https, and return json reponse.
172
+ def post_ssl(address, method, data)
173
+ check_key
174
+ uri = URI.parse(address)
175
+ data["method"] = method
176
+ data["nonce"] = get_nonce
177
+ begin
178
+ req = Net::HTTP::Post.new(uri)
179
+ req.set_form_data(data)
180
+ req["Key"] = @api_key
181
+ req["Sign"] = OpenSSL::HMAC::hexdigest(OpenSSL::Digest.new('sha512'), @api_secret, req.body)
182
+
183
+
184
+ https = Net::HTTP.new(uri.host, uri.port)
185
+ https.use_ssl = true
186
+ https.open_timeout = 5
187
+ https.read_timeout = 15
188
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
189
+ https.verify_depth = 5
190
+
191
+ https.start {|w|
192
+ response = w.request(req)
193
+ case response
194
+ when Net::HTTPSuccess
195
+ json = JSON.parse(response.body)
196
+ raise JSONException, response.body if json == nil
197
+ raise APIErrorException, json["error"] if json.is_a?(Hash) && json["success"] == 0
198
+ get_cool_down
199
+ return json["return"]
200
+ else
201
+ raise ConnectionFailedException, "Failed to connect to etwings: " + response.value
202
+ end
203
+ }
204
+ rescue
205
+ raise
206
+ end
207
+ end
208
+
209
+ def get_nonce
210
+ time = Time.now.to_f
211
+ return time.to_i
212
+ end
213
+
214
+ def get_cool_down
215
+ if @cool_down
216
+ sleep(@cool_down_time)
217
+ end
218
+ end
219
+
220
+ end
221
+ end
@@ -0,0 +1,5 @@
1
+ module Etwings
2
+ class ConnectionFailedException < StandardError; end
3
+ class APIErrorException < StandardError; end
4
+ class JSONException < StandardError; end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Etwings
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Etwings do
4
+ it 'has a version number' do
5
+ expect(Etwings::VERSION).not_to be nil
6
+ end
7
+
8
+ # it 'does something useful' do
9
+ # expect(false).to eq(true)
10
+ # end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'etwings'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: etwings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Palon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: Etwings API wrapper for monacoin/bitcoin trade.
56
+ email:
57
+ - palon7@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - etwings.gemspec
68
+ - examples/config.default.rb
69
+ - examples/test.rb
70
+ - examples/test_trade.rb
71
+ - lib/etwings.rb
72
+ - lib/etwings/exceptions.rb
73
+ - lib/etwings/version.rb
74
+ - spec/etwings_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/palon7/etwings-ruby
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Etwings API wrapper.
100
+ test_files:
101
+ - spec/etwings_spec.rb
102
+ - spec/spec_helper.rb