Class: RubyTorrent::MetaInfoInfoFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytorrent/metainfo.rb

Instance Method Summary collapse

Constructor Details

#initialize(dict = nil) ⇒ MetaInfoInfoFile

Returns a new instance of MetaInfoInfoFile.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubytorrent/metainfo.rb', line 27

def initialize(dict=nil)
  @s = TypedStruct.new do |s|
    s.field :length => Integer, :md5sum => String, :sha1 => String,
            :path => String
    s.array :path
    s.required :length, :path
  end

  @dict = dict
  unless dict.nil?
    @s.parse dict
    check
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



42
43
44
# File 'lib/rubytorrent/metainfo.rb', line 42

def method_missing(meth, *args)
  @s.send(meth, *args)
end

Instance Method Details

#checkObject



46
47
48
# File 'lib/rubytorrent/metainfo.rb', line 46

def check
  raise MetaInfoFormatError, "invalid file length" unless @s.length >= 0
end

#to_bencodingObject



50
51
52
53
# File 'lib/rubytorrent/metainfo.rb', line 50

def to_bencoding
  check
  (@dict || @s).to_bencoding
end