zaif 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8ab2e62308d3eb82e9d8fb5308b30eb5364fab8
4
+ data.tar.gz: 7b61c28e4690ead385ba6bfbb8b5be5b9fa4e3d8
5
+ SHA512:
6
+ metadata.gz: 3cfb61825e2ba6c3a45daf29d579cfe226cb21929a800be074b9b510d955412e585ae961e4a8557eadb2e0f0c54b9763eafab82728e11844b87bc3ebe88beb97
7
+ data.tar.gz: b75d67953b4d80baf451b4c2eecd2ddcf24d215f7b56839c6a85477a7fa941087c585e8482df7ecd0f8405a752d9d812870b09e8e2c9a72777ad3952c199da47
@@ -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
@@ -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.
@@ -0,0 +1,37 @@
1
+ # Zaif
2
+
3
+ Zaif API wrapper for ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zaif'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zaif
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'zaif'
23
+
24
+ api = zaif::API.new(:api_key => ZAIF_KEY, :api_secret => ZAIF_SECRET)
25
+ api.bid("btc", 30760, 0.0001)
26
+ api.ask("btc", 30320, 0.0001)
27
+
28
+ api.get_info
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/palon7/zaif-ruby/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
@@ -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
+
@@ -0,0 +1,3 @@
1
+ # You can obtain from https://exchange.etwings.com/api_keys
2
+ ZAIF_KEY = ""
3
+ ZAIF_SECRET = ""
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require "zaif"
4
+ require "pp"
5
+ api = Zaif::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,9 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require "zaif"
4
+ require "./config.rb"
5
+ require "pp"
6
+ api = Zaif::API.new(:api_key => ZAIF_KEY, :api_secret => ZAIF_SECRET)
7
+ api.bid("mona", 23, 1)
8
+
9
+ api.get_info
@@ -0,0 +1,231 @@
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 "zaif/version"
10
+ require "zaif/exceptions"
11
+
12
+ module Zaif
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
+ @zaif_public_url = "https://api.zaif.jp/api/1/"
21
+ @zaif_trade_url = "https://api.zaif.jp/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
+ # @param [String] currency_code Base currency code
35
+ # @param [String] counter_currency_code Counter currency code
36
+ def get_last_price(currency_code, counter_currency_code = "jpy")
37
+ json = get_ssl(@zaif_public_url + "last_price/" + currency_code + "_" + counter_currency_code)
38
+ return json["last_price"]
39
+ end
40
+
41
+ # Get ticker of *currency_code* / *counter_currency_code*.
42
+ # @param [String] currency_code Base currency code
43
+ # @param [String] counter_currency_code Counter currency code
44
+ def get_ticker(currency_code, counter_currency_code = "jpy")
45
+ json = get_ssl(@zaif_public_url + "ticker/" + currency_code + "_" + counter_currency_code)
46
+ return json
47
+ end
48
+
49
+ # Get trades of *currency_code* / *counter_currency_code*.
50
+ # @param [String] currency_code Base currency code
51
+ # @param [String] counter_currency_code Counter currency code
52
+ def get_trades(currency_code, counter_currency_code = "jpy")
53
+ json = get_ssl(@zaif_public_url + "trades/" + currency_code + "_" + counter_currency_code)
54
+ return json
55
+ end
56
+
57
+ # Get depth of *currency_code* / *counter_currency_code*.
58
+ # @param [String] currency_code Base currency code
59
+ # @param [String] counter_currency_code Counter currency code
60
+ def get_depth(currency_code, counter_currency_code = "jpy")
61
+ json = get_ssl(@zaif_public_url + "depth/" + currency_code + "_" + counter_currency_code)
62
+ return json
63
+ end
64
+
65
+ #
66
+ # Trade API
67
+ #
68
+
69
+ # Get user infomation.
70
+ # Need api key.
71
+ # @return [Hash] Infomation of user.
72
+ def get_info
73
+ json = post_ssl(@zaif_trade_url, "get_info", {})
74
+ return json
75
+ end
76
+
77
+ # Get your trade history.
78
+ # Avalible options: from. count, from_id, end_id, order, since, end, currency_pair
79
+ # Need api key.
80
+ # @param [Hash]
81
+ def get_my_trades(option = {})
82
+ json = post_ssl(@zaif_trade_url, "trade_history", option)
83
+ # Convert to datetime
84
+ json.each do|k, v|
85
+ v["datetime"] = Time.at(v["timestamp"].to_i)
86
+ end
87
+
88
+ return json
89
+ end
90
+
91
+ # Get your active orders.
92
+ # Avalible options: currency_pair
93
+ # Need api key.
94
+ def get_active_orders(option = {})
95
+ json = post_ssl(@zaif_trade_url, "active_orders", option)
96
+ # Convert to datetime
97
+ json.each do|k, v|
98
+ v["datetime"] = Time.at(v["timestamp"].to_i)
99
+ end
100
+
101
+ return json
102
+ end
103
+ # Issue trade.
104
+ # Need api key.
105
+ def trade(currency_code, price, amount, action, counter_currency_code = "jpy")
106
+ currency_pair = currency_code + "_" + counter_currency_code
107
+ json = post_ssl(@zaif_trade_url, "trade", {:currency_pair => currency_pair, :action => action, :price => price, :amount => amount})
108
+ return json
109
+ end
110
+
111
+ # Issue bid order.
112
+ # Need api key.
113
+ def bid(currency_code, price, amount, counter_currency_code = "jpy")
114
+ return trade(currency_code, price, amount, "bid", counter_currency_code)
115
+ end
116
+
117
+ # Issue ask order.
118
+ # Need api key.
119
+ def ask(currency_code, price, amount, counter_currency_code = "jpy")
120
+ return trade(currency_code, price, amount, "ask", counter_currency_code)
121
+ end
122
+
123
+ # Cancel order.
124
+ # Need api key.
125
+ def cancel(order_id)
126
+ json = post_ssl(@zaif_trade_url, "cancel_order", {:order_id => order_id})
127
+ return json
128
+ end
129
+
130
+ # Withdraw funds.
131
+ # Need api key.
132
+ def withdraw(currency_code, address, amount, option = {})
133
+ option["currency"] = currency_code
134
+ option["address"] = address
135
+ option["amount"] = amount
136
+ json = post_ssl(@zaif_trade_url, "withdraw", option)
137
+ return json
138
+ end
139
+
140
+ #
141
+ # Class private method
142
+ #
143
+
144
+ private
145
+
146
+ def check_key
147
+ if @api_key.nil? or @api_secret.nil?
148
+ raise "You need to set a API key and secret"
149
+ end
150
+ end
151
+
152
+ # Connect to address via https, and return json reponse.
153
+ def get_ssl(address)
154
+ uri = URI.parse(address)
155
+ begin
156
+ https = Net::HTTP.new(uri.host, uri.port)
157
+ https.use_ssl = true
158
+ https.open_timeout = 5
159
+ https.read_timeout = 15
160
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
161
+ https.verify_depth = 5
162
+
163
+ https.start {|w|
164
+ response = w.get(uri.request_uri)
165
+ case response
166
+ when Net::HTTPSuccess
167
+ json = JSON.parse(response.body)
168
+ raise JSONException, response.body if json == nil
169
+ raise APIErrorException, json["error"] if json.is_a?(Hash) && json.has_key?("error")
170
+ get_cool_down
171
+ return json
172
+ else
173
+ raise ConnectionFailedException, "Failed to connect to zaif."
174
+ end
175
+ }
176
+ rescue
177
+ raise
178
+ end
179
+ end
180
+
181
+ # Connect to address via https, and return json reponse.
182
+ def post_ssl(address, method, data)
183
+ check_key
184
+ uri = URI.parse(address)
185
+ data["method"] = method
186
+ data["nonce"] = get_nonce
187
+ begin
188
+ req = Net::HTTP::Post.new(uri)
189
+ req.set_form_data(data)
190
+ req["Key"] = @api_key
191
+ req["Sign"] = OpenSSL::HMAC::hexdigest(OpenSSL::Digest.new('sha512'), @api_secret, req.body)
192
+
193
+
194
+ https = Net::HTTP.new(uri.host, uri.port)
195
+ https.use_ssl = true
196
+ https.open_timeout = 5
197
+ https.read_timeout = 15
198
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
199
+ https.verify_depth = 5
200
+
201
+ https.start {|w|
202
+ response = w.request(req)
203
+ case response
204
+ when Net::HTTPSuccess
205
+ json = JSON.parse(response.body)
206
+ raise JSONException, response.body if json == nil
207
+ raise APIErrorException, json["error"] if json.is_a?(Hash) && json["success"] == 0
208
+ get_cool_down
209
+ return json["return"]
210
+ else
211
+ raise ConnectionFailedException, "Failed to connect to zaif: " + response.value
212
+ end
213
+ }
214
+ rescue
215
+ raise
216
+ end
217
+ end
218
+
219
+ def get_nonce
220
+ time = Time.now.to_f
221
+ return time.to_i
222
+ end
223
+
224
+ def get_cool_down
225
+ if @cool_down
226
+ sleep(@cool_down_time)
227
+ end
228
+ end
229
+
230
+ end
231
+ end
@@ -0,0 +1,5 @@
1
+ module Zaif
2
+ class ConnectionFailedException < StandardError; end
3
+ class APIErrorException < StandardError; end
4
+ class JSONException < StandardError; end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Zaif
2
+ VERSION = "0.0.2"
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'
@@ -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 'zaif/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zaif"
8
+ spec.version = Zaif::VERSION
9
+ spec.authors = ["Palon"]
10
+ spec.email = ["palon7@gmail.com"]
11
+ spec.summary = %q{Zaif API wrapper.}
12
+ spec.description = %q{Zaif API wrapper for monacoin/bitcoin trade.}
13
+ spec.homepage = "https://github.com/palon7/zaif-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
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zaif
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Palon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-05 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: Zaif 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
+ - examples/config.default.rb
68
+ - examples/test.rb
69
+ - examples/test_trade.rb
70
+ - lib/zaif.rb
71
+ - lib/zaif/exceptions.rb
72
+ - lib/zaif/version.rb
73
+ - spec/etwings_spec.rb
74
+ - spec/spec_helper.rb
75
+ - zaif.gemspec
76
+ homepage: https://github.com/palon7/zaif-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: Zaif API wrapper.
100
+ test_files:
101
+ - spec/etwings_spec.rb
102
+ - spec/spec_helper.rb