Class: ZATCA::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/zatca/tags.rb

Instance Method Summary collapse

Constructor Details

#initialize(tags) ⇒ Tags

Returns a new instance of Tags.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zatca/tags.rb', line 5

def initialize(tags)
  keys_to_coerce_to_string = tags.except(:timestamp).keys
  stringified_tags = tags.map do |key, value|
    if keys_to_coerce_to_string.include?(key)
      [key, value.to_s]
    else
      [key, value]
    end
  end.to_h

  schema_result = TagsSchema.call(stringified_tags)
  if schema_result.failure?
    raise(Error, "Parsing tags failed due to:\n#{schema_result.errors(full: true).to_h}")
  end

  stringified_tags[:timestamp] = tags[:timestamp].to_s

  # Create tags, then sort them by ID
  @tags = stringified_tags.map do |key, value|
    Tag.new(key: key, value: value)
  end.sort_by(&:id)
end

Instance Method Details

#[](index) ⇒ Object



28
29
30
# File 'lib/zatca/tags.rb', line 28

def [](index)
  @tags[index]
end

#to_base64Object



32
33
34
# File 'lib/zatca/tags.rb', line 32

def to_base64
  Base64.strict_encode64(to_tlv)
end

#to_hex_tlvObject

This is helpful for debugging only, for ZATCA’s requirements just call ‘to_base64`



37
38
39
# File 'lib/zatca/tags.rb', line 37

def to_hex_tlv
  to_tlv.unpack1("H*")
end

#to_tlvObject

This is helpful for debugging only, for ZATCA’s requirements just call ‘to_base64`



42
43
44
# File 'lib/zatca/tags.rb', line 42

def to_tlv
  @tags.map(&:to_tlv).join("")
end