Class: Skyfall::CID

Inherits:
Object
  • Object
show all
Defined in:
lib/skyfall/cid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CID

Returns a new instance of CID.



29
30
31
# File 'lib/skyfall/cid.rb', line 29

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/skyfall/cid.rb', line 13

def data
  @data
end

Class Method Details

.from_cbor_tag(tag) ⇒ Object

Raises:



15
16
17
18
19
# File 'lib/skyfall/cid.rb', line 15

def self.from_cbor_tag(tag)
  data = tag.value
  raise DecodeError.new("Unexpected first byte of CID: #{data[0]}") unless data[0] == "\x00"
  CID.new(data[1..-1])
end

.from_json(string) ⇒ Object

Raises:



21
22
23
24
25
26
27
# File 'lib/skyfall/cid.rb', line 21

def self.from_json(string)
  raise DecodeError.new("Unexpected CID length") unless string.length == 59
  raise DecodeError.new("Unexpected CID prefix") unless string[0] == 'b'

  data = Base32.decode(string[1..-1].upcase)
  CID.new(data)
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/skyfall/cid.rb', line 41

def ==(other)
  other.is_a?(CID) && @data == other.data
end

#inspectObject



37
38
39
# File 'lib/skyfall/cid.rb', line 37

def inspect
  "CID(\"#{to_s}\")"
end

#to_sObject



33
34
35
# File 'lib/skyfall/cid.rb', line 33

def to_s
  'b' + Base32.encode(@data).downcase.gsub(/=+$/, '')
end