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
# 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
end

Instance Attribute Details

#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)


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 peek
    when ?i then parse_integer!
    when ?l then parse_list!
    when ?d then parse_dict!
    when ?0 .. ?9 then parse_string!
  end
end