Class: Bibox::Utilities
- Inherits:
-
Object
- Object
- Bibox::Utilities
- Defined in:
- lib/bibox/utilities.rb
Class Method Summary collapse
- .convert_value(value, type, use_ms_for_time: true) ⇒ Object
- .decode_and_inflate(data) ⇒ Object
- .divide(number, divisor) ⇒ Object
- .epoch_to_time(epoch, ms: false) ⇒ Object
- .numeric?(string) ⇒ Boolean
- .time_to_epoch(time, ms: false) ⇒ Object
- .to_decimal(number) ⇒ Object
Class Method Details
.convert_value(value, type, use_ms_for_time: true) ⇒ Object
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 37 38 39 40 |
# 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) ⇒ Object
6 7 8 |
# 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) ⇒ Object
65 66 67 68 |
# 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) ⇒ Object
46 47 48 |
# 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) ⇒ Boolean
50 51 52 53 54 |
# File 'lib/bibox/utilities.rb', line 50 def numeric?(string) !!Kernel.Float(string) rescue TypeError, ArgumentError false end |
.time_to_epoch(time, ms: false) ⇒ Object
42 43 44 |
# 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) ⇒ Object
56 57 58 59 60 61 62 63 |
# 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 |