Class: BEncode
- Inherits:
-
Object
- Object
- BEncode
- Defined in:
- lib/nrepl-lazuli/bencode.rb
Class Method Summary collapse
Instance Method Summary collapse
- #eos? ⇒ Boolean
-
#initialize(stream) ⇒ BEncode
constructor
A new instance of BEncode.
- #parse! ⇒ Object
Constructor Details
#initialize(stream) ⇒ BEncode
Returns a new instance of BEncode.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/nrepl-lazuli/bencode.rb', line 17 def initialize(stream) @stream = if stream.kind_of?(IO) || stream.kind_of?(StringIO) stream elsif stream.respond_to? :string StringIO.new stream.string elsif stream.respond_to? :to_s StringIO.new stream.to_s end @encoding = @stream.external_encoding || Encoding::default_external end |
Class Method Details
.encode(data) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/nrepl-lazuli/bencode.rb', line 2 def self.encode(data) case data when Integer "i#{data}e" when String "#{data.bytesize}:#{data}" when Array "l#{data.map { |e| encode(e) }.join}e" when Hash "d#{data.map { |k, v| encode(k) + encode(v) }.join}e" else raise ArgumentError, "Cannot BEncode type: #{data.class} on text #{data.inspect}" end end |
Instance Method Details
#eos? ⇒ Boolean
38 39 40 |
# File 'lib/nrepl-lazuli/bencode.rb', line 38 def eos? @stream.eof? end |
#parse! ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/nrepl-lazuli/bencode.rb', line 29 def parse! case peek when ?i then parse_integer! when ?l then parse_list! when ?d then parse_dict! when ?0 .. ?9 then parse_string! end end |