Class: BEncode::Parser

Inherits:
Object show all
Defined in:
lib/bencode/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bencode/parser.rb', line 7

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

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



5
6
7
# File 'lib/bencode/parser.rb', line 5

def encoding
  @encoding
end

#streamObject (readonly)

Returns the value of attribute stream.



5
6
7
# File 'lib/bencode/parser.rb', line 5

def stream
  @stream
end

Instance Method Details

#eos?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/bencode/parser.rb', line 28

def eos?
  stream.eof?
end

#parse!Object



19
20
21
22
23
24
25
26
# File 'lib/bencode/parser.rb', line 19

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