class Kucoin::Utilities::Parsing

Public Class Methods

convert_value(value, type, use_ms_for_time: true) click to toggle source
# File lib/kucoin/utilities/parsing.rb, line 6
def convert_value(value, type, use_ms_for_time: true)
  if type.is_a?(Symbol)
    return case type
      when :string
        value.to_s
      when :symbol
        value.to_s.underscore.downcase.to_sym
      when :integer
        value.to_i
      when :float
        value.to_f
      when :decimal
        to_decimal(value)
      when :boolean
        ["true", "1"].include?(value.to_s.downcase)
      when :datetime
        DateTime.parse(value)
      when :time
        epoch_to_time(value, ms: use_ms_for_time)
      when :hash
        value.symbolize_keys
      else
        value
    end
  elsif type.is_a?(Hash)
    return case type.keys.first
      when :enum
        type[:enum][value.to_i]
      else
        value
    end
  end
end
divide(number, divisor) click to toggle source
# File lib/kucoin/utilities/parsing.rb, line 63
def divide(number, divisor)
  result = number / divisor
  result.nan? || result.infinite? ? 0 : result
end
epoch_to_time(epoch, ms: false) click to toggle source
# File lib/kucoin/utilities/parsing.rb, line 44
def epoch_to_time(epoch, ms: false)
  ms ? ::Time.at(epoch.to_i / 1_000).utc : ::Time.at(epoch.to_i).utc
end
numeric?(string) click to toggle source
# File lib/kucoin/utilities/parsing.rb, line 48
def numeric?(string)
  !!Kernel.Float(string) 
rescue TypeError, ArgumentError
  false
end
time_to_epoch(time, ms: false) click to toggle source
# File lib/kucoin/utilities/parsing.rb, line 40
def time_to_epoch(time, ms: false)
  ms ? (time.to_i * 1_000) : time.to_i
end
to_decimal(number) click to toggle source
# File lib/kucoin/utilities/parsing.rb, line 54
def to_decimal(number)
  if number
    num = number.to_s
    num.empty? ? nil : BigDecimal.new(num)
  else
    nil
  end
end