Class: Nanook::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/nanook/util.rb

Overview

Set of class utility methods.

Constant Summary

STEP =

Constant used to convert back and forth between raw and NANO.

BigDecimal.new("10")**BigDecimal.new("30")

Class Method Summary collapse

Class Method Details

.coerce_empty_string_to_type(response, type) ⇒ Object

Converts an empty String value into an empty version of another type.

The RPC often returns an empty String ("") as a value, when a nil, or empty Array ([]), or empty Hash ({}) would be better. If the response might be

Parameters:

  • response

    the value returned from the RPC server

  • type

    the type to return an empty of



35
36
37
38
39
40
41
# File 'lib/nanook/util.rb', line 35

def self.coerce_empty_string_to_type(response, type)
  if response == "" || response.nil?
    return type.new
  end

  response
end

.NANO_to_raw(nano) ⇒ Integer

Converts an amount of NANO to an amount of raw.

Parameters:

  • nano (Float|Integer)

    amount in nano

Returns:

  • (Integer)

    amount in raw



15
16
17
# File 'lib/nanook/util.rb', line 15

def self.NANO_to_raw(nano)
  (BigDecimal.new(nano.to_s) * STEP).to_i
end

.raw_to_NANO(raw) ⇒ Float|Integer

Converts an amount of raw to an amount of NANO.

Parameters:

  • raw (Integer)

    amount in raw

Returns:

  • (Float|Integer)

    amount in NANO



23
24
25
# File 'lib/nanook/util.rb', line 23

def self.raw_to_NANO(raw)
  (raw.to_f / STEP).to_f
end