Class: BTCompare::TorrentFile

Inherits:
Object
  • Object
show all
Defined in:
lib/btcompare/torrent_file.rb

Overview

Holds data about an individual torrent file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ TorrentFile

Returns a new instance of TorrentFile.

Parameters:

  • filename (String)

    Path to torrent file


27
28
29
30
31
32
33
34
35
# File 'lib/btcompare/torrent_file.rb', line 27

def initialize filename
	@filename = filename
	
	@data = BEncode::load(File.open( filename, 'r' ))

	@contains = @data['info']['name']
	@piece_length = @data['info']['piece length']
	@individual_pieces = nil
end

Instance Attribute Details

#containsObject (readonly)

Contains the filenames of the contents of the torrent.


19
20
21
# File 'lib/btcompare/torrent_file.rb', line 19

def contains
  @contains
end

#dataObject (readonly)

Raw data created parsed from the torrent file


11
12
13
# File 'lib/btcompare/torrent_file.rb', line 11

def data
  @data
end

#filenameObject (readonly)

File name of the torrent file


15
16
17
# File 'lib/btcompare/torrent_file.rb', line 15

def filename
  @filename
end

#piece_lengthObject (readonly)

Integer

Contains the length of a piece


23
24
25
# File 'lib/btcompare/torrent_file.rb', line 23

def piece_length
  @piece_length
end

Instance Method Details

#contains_found?Boolean

Checks to see if contains is found in current directory.

Returns:

  • (Boolean)

    True if found.


69
70
71
# File 'lib/btcompare/torrent_file.rb', line 69

def contains_found?
	File.exist? @contains
end

#individual_piecesObject

Generates individual pieces if not alreaded found. Caches data once created. Immediately returns array if already generated.

Returns:

  • array of individual SHA1 hashes from torrent file


42
43
44
45
46
47
48
49
50
# File 'lib/btcompare/torrent_file.rb', line 42

def individual_pieces
	unless @individual_pieces == nil then
		return @individual_pieces
	end
	
	@individual_pieces = @data['info']['pieces'].unpack( "H*" ).first.scan /.{40}/m

	return @individual_pieces 
end

#invalidate_cacheObject

Invalidates cached data


61
62
63
# File 'lib/btcompare/torrent_file.rb', line 61

def invalidate_cache
	@individual_pieces = nil
end

#piece_countFixnum

Created based on ( length / piece length ).ceil

Returns:

  • (Fixnum)

    Number of peices


55
56
57
# File 'lib/btcompare/torrent_file.rb', line 55

def piece_count 
	return ( @data['info']['length'] / @data['info']['piece length'] ).ceil
end