module ApiHelpers::ClassMethods

Public Instance Methods

bittrex_uri() click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 8
def bittrex_uri
  'https://api.bittrex.com/v3/'
end
delete_signed(api_group, params = {}) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 45
def delete_signed(api_group, params = {})
  api_group, params, sub_account_id, headers, url = setup_call(api_group, params, 'DELETE')

  begin
    response = HTTParty.delete(url, {body: params, headers: headers})
  rescue => e
    res_hash e.response
  end
end
get(api_group, params = {}) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 12
def get(api_group, params = {})
  params.compact!
  api_group, params, sub_account_id = setup_params(api_group, params)

  begin
    response = HTTParty.get(bittrex_uri + api_group, query: params)
  rescue => e
    res_hash e.response
  end
end
get_signed(api_group, params = {}) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 23
def get_signed(api_group, params = {})
  api_group, params, sub_account_id, headers, url = setup_call(api_group, params, 'GET')

  begin
    response = HTTParty.get(url, {query: params, headers: headers})
    res_hash response
  rescue => e
    res_hash e.response
  end
end
post_signed(api_group, params = {}) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 34
def post_signed(api_group, params = {})
  api_group, params, sub_account_id, headers, url = setup_call(api_group, params, 'POST')

  begin
    response = HTTParty.post(url, {body: params.to_json, headers: headers})
    res_hash response
  rescue => e
    res_hash e.response
  end
end

Private Instance Methods

create_content_sign(content) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 104
def create_content_sign(content)
  content = content.compact.map { |k, v| "#{k}=#{v}" }.join('&') if content.is_a?(Hash)
  Digest::SHA512.hexdigest(content.to_s)
end
create_sign_hmac(pre_sign) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 100
def create_sign_hmac(pre_sign)
  OpenSSL::HMAC.hexdigest('sha512', BittrexEnterprise.configuration.secret, pre_sign) #.to_s
end
res_hash(res) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 109
def res_hash(res)
  data = JSON.parse(res.body)
  data.extend DeepSymbolizable
  #### IF YOU WANT THE RAW RESPONSE IN ADDITION TO THE DATA USE THIS
  #### { data: data.deep_symbolize, res: res }
  #### INSTEAD OF THE FOLLOWING LINE
  { data: data.deep_symbolize }
end
setup_call(api_group, params, method) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 57
def setup_call(api_group, params, method)
  params.compact!
  api_group, params, sub_account_id = setup_params(api_group, params)

  url = bittrex_uri + api_group

  headers = setup_headers(method, url, params, sub_account_id)

  [api_group, params, sub_account_id, headers, url]
end
setup_headers(method, uri, params='', api_subaccount_id='') click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 79
def setup_headers(method, uri, params='', api_subaccount_id='')
  api_key = BittrexEnterprise.configuration.key
  api_timestamp = DateTime.now.strftime('%Q')
  params = params.to_json if method == 'POST'
  api_content_hash = create_content_sign(params)
  pre_sign = [api_timestamp, uri, method, api_content_hash, api_subaccount_id].join('')
  api_signature = create_sign_hmac(pre_sign)

  headers = {
    "Content-Type": 'application/json',
    "Api-Key": api_key,
    "Api-Timestamp": api_timestamp,
    "Api-Content-Hash": api_content_hash,
    "Api-Signature": api_signature
  }

  headers['Api-Subaccount-Id'] = api_subaccount_id if api_subaccount_id != ''

  headers
end
setup_params(api_group, params) click to toggle source
# File lib/bittrex-enterprise/api_helpers.rb, line 68
def setup_params(api_group, params)
  sub_account_id = params.delete(:sub_account_id) || ''
  matchArr = api_group.match(/{(\D*)}/)&.captures
  if matchArr
    matchArr.each do |m|
      api_group.gsub!("{#{m}}", params.delete(m.to_sym))
    end
  end
  [api_group, params, sub_account_id]
end