Class: RubyTorrent::MetaInfo
Class Method Summary collapse
- .from_bstream(bs) ⇒ Object
-
.from_location(fn, http_proxy = ENV["http_proxy"]) ⇒ Object
either a filename or a URL.
- .from_stream(s) ⇒ Object
Instance Method Summary collapse
- #check ⇒ Object
-
#initialize(dict = nil) ⇒ MetaInfo
constructor
A new instance of MetaInfo.
- #method_missing(meth, *args) ⇒ Object
- #multiple? ⇒ Boolean
- #single? ⇒ Boolean
- #to_bencoding ⇒ Object
- #trackers ⇒ Object
Constructor Details
#initialize(dict = nil) ⇒ MetaInfo
Returns a new instance of MetaInfo.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rubytorrent/metainfo.rb', line 133 def initialize(dict=nil) # raise TypeError, "argument must be a Hash (maybe see MetaInfo.from_location)" unless dict.is_a? Hash @s = TypedStruct.new do |s| s.field :info => MetaInfoInfo, :announce => URI::HTTP, :announce_list => Array, :creation_date => Time, :comment => String, :created_by => String, :encoding => String s.label :announce_list => "announce-list", :creation_date => "creation date", :created_by => "created by" s.array :announce_list s.coerce :info => lambda { |x| MetaInfoInfo.new(x) }, :creation_date => lambda { |x| Time.at(x) }, :announce => lambda { |x| URI.parse(x) }, :announce_list => lambda { |x| x.map { |y| y.map { |z| URI.parse(z) } } } 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
195 196 197 |
# File 'lib/rubytorrent/metainfo.rb', line 195 def method_missing(meth, *args) @s.send(meth, *args) end |
Class Method Details
.from_bstream(bs) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rubytorrent/metainfo.rb', line 167 def self.from_bstream(bs) dict = nil bs.each do |e| if dict == nil dict = e else raise MetaInfoFormatError, "too many bencoded elements for metainfo file (just need one)" end end raise MetaInfoFormatError, "bencoded element must be a dictionary, got a #{dict.class}" unless dict.kind_of? ::Hash MetaInfo.new(dict) end |
.from_location(fn, http_proxy = ENV["http_proxy"]) ⇒ Object
either a filename or a URL
183 184 185 186 187 188 189 |
# File 'lib/rubytorrent/metainfo.rb', line 183 def self.from_location(fn, http_proxy=ENV["http_proxy"]) if http_proxy # lame! open(fn, "rb", :proxy => http_proxy) { |f| from_bstream(BStream.new(f)) } else open(fn, "rb") { |f| from_bstream(BStream.new(f)) } end end |
Instance Method Details
#check ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/rubytorrent/metainfo.rb', line 159 def check if @s.announce_list @s.announce_list.each do |tier| tier.each { |track| raise MetaInfoFormatError, "expecting HTTP URL in announce-list, got #{track} instead" unless track.is_a? URI::HTTP } end end end |
#multiple? ⇒ Boolean
157 |
# File 'lib/rubytorrent/metainfo.rb', line 157 def multiple?; info.multiple?; end |
#single? ⇒ Boolean
156 |
# File 'lib/rubytorrent/metainfo.rb', line 156 def single?; info.single?; end |
#to_bencoding ⇒ Object
199 200 201 202 |
# File 'lib/rubytorrent/metainfo.rb', line 199 def to_bencoding check (@dict || @s).to_bencoding end |
#trackers ⇒ Object
204 205 206 207 208 209 210 211 212 |
# File 'lib/rubytorrent/metainfo.rb', line 204 def trackers if announce_list && (announce_list.length > 0) announce_list.map do |tier| tier.extend(ArrayShuffle).shuffle end.flatten else [announce] end end |