zaif_wrapper 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fca04b0cb5dfe64933455e0b3d5621b660f429a2fbd85f4fbbcf4343e6c64b31
4
- data.tar.gz: 38d1951fd6b11338f7c094d6d1ea2a2e52422b505a76f0cb012fca3827e5f715
3
+ metadata.gz: e27fd5013f0380c8786937a1f32a7efa10aaf5c9bb159b5c8217be6b5c1a9967
4
+ data.tar.gz: 4290253e52a8a4a43c2234210c9d448febfe079723542d13b5a12d7e97c4b181
5
5
  SHA512:
6
- metadata.gz: f422e3ac8f0a14d21700c3964260bf826a4e9248b0dfbad40755d5cfbec2b8f50a189d543353d53f3dce37da25f917e61d75a4da269b533917faa0ac5fa4eec9
7
- data.tar.gz: 10bf44494aea8ef677f970b5b0c70ef99525a4f4944fbb4f1ef215a59926390398824e3cedee5d813a5715fc02affa9aa94b84dddba878fd4ffeaff542f4ed77
6
+ metadata.gz: '09c228e10cb62b919c54f8d1ef30fa6a0acd3067617402557df60886935580e3abb4e8b07904767137f67a6ec5055573c5b089acda0dd1840afd97a54bda3e70'
7
+ data.tar.gz: 2e8ab17f36c5a0e8d49d0dc1ec111d951672ec049f3232b7c4a2326bdf464b94e4bb7fa58f598b6b2394c920e0f688a183786a83a5cabc7a5673144c167b2093
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zaif_wrapper (0.1.0)
4
+ zaif_wrapper (0.1.1)
5
5
  json
6
6
  rest-client
7
7
  websocket-client-simple
@@ -6,52 +6,36 @@ require 'websocket-client-simple'
6
6
 
7
7
  module ZaifWrapper
8
8
  module Client
9
- class ZaifPublicApi
10
- REQUEST_URL_BASE = 'https://api.zaif.jp/api/1/'
11
- METHODS = {
12
- :currencies => 'currency_code',
13
- :currency_pairs => 'currency_pair',
14
- :last_price => 'currency_pair',
15
- :ticker => 'currency_pair',
16
- :trades => 'currency_pair',
17
- :depth => 'currency_pair'
18
- }.freeze
19
-
20
- def request(path)
21
- response = RestClient.get "#{REQUEST_URL_BASE}#{path}"
9
+ class ZaifParentApi
10
+ PUBLIC_REQUEST_URL_BASE = 'https://api.zaif.jp/api/1/'.freeze
11
+ PRIVATE_REQUEST_URL_BASE = 'https://api.zaif.jp/tapi'.freeze
12
+ FUTURE_REQUEST_URL_BASE = 'https://api.zaif.jp/fapi/1/'.freeze
13
+ LEVERAGE_REQUEST_URL_BASE = 'https://api.zaif.jp/tlapi'.freeze
14
+ LEVERAGE_METHODS = ['get_positions', 'position_history', 'active_positions', 'create_position', 'change_position', 'cancel_position'].freeze
15
+ PUBLIC_METHODS = {
16
+ :currencies => 'currency_code',
17
+ :currency_pairs => 'currency_pair',
18
+ :last_price => 'currency_pair',
19
+ :ticker => 'currency_pair',
20
+ :trades => 'currency_pair',
21
+ :depth => 'currency_pair'
22
+ }.freeze
23
+ PRIVATE_METHODS = ['get_info', 'get_info2', 'get_personal_info', 'get_id_info', 'trade_history', 'active_orders', 'trade', 'cancel_order', 'withdraw', 'deposit_history', 'withdraw_history'].freeze
24
+ FUTURE_METHODS = ['groups', 'last_price', 'ticker', 'trades', 'depth'].freeze
25
+
26
+ def get_request(host, path)
27
+ response = RestClient.get "#{host}#{path}"
22
28
  JSON.parse(response.body)
23
29
  end
24
30
 
