Class: QuartzTorrent::MagnetURI
- Inherits:
-
Object
- Object
- QuartzTorrent::MagnetURI
- Defined in:
- lib/quartz_torrent/magnet.rb
Constant Summary collapse
- @@regex =
/magnet:\?(.*)/
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
-
.encodeFromMetainfo(metainfo) ⇒ Object
Create a magnet URI string given the metainfo from a torrent file.
-
.magnetURI?(str) ⇒ Boolean
Returns true if the passed string is a magnet URI, false otherwise.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the value of the specified key from the magnet URI.
-
#btInfoHash ⇒ Object
Return the first Bittorrent info hash found in the magnet URI.
-
#displayName ⇒ Object
Return the first display name found in the magnet link.
-
#initialize(str) ⇒ MagnetURI
constructor
Create a new MagnetURI object given a magnet URI string.
-
#tracker ⇒ Object
Return the first tracker URL found in the magnet link.
-
#trackers ⇒ Object
Return an array of all tracker URLS in the magnet link.
Constructor Details
#initialize(str) ⇒ MagnetURI
Create a new MagnetURI object given a magnet URI string.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/quartz_torrent/magnet.rb', line 10 def initialize(str) @params = {} @raw = str if str =~ @@regex parseQuery $1 else raise "Not a magnet URI" end end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
21 22 23 |
# File 'lib/quartz_torrent/magnet.rb', line 21 def raw @raw end |
Class Method Details
.encodeFromMetainfo(metainfo) ⇒ Object
Create a magnet URI string given the metainfo from a torrent file.
84 85 86 87 88 89 |
# File 'lib/quartz_torrent/magnet.rb', line 84 def self.encodeFromMetainfo() s = "magnet:?xt=urn:btih:" s << .infoHash.unpack("H*").first s << "&tr=" s << .announce end |
.magnetURI?(str) ⇒ Boolean
Returns true if the passed string is a magnet URI, false otherwise.
24 25 26 |
# File 'lib/quartz_torrent/magnet.rb', line 24 def self.magnetURI?(str) str =~ @@regex end |
Instance Method Details
#[](key) ⇒ Object
Return the value of the specified key from the magnet URI.
29 30 31 |
# File 'lib/quartz_torrent/magnet.rb', line 29 def [](key) @params[key] end |
#btInfoHash ⇒ Object
Return the first Bittorrent info hash found in the magnet URI. The returned info hash is in binary format.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/quartz_torrent/magnet.rb', line 35 def btInfoHash result = nil @params['xt'].each do |topic| if topic =~ /urn:btih:(.*)/ hash = $1 if hash.length == 40 # Hex-encoded info hash. Convert to binary. result = [hash].pack "H*" else # Base32 encoded result = Base32.decode hash end break end end result end |
#displayName ⇒ Object
Return the first display name found in the magnet link. Returns nil if the magnet has no display name.
74 75 76 77 78 79 80 81 |
# File 'lib/quartz_torrent/magnet.rb', line 74 def displayName dn = @params['dn'] if dn dn.first else nil end end |
#tracker ⇒ Object
Return the first tracker URL found in the magnet link. Returns nil if the magnet has no tracker info.
54 55 56 57 58 59 60 61 |
# File 'lib/quartz_torrent/magnet.rb', line 54 def tracker tr = @params['tr'] if tr tr.first else nil end end |
#trackers ⇒ Object
Return an array of all tracker URLS in the magnet link.
64 65 66 67 68 69 70 71 |
# File 'lib/quartz_torrent/magnet.rb', line 64 def trackers tr = @params['tr'] if tr tr else [] end end |