Class: BinaryCodec::Currency

Inherits:
Hash160 show all
Defined in:
lib/binary-codec/types/currency.rb

Constant Summary collapse

XRP_HEX_REGEX =
/^0{40}$/
ISO_REGEX =
/^[A-Z0-9a-z?!@#$%^&*(){}\[\]\|]{3}$/
HEX_REGEX =
/^[A-F0-9]{40}$/
STANDARD_FORMAT_HEX_REGEX =
/^0{24}[\x00-\x7F]{6}0{10}$/

Instance Attribute Summary collapse

Attributes inherited from SerializedType

#bytes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#compare_to, from_parser, #nibblet

Methods inherited from ComparableSerializedType

#compare_to, #eq, #gt, #gte, #lt, #lte

Methods inherited from SerializedType

from_bytes, from_hex, from_json, from_parser, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex, #to_s

Constructor Details

#initialize(byte_buf = nil) ⇒ Currency



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/binary-codec/types/currency.rb', line 17

def initialize(byte_buf = nil)
  super(byte_buf || Array.new(20, 0)) # Defaults to XRP bytes if no buffer is given
  hex = bytes_to_hex(@bytes)

  if XRP_HEX_REGEX.match?(hex)
    @_iso = 'XRP'
  elsif STANDARD_FORMAT_HEX_REGEX.match?(hex)
    @_iso = iso_code_from_hex(@bytes[12..14])
  else
    @_iso = nil
  end
end

Instance Attribute Details

#isoObject (readonly)

Returns the value of attribute iso.



13
14
15
# File 'lib/binary-codec/types/currency.rb', line 13

def iso
  @iso
end

Class Method Details

.from(value) ⇒ Object

Raises:

  • (StandardError)


34
35
36
37
38
39
40
41
42
# File 'lib/binary-codec/types/currency.rb', line 34

def self.from(value)
  return value if value.is_a?(Currency)

  if value.is_a?(String)
    return Currency.new(bytes_from_representation(value))
  end

  raise StandardError, 'Cannot construct Currency from value given'
end

Instance Method Details

#to_jsonObject



44
45
46
47
48
49
# File 'lib/binary-codec/types/currency.rb', line 44

def to_json
  iso = self.iso
  return iso unless iso.nil?

  bytes_to_hex(@bytes)
end