module Cryptopia::Api::Private
Constants
- AVAILABLE_PARAMS
- ENDPOINT
- EXACT_PARAMS
- OPTIONAL_PARAMS
Attributes
api_key[R]
api_secret[R]
options[R]
url[R]
Public Class Methods
new(api_key = nil, api_secret = nil)
click to toggle source
# File lib/cryptopia/api/private.rb, line 30 def initialize(api_key = nil, api_secret = nil) @api_key = api_key @api_secret = api_secret end
Public Instance Methods
balance(options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 35 def balance(options = {}) for_uri(Private::ENDPOINT) do if invalid_params?(:balance, options) raise ArgumentError, "Arguments must be #{params(:balance)}" end handle_response(auth_post('/GetBalance', options)) end end
cancel_trade(options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 99 def cancel_trade(options = {}) for_uri(Private::ENDPOINT) do if invalid_params?(:cancel_trade, options, true) raise ArgumentError, "Arguments must be #{params(:cancel_trade)}" end handle_response(auth_post('/CancelTrade', options)) end end
deposit_address(options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 45 def deposit_address(options = {}) for_uri(Private::ENDPOINT) do if invalid_params?(:deposit_address, options) raise ArgumentError, "Arguments must be #{params(:deposit_address)}" end handle_response(auth_post('/GetDepositAddress', options)) end end
open_orders(options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 55 def open_orders(options = {}) for_uri(Private::ENDPOINT) do if invalid_params?(:deposit_address, options) raise ArgumentError, "Arguments must be #{params(:deposit_address)}" end handle_response(auth_post('/GetOpenOrders', options)) end end
submit_trade(options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 89 def submit_trade(options = {}) for_uri(Private::ENDPOINT) do if invalid_params?(:submit_trade, options, true) raise ArgumentError, "Arguments must be #{params(:submit_trade)}" end handle_response(auth_post('/SubmitTrade', options)) end end
trade_history(options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 65 def trade_history(options = {}) for_uri(Private::ENDPOINT) do if invalid_params?(:trade_history, options) raise ArgumentError, "Arguments must be #{params(:trade_history)}" end handle_response(auth_post('/GetTradeHistory', options)) end end
transactions(options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 75 def transactions(options = {}) for_uri(Private::ENDPOINT) do if invalid_params?(:transactions, options) raise ArgumentError, "Arguments must be #{params(:transactions)}" end if invalid_transaction_type?(options) raise ArgumentError, "Type must be 'Deposit' or 'Withdraw'" end handle_response(auth_post('/GetTransactions', options)) end end
Private Instance Methods
auth_post(endpoint, options = {})
click to toggle source
# File lib/cryptopia/api/private.rb, line 113 def auth_post(endpoint, options = {}) if keys_is_not_present? raise ArgumentError, "The api key and/or api secret must be informed" end @url = self.class.base_uri + endpoint @options = options.to_json response = self.class.post( endpoint, body: @options, headers: { 'Authorization' => "amx #{authorization_formatted_value}", 'Content-Type' => 'application/json' }) # Nonce should be reset after each request to avoid # "Nonce has already been used for this request." error @nonce = nil response end
hashed_post_params()
click to toggle source
# File lib/cryptopia/api/private.rb, line 169 def hashed_post_params md5 = Digest::MD5.new.digest(options.to_s) Base64.strict_encode64(md5) end
hmacsignature()
click to toggle source
# File lib/cryptopia/api/private.rb, line 149 def hmacsignature hmac = OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha256'), Base64.strict_decode64(api_secret), signature ) Base64.strict_encode64(hmac).strip end
invalid_params?(endpoint, options = {}, exact = false)
click to toggle source
# File lib/cryptopia/api/private.rb, line 185 def invalid_params?(endpoint, options = {}, exact = false) return false if options.keys.length.zero? available_keys = options.keys - AVAILABLE_PARAMS[endpoint] available_keys.length == 1 && ( OPTIONAL_PARAMS.key?(endpoint) && (OPTIONAL_PARAMS[endpoint] - available_keys) >= 1 ) && ( exact && EXACT_PARAMS.key?(endpoint) && EXACT_PARAMS[endpoint] ) end
invalid_transaction_type?(options)
click to toggle source
# File lib/cryptopia/api/private.rb, line 179 def invalid_transaction_type?(options) return false if options.keys.length.zero? options.key?(:Type) && (options[:Type] != 'Deposit' && options[:Type] != 'Withdraw') end
keys_is_not_present?()
click to toggle source
# File lib/cryptopia/api/private.rb, line 136 def keys_is_not_present? (api_key.nil? || (!api_key.nil? && api_key == '')) || (api_secret.nil? || (!api_secret.nil? && api_secret == '')) end
nonce()
click to toggle source
# File lib/cryptopia/api/private.rb, line 175 def nonce @nonce ||= Time.now.to_i.to_s end
params(endpoint)
click to toggle source
# File lib/cryptopia/api/private.rb, line 201 def params(endpoint) AVAILABLE_PARAMS[endpoint].join(' or ') end
signature()
click to toggle source
# File lib/cryptopia/api/private.rb, line 159 def signature [ api_key, 'POST', CGI::escape(url).downcase, nonce, hashed_post_params ].join.strip end