Class: ZATCA::Tag

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

Constant Summary collapse

TAG_IDS =
{
  seller_name: 1,
  vat_registration_number: 2,
  timestamp: 3,
  invoice_total: 4,
  vat_total: 5,
  xml_invoice_hash: 6,
  ecdsa_signature: 7,
  ecdsa_public_key: 8,
  ecdsa_stamp_signature: 9 # TODO: is this needed ?
}.freeze
PHASE_1_TAGS =
[
  :seller_name,
  :vat_registration_number,
  :timestamp,
  :invoice_total,
  :vat_total
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, value:) ⇒ Tag

Returns a new instance of Tag.



25
26
27
28
29
# File 'lib/zatca/tag.rb', line 25

def initialize(key:, value:)
  @id = TAG_IDS[key]
  @key = key
  @value = value.to_s
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



23
24
25
# File 'lib/zatca/tag.rb', line 23

def id
  @id
end

#keyObject

Returns the value of attribute key.



23
24
25
# File 'lib/zatca/tag.rb', line 23

def key
  @key
end

#valueObject

Returns the value of attribute value.



23
24
25
# File 'lib/zatca/tag.rb', line 23

def value
  @value
end

Instance Method Details

#should_be_utf8_encoded?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/zatca/tag.rb', line 35

def should_be_utf8_encoded?
  PHASE_1_TAGS.include?(key)
end

#to_hObject



31
32
33
# File 'lib/zatca/tag.rb', line 31

def to_h
  {id: @id, key: @key, value: @value}
end

#to_tlvObject



39
40
41
42
# File 'lib/zatca/tag.rb', line 39

def to_tlv
  tlv = @id.chr + @value.bytesize.chr + @value
  tlv.force_encoding("ASCII-8BIT")
end