Class: Bech32::Nostr::TLVEntry
- Inherits:
-
Object
- Object
- Bech32::Nostr::TLVEntry
- Defined in:
- lib/bech32/nostr/entity.rb
Instance Attribute Summary collapse
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(type, value, label = nil) ⇒ TLVEntry
constructor
A new instance of TLVEntry.
-
#to_payload ⇒ String
Convert to binary data.
- #to_s ⇒ Object
Constructor Details
#initialize(type, value, label = nil) ⇒ TLVEntry
Returns a new instance of TLVEntry.
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
#label ⇒ Object (readonly)
Returns the value of attribute label.
27 28 29 |
# File 'lib/bech32/nostr/entity.rb', line 27 def label @label end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
26 27 28 |
# File 'lib/bech32/nostr/entity.rb', line 26 def type @type end |
#value ⇒ Object (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_payload ⇒ String
Convert to 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_s ⇒ Object
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 |