25
- METHODS.each do |method_name, params|
26
- define_method(method_name) { |params|
27
- path = "#{method_name.to_s}/#{params}"
28
- request(path)
29
- }
30
- end
31
- end
32
-
33
- class ZaifPrivateApi
34
- REQUEST_URL_BASE = 'https://api.zaif.jp/tapi'
35
-
36
- def initialize(api_key, api_secret)
37
- @api_key = api_key
38
- @api_secret = api_secret
39
- end
40
-
41
- def request(method, params = {})
42
- body = {
43
- 'method' => method,
44
- 'nonce' => get_nonce
31
+ def post_request(body, host)
32
+ body.store('nonce', get_nonce)
33
+ signature_text = ""
34
+ body.each_with_index { |param, i|
35
+ signature_text = signature_text + '&' if i != 0
36
+ signature_text = "#{signature_text}#{param[0]}=#{param[1]}"
45
37
  }
46
- signature_text = "method=#{method}&nonce=#{get_nonce}"
47
- unless params.empty?
48
- params.each { |param|
49
- body.store(param[0], param[1])
50
- signature_text = "#{signature_text}&#{param[0]}=#{param[1]}"
51
- }
52
- end
53
-
54
- response = RestClient.post REQUEST_URL_BASE, body, {
38
+ response = RestClient.post host, body, {
55
39
  content_type: :json,
56
40
  accept: :json,
57
41
  key: @api_key,
@@ -60,55 +44,6 @@ module ZaifWrapper
60
44
  JSON.parse(response.body)
61
45
  end
62
46
 
63
- def get_info
64
- request('get_info')
65
- end
66
-
67
- def get_info2
68
- request('get_info2')
69
- end
70
-
71
- def get_personal_info
72
- request('get_personal_info')
73
- end
74
-
75
- def get_id_info
76
- request('get_id_info')
77
- end
78
-
79
- def trade_history(params = {})
80
- request('trade_history', params)
81
- end
82
-
83
- def active_orders(params = {})
84
- request('active_orders', params)
85
- end
86
-
87
- def trade(params = {})
88
- raise "Required parameters are missing" if params["currency_pair"].nil? || params["action"].nil? || params["price"].nil? || params["amount"].nil?
89
- request('trade', params)
90
- end
91
-
92
- def cancel_order(params = {})
93
- raise "Required parameters are missing" if params["order_id"].nil?
94
- request('cancel_order', params)
95
- end
96
-
97
- def withdraw(params = {})
98
- raise "Required parameters are missing" if params["currency"].nil? || params["address"].nil? || params["amount"].nil?
99
- request('withdraw', params)
100
- end
101
-
102
- def deposit_history(params = {})
103
- raise "Required parameters are missing" if params["currency"].nil?
104
- request('deposit_history', params)
105
- end
106
-
107
- def withdraw_history(params = {})
108
- raise "Required parameters are missing" if params["currency"].nil?
109
- request('withdraw_history', params)
110
- end
111
-
112
47
  def get_nonce
113
48
  Time.now.to_f.to_i
114
49
  end
@@ -116,113 +51,120 @@ module ZaifWrapper
116
51
  def create_signature(body)
117
52
  OpenSSL::HMAC::hexdigest(OpenSSL::Digest.new('sha512'), @api_secret, body.to_s)
118
53
  end
119
- end
120
-
121
- class ZaifFutureApi
122
- REQUEST_URL_BASE = 'https://api.zaif.jp/fapi/1/'
123
-
124
- def request(path)
125
- response = RestClient.get "#{REQUEST_URL_BASE}#{path}"
126
- JSON.parse(response.body)
127
- end
128
54
 
129
- ## /groups/#{group_id}
130
- def groups(group_id)
131
- path = "groups/#{group_id}"
132
- request(path)
133
- end
134
-
135
- ## last_price/#{group_id}#{currency_pair}
136
- def last_price(group_id, currency_pair)
137
- path = "last_price/#{group_id}/#{currency_pair}"
138
- request(path)
139
- end
140
-
141
- ## /ticker/#{group_id}/#{currency_pair}
142
- def ticker(group_id, currency_pair)
143
- path = "ticker/#{group_id}/#{currency_pair}"
144
- request(path)
145
- end
146
-
147
- ## /trades/{group_id}/{currency_pair}
148
- def trades(group_id, currency_pair)
149
- path = "trades/#{group_id}/#{currency_pair}"
150
- request(path)
151
- end
152
-
153
- ## /depth/{group_id}/{currency_pair}
154
- def depth(group_id, currency_pair)
155
- path = "depth/#{group_id}/#{currency_pair}"
156
- request(path)
55
+ end
56
+ class ZaifPublicApi < ZaifParentApi
57
+ PUBLIC_METHODS.each do |method_name, params|
58
+ define_method(method_name) { |params|
59
+ path = "#{method_name.to_s}/#{params}"
60
+ get_request(PUBLIC_REQUEST_URL_BASE, path)
61
+ }
157
62
  end
158
63
  end
159
64
 
160
- class ZaifLeverageApi
161
- REQUEST_URL_BASE = 'https://api.zaif.jp/tlapi'
162
-
65
+ class ZaifPrivateApi < ZaifParentApi
163
66
  def initialize(api_key, api_secret)
164
67
  @api_key = api_key
165
68
  @api_secret = api_secret
166
69
  end
167
70
 
168
- def request(method, params = {})
169
- body = {
170
- 'method' => method,
171
- 'nonce' => get_nonce
172
- }
173
- signature_text = "method=#{method}&nonce=#{get_nonce}"
174
- unless params.empty?
175
- params.each { |param|
176
- body.store(param[0], param[1])
177
- signature_text = "#{signature_text}&#{param[0]}=#{param[1]}"
178
- }
71
+ def method_missing(name, *args)
72
+ if PRIVATE_METHODS.include?(name.to_s)
73
+ klass = class << self; self end
74
+ klass.class_eval do
75
+ define_method(name) do |body = {}|
76
+ check(name, body)
77
+ body.store("method", name.to_s)
78
+ post_request(body, PRIVATE_REQUEST_URL_BASE)
79
+ end
80
+ end
81
+ if args.length == 1
82
+ __send__(name, args[0])
83
+ else
84
+ __send__(name)
85
+ end
179
86
  end
180
-
181
- response = RestClient.post REQUEST_URL_BASE, body, {
182
- content_type: :json,
183
- accept: :json,
184
- key: @api_key,
185
- sign: create_signature(signature_text)
186
- }
187
- JSON.parse(response.body)
188
87
  end
189
88
 
190
- def get_positions(params = {})
191
- raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?)
192
- request('get_positions', params)
193
- end
194
-
195
- def position_history(params = {})
196
- raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?)
197
- request('position_history', params)
198
- end
199
-
200
- def active_positions(params = {})
201
- raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?)
202
- request('active_positions', params)
203
- end
204
-
205
- def create_position(params = {})
206
- raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?) || params["currency_pair"].nil? || params["action"].nil? || params["price"].nil? || params["amount"].nil? || params["leverage"].nil?
207
- request('create_position', params)
89
+ def check(method_name, body)
90
+ case method_name
91
+ when 'trade' then
92
+ raise "Required parameters are missing" if body["currency_pair"].nil? || body["action"].nil? || body["price"].nil? || body["amount"].nil?
93
+ when 'cancel_order' then
94
+ raise "Required parameters are missing" if body["order_id"].nil?
95
+ when 'withdraw' then
96
+ raise "Required parameters are missing" if body["currency"].nil? || body["address"].nil? || body["amount"].nil?
97
+ when 'deposit_history' then
98
+ raise "Required parameters are missing" if body["currency"].nil?
99
+ when 'withdraw_history' then
100
+ raise "Required parameters are missing" if body["currency"].nil?
101
+ end
208
102
  end
