class Bibox::Utilities

Public Class Methods

convert_value(value, type, use_ms_for_time: true) click to toggle source
# File lib/bibox/utilities.rb, line 10
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
decode_and_inflate(data) click to toggle source
# File lib/bibox/utilities.rb, line 6
def decode_and_inflate(data)
  Zlib::GzipReader.new(StringIO.new(Base64.decode64(data))).read
end
divide(number, divisor) click to toggle source
# File lib/bibox/utilities.rb, line 65
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/bibox/utilities.rb, line 46
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/bibox/utilities.rb, line 50
def numeric?(string)
  !!Kernel.Float(string) 
rescue TypeError, ArgumentError
  false
end
time_to_epoch(time, ms: false) click to toggle source
# File lib/bibox/utilities.rb, line 42
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/bibox/utilities.rb, line 56
def to_decimal(number)
  if number
    num = number.to_s
    num.empty? ? nil : BigDecimal.new(num)
  else
    nil
  end
end