Class: Bech32::Nostr::TLVEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/bech32/nostr/entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value, label = nil) ⇒ TLVEntry

Returns a new instance of TLVEntry.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
# File 'lib/bech32/nostr/entity.rb', line 30

def initialize(type, value, label = nil)
  raise ArgumentError, "Type #{type} unsupported." unless TLVEntity::TYPES.include?(type)

  @type = type
  @value = value
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



27
28
29
# File 'lib/bech32/nostr/entity.rb', line 27

def label
  @label
end

#typeObject (readonly)

Returns the value of attribute type.



26
27
28
# File 'lib/bech32/nostr/entity.rb', line 26

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



28
29
30
# File 'lib/bech32/nostr/entity.rb', line 28

def value
  @value
end

Instance Method Details

#to_payloadString

Convert to binary data.

Returns:

  • (String)

    binary data.



40
41
42
43
44
45
46
47
48
# File 'lib/bech32/nostr/entity.rb', line 40

def to_payload
  data = if value.is_a?(Integer)
           [value].pack('N')
         else
           hex_string?(value) ? [value].pack('H*') : value
         end
  len = data.bytesize
  [type, len].pack('CC') + data
end

#to_sObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bech32/nostr/entity.rb', line 50

def to_s
  type_label = case type
               when TLVEntity::TYPE_SPECIAL
                 'special'
               when TLVEntity::TYPE_RELAY
                 'relay'
               when TLVEntity::TYPE_AUTHOR
                 'author'
               when TLVEntity::TYPE_KIND
                 'kind'
               else
                 'unknown'
               end
  "#{type_label}: #{value}"
end