103
+ end
209
104
 
210
- def change_position(params = {})
211
- raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?) || params["leverage_id"].nil? || params["price"].nil?
212
- request('change_position', params)
105
+ class ZaifFutureApi < ZaifParentApi
106
+ def method_missing(name, *args)
107
+ if FUTURE_METHODS.include?(name.to_s)
108
+ klass = class << self; self end
109
+ klass.class_eval do
110
+ define_method(name) do |body = []|
111
+ path = name
112
+ if body.length != 0
113
+ body.each do |param|
114
+ path = path + "/#{param}"
115
+ end
116
+ end
117
+ get_request(FUTURE_REQUEST_URL_BASE, path)
118
+ end
119
+ end
120
+ if args.length == 1
121
+ __send__(name, args[0])
122
+ else
123
+ __send__(name)
124
+ end
125
+ end
213
126
  end
127
+ end
214
128
 
215
- def cancel_position(params = {})
216
- raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?) || params["leverage_id"].nil?
217
- request('cancel_position', params)
129
+ class ZaifLeverageApi < ZaifParentApi
130
+ def initialize(api_key, api_secret)
131
+ @api_key = api_key
132
+ @api_secret = api_secret
218
133
  end
219
134
 
220
- def get_nonce
221
- Time.now.to_f.to_i
135
+ def method_missing(name, *args)
136
+ if LEVERAGE_METHODS.include?(name.to_s)
137
+ klass = class << self; self end
138
+ klass.class_eval do
139
+ define_method(name) do |body = {}|
140
+ check(name, body)
141
+ body.store("method", name.to_s)
142
+ post_request(body, LEVERAGE_REQUEST_URL_BASE)
143
+ end
144
+ end
145
+ if args.length == 1
146
+ __send__(name, args[0])
147
+ else
148
+ __send__(name)
149
+ end
150
+ end
222
151
  end
223
152
 
224
- def create_signature(body)
225
- OpenSSL::HMAC::hexdigest(OpenSSL::Digest.new('sha512'), @api_secret, body.to_s)
153
+ def check(method_name, body)
154
+ case method_name
155
+ when 'get_positions' then
156
+ raise "Required parameters are missing" if body["type"].nil? || (body["type"] == 'futures' && body["group_id"].nil?)
157
+ when 'position_history' then
158
+ raise "Required parameters are missing" if body["type"].nil? || (body["type"] == 'futures' && body["group_id"].nil?)
159
+ when 'active_positions' then
160
+ raise "Required parameters are missing" if body["type"].nil? || (body["type"] == 'futures' && body["group_id"].nil?)
161
+ when 'create_position' then
162
+ raise "Required parameters are missing" if body["type"].nil? || (body["type"] == 'futures' && body["group_id"].nil?) || body["currency_pair"].nil? || body["action"].nil? || body["price"].nil? || body["amount"].nil? || body["leverage"].nil?
163
+ when 'change_position' then
164
+ raise "Required parameters are missing" if body["type"].nil? || (body["type"] == 'futures' && body["group_id"].nil?) || body["leverage_id"].nil? || body["price"].nil?
165
+ when 'cancel_position' then
166
+ raise "Required parameters are missing" if body["type"].nil? || (body["type"] == 'futures' && body["group_id"].nil?) || body["leverage_id"].nil?
167
+ end
226
168
  end
227
169
  end
228
170
 
@@ -1,3 +1,3 @@
1
1
  module ZaifWrapper
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaif_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - cobafan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-11 00:00:00.000000000 Z
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client