Class: I2P::Structure

Inherits:
Object
  • Object
show all
Defined in:
lib/i2p/data/structure.rb

Overview

**I2P data structure.**

Defines a base class for I2P data structures.

Direct Known Subclasses

Certificate, Destination, Key, KeyPair

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(base64) ⇒ Structure

Parses a data structure from the given ‘base64` string.

Parameters:

  • base64 (String, #to_s)

Returns:

Since:

  • 0.1.3



16
17
18
19
20
21
# File 'lib/i2p/data/structure.rb', line 16

def self.parse(base64)
  base64 = base64.dup
  base64.gsub!('~', '/')
  base64.gsub!('-', '+')
  self.read(StringIO.new(base64.unpack('m').first))
end

.read(input) ⇒ Structure

Reads a data structure from the given ‘input` stream.

Parameters:

  • input (IO, StringIO)

Returns:

Raises:

  • (NotImplementedError)

Since:

  • 0.1.3



28
29
30
# File 'lib/i2p/data/structure.rb', line 28

def self.read(input)
  raise NotImplementedError.new("#{self}.read")
end

Instance Method Details

#==(other) ⇒ Boolean

Returns ‘true` if `other` has the same binary string representation as this data structure.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 0.1.3



61
62
63
# File 'lib/i2p/data/structure.rb', line 61

def ==(other)
  to_s == other.to_s
end

#eql?(other) ⇒ Boolean

Returns ‘true` if `other == self` and `other` has the same class as this data structure.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 0.1.3



51
52
53
# File 'lib/i2p/data/structure.rb', line 51

def eql?(other)
  other.is_a?(self.class) && self == other
end

#sizeInteger Also known as: bytesize

Returns the byte size of this data structure.

Returns:

  • (Integer)

Since:

  • 0.1.3



36
37
38
39
40
41
42
# File 'lib/i2p/data/structure.rb', line 36

def size
  if self.class.const_defined?(:BYTESIZE)
    self.class.const_get(:BYTESIZE)
  else
    raise NotImplementedError.new("#{self.class}#size")
  end
end

#to_base64String

Returns the Base64-encoded representation of this data structure.

Returns:

  • (String)

Since:

  • 0.1.3



69
70
71
72
73
74
# File 'lib/i2p/data/structure.rb', line 69

def to_base64
  base64 = [to_s].pack('m').delete("\n")
  base64.gsub!('/', '~')
  base64.gsub!('+', '-')
  base64
end

#to_sString

Returns the binary string representation of this data structure.

Returns:

  • (String)

Raises:

  • (NotImplementedError)

Since:

  • 0.1.3



80
81
82
# File 'lib/i2p/data/structure.rb', line 80

def to_s
  raise NotImplementedError.new("#{self.class}#to_s")
end