class VaultOfSatoshi::API::Base

Constants

ALL_CURRENCIES
CRYPTO_CURRENCIES
FIAT_CURRENCIES

Public Class Methods

new(api_key, api_secret) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 11
def initialize(api_key, api_secret)
  @api_key = api_key
  @api_secret = api_secret
end

Public Instance Methods

inspect() click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 16
def inspect
  "#<#{self.class}:#{self.object_id}>"
end

Private Instance Methods

date_input_to_microseconds(date_input) click to toggle source

Accepts a date_input in the form of a unix timestamp, Date object, or Time object

# File lib/vault_of_satoshi/api/base.rb, line 78
def date_input_to_microseconds(date_input)
  DateTime.strptime(date_input.to_f.to_s, '%s').strftime("%s").to_i * 1_000_000
end
generate_api_sign(endpoint, params) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 33
def generate_api_sign(endpoint, params)
  data = params.to_param
  Base64.strict_encode64(OpenSSL::HMAC.hexdigest('sha512', @api_secret, endpoint + 0.chr + data))
end
generate_currency_object(number) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 82
def generate_currency_object(number)
  precision = 8
  value = sprintf("%0.#{precision}f", number)
  {
    precision: precision,
    value: value,
    value_int: value.gsub('.', '').to_i
  }
end
headers(endpoint, params) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 26
def headers(endpoint, params)
  {
    "Api-Key"  => @api_key,
    "Api-Sign" => generate_api_sign(endpoint, params)
  }
end
nonce() click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 22
def nonce
  sprintf("%0.6f", Time.now.to_f).gsub('.', '')
end
parse_boolean(int) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 73
def parse_boolean(int)
  int.to_i == 1
end
parse_currency_object(object) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 69
def parse_currency_object(object)
  BigDecimal.new(object["value"]).truncate(object["precision"])
end
parse_data(data, options = {}) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 38
def parse_data(data, options = {})
  data = OpenStruct.new(data)

  [*options[:unix_timestamps]].each do |unix_timestamp|
    data[unix_timestamp] = parse_unix_timestamp(data[unix_timestamp])
  end

  [*options[:microsecond_timestamps]].each do |microsecond_timestamp|
    data[microsecond_timestamp] = parse_microsecond_timestamp(data[microsecond_timestamp])
  end

  [*options[:currency_objects]].each do |currency_object|
    data[currency_object] = parse_currency_object(data[currency_object])
  end

  [*options[:booleans]].each do |boolean|
    data[boolean] = parse_boolean(data[boolean])
  end

  data
end
parse_microsecond_timestamp(micro_seconds_since_epoch) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 64
def parse_microsecond_timestamp(micro_seconds_since_epoch)
  seconds_since_epoch = BigDecimal.new(micro_seconds_since_epoch.to_s) / 1_000_000
  DateTime.strptime(seconds_since_epoch.to_s("F"), "%s")
end
parse_unix_timestamp(seconds_since_epoch) click to toggle source
# File lib/vault_of_satoshi/api/base.rb, line 60
def parse_unix_timestamp(seconds_since_epoch)
  DateTime.strptime(seconds_since_epoch.to_s, "%s")
end