Class: ZaifWrapper::Client::ZaifLeverageApi

Inherits:
Object
  • Object
show all
Defined in:
lib/zaif_wrapper/client.rb

Constant Summary collapse

REQUEST_URL_BASE =
'https://api.zaif.jp/tlapi'

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret) ⇒ ZaifLeverageApi

Returns a new instance of ZaifLeverageApi.



163
164
165
166
# File 'lib/zaif_wrapper/client.rb', line 163

def initialize(api_key, api_secret)
  @api_key = api_key
  @api_secret = api_secret
end

Instance Method Details

#active_positions(params = {}) ⇒ Object



200
201
202
203
# File 'lib/zaif_wrapper/client.rb', line 200

def active_positions(params = {})
  raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?)
  request('active_positions', params)
end

#cancel_position(params = {}) ⇒ Object



215
216
217
218
# File 'lib/zaif_wrapper/client.rb', line 215

def cancel_position(params = {})
  raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?) || params["leverage_id"].nil?
  request('cancel_position', params)
end

#change_position(params = {}) ⇒ Object



210
211
212
213
# File 'lib/zaif_wrapper/client.rb', line 210

def change_position(params = {})
  raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?) || params["leverage_id"].nil? || params["price"].nil?
  request('change_position', params)
end

#create_position(params = {}) ⇒ Object



205
206
207
208
# File 'lib/zaif_wrapper/client.rb', line 205

def create_position(params = {})
  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?
  request('create_position', params)
end

#create_signature(body) ⇒ Object



224
225
226
# File 'lib/zaif_wrapper/client.rb', line 224

def create_signature(body)
  OpenSSL::HMAC::hexdigest(OpenSSL::Digest.new('sha512'), @api_secret, body.to_s)
end

#get_nonceObject



220
221
222
# File 'lib/zaif_wrapper/client.rb', line 220

def get_nonce
  Time.now.to_f.to_i
end

#get_positions(params = {}) ⇒ Object



190
191
192
193
# File 'lib/zaif_wrapper/client.rb', line 190

def get_positions(params = {})
  raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?)
  request('get_positions', params)
end

#position_history(params = {}) ⇒ Object



195
196
197
198
# File 'lib/zaif_wrapper/client.rb', line 195

def position_history(params = {})
  raise "Required parameters are missing" if params["type"].nil? || (params["type"] == 'futures' && params["group_id"].nil?)
  request('position_history', params)
end

#request(method, params = {}) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/zaif_wrapper/client.rb', line 168

def request(method, params = {})
  body = {
      'method' => method,
      'nonce' => get_nonce
  }
  signature_text = "method=#{method}&nonce=#{get_nonce}"
  unless params.empty?
    params.each { |param|
      body.store(param[0], param[1])
      signature_text = "#{signature_text}&#{param[0]}=#{param[1]}"
    }
  end

  response = RestClient.post REQUEST_URL_BASE, body, {
      content_type: :json,
      accept: :json,
      key: @api_key,
      sign: create_signature(signature_text)
  }
  JSON.parse(response.body)
end