Class: Kucoin::Utilities::Parsing

Inherits:
Object
  • Object
show all
Defined in:
lib/kucoin/utilities/parsing.rb

Class Method Summary collapse

Class Method Details

.convert_value(value, type, use_ms_for_time: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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 :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) ⇒ Object



61
62
63
64
# File 'lib/kucoin/utilities/parsing.rb', line 61

def divide(number, divisor)
  result = number / divisor
  result.nan? || result.infinite? ? 0 : result
end

.epoch_to_time(epoch, ms: false) ⇒ Object



42
43
44
# File 'lib/kucoin/utilities/parsing.rb', line 42

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) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/kucoin/utilities/parsing.rb', line 46

def numeric?(string)
  !!Kernel.Float(string) 
rescue TypeError, ArgumentError
  false
end

.time_to_epoch(time, ms: false) ⇒ Object



38
39
40
# File 'lib/kucoin/utilities/parsing.rb', line 38

def time_to_epoch(time, ms: false)
  ms ? (time.to_i * 1_000) : time.to_i
end

.to_decimal(number) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/kucoin/utilities/parsing.rb', line 52

def to_decimal(number)
  if number
    num = number.to_s
    num.empty? ? nil : BigDecimal.new(num)
  else
    nil
  end
end