Class: I2P::Structure
- Inherits:
-
Object
- Object
- I2P::Structure
- Defined in:
- lib/i2p/data/structure.rb
Overview
**I2P data structure.**
Defines a base class for I2P data structures.
Direct Known Subclasses
Class Method Summary collapse
-
.parse(base64) ⇒ Structure
Parses a data structure from the given ‘base64` string.
-
.read(input) ⇒ Structure
Reads a data structure from the given ‘input` stream.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns ‘true` if `other` has the same binary string representation as this data structure.
-
#eql?(other) ⇒ Boolean
Returns ‘true` if `other == self` and `other` has the same class as this data structure.
-
#size ⇒ Integer
(also: #bytesize)
Returns the byte size of this data structure.
-
#to_base64 ⇒ String
Returns the Base64-encoded representation of this data structure.
-
#to_s ⇒ String
Returns the binary string representation of this data structure.
Class Method Details
.parse(base64) ⇒ Structure
Parses a data structure from the given ‘base64` string.
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.
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.
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.
51 52 53 |
# File 'lib/i2p/data/structure.rb', line 51 def eql?(other) other.is_a?(self.class) && self == other end |
#size ⇒ Integer Also known as: bytesize
Returns the byte size of this data structure.
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_base64 ⇒ String
Returns the Base64-encoded representation of this data structure.
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_s ⇒ String
Returns the binary string representation of this data structure.
80 81 82 |
# File 'lib/i2p/data/structure.rb', line 80 def to_s raise NotImplementedError.new("#{self.class}#to_s") end |