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.



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

def initialize(stream)
  if stream.kind_of? BEncode::ParseIO
    @stream = stream
  elsif stream.respond_to? :string
    @stream = StringIO.new stream.string
  elsif stream.respond_to? :to_s
    @stream = StringIO.new stream.to_s
  end
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



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

def stream
  @stream
end

Instance Method Details

#eos?Boolean

Returns:

  • (Boolean)


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

def eos?
  stream.eof?
end

#parse!Object



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

def parse!
  case stream.peek
    when ?i then parse_integer!
    when ?l then parse_list!
    when ?d then parse_dict!
    when ?0 .. ?9 then parse_string!
  end
end