Class: Transmission::Model::Torrent
- Inherits:
-
Object
- Object
- Transmission::Model::Torrent
- Defined in:
- lib/transmission/model/torrent.rb,
lib/transmission/extensions/torrent_status.rb,
lib/transmission/extensions/torrent_actions.rb,
lib/transmission/extensions/torrent_prettify.rb,
lib/transmission/extensions/torrent_attributes.rb
Defined Under Namespace
Classes: DuplicateTorrentError, MissingAttributesError, TorrentError, TorrentNotFoundError
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#connector ⇒ Object
Returns the value of attribute connector.
-
#deleted ⇒ Object
Returns the value of attribute deleted.
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#torrents ⇒ Object
Returns the value of attribute torrents.
Class Method Summary collapse
- .add(options = {}) ⇒ Object
- .all(options = {}) ⇒ Object
- .connector ⇒ Object
- .find(id, options = {}) ⇒ Object
- .start_all!(options = {}) ⇒ Object
- .stop_all!(options = {}) ⇒ Object
Instance Method Summary collapse
-
#biggest ⇒ Object
Returns torrent biggest file and size.
-
#completed? ⇒ Boolean
Returns true if torrent is completed.
-
#content ⇒ Object
Returns torrent files names.
- #delete!(remove_local_data = false) ⇒ Object
-
#downloading? ⇒ Boolean
Returns true if torrent is downloading.
-
#error ⇒ Object
Returns torrent error string.
-
#error? ⇒ Boolean
Returns true if torrent has errors.
-
#eta ⇒ Object
Returns torrent eta (in seconds).
-
#file? ⇒ Boolean
Returns true if torrent is a single file.
-
#files ⇒ Object
Returns all torrent files full path.
-
#finished? ⇒ Boolean
Returns true if torrent is finished.
-
#folder? ⇒ Boolean
Returns true if torrent is a folder.
-
#hash ⇒ Object
Returns torrent hash.
-
#id ⇒ Object
Returns torrent id.
-
#incomplete? ⇒ Boolean
Returns true if torrent is not completed.
-
#initialize(torrent_object, connector) ⇒ Torrent
constructor
A new instance of Torrent.
-
#isolated? ⇒ Boolean
Returns true if torrent is isolated.
-
#length ⇒ Object
Returns torrent size (in bytes).
-
#magnet ⇒ Object
Returns torrent magnet link.
- #method_missing(symbol, *args) ⇒ Object
- #move_bottom! ⇒ Object
- #move_down! ⇒ Object
- #move_top! ⇒ Object
- #move_up! ⇒ Object
- #multi? ⇒ Boolean
-
#name ⇒ Object
Returns torrent name.
-
#path ⇒ Object
Returns torrent full download path.
-
#paused? ⇒ Boolean
Returns true if torrent is paused.
-
#peers ⇒ Object
Returns torrent peers.
-
#peers_leeching ⇒ Object
Returns torrent leeching peers.
-
#peers_seeding ⇒ Object
Returns torrent seeding peers.
-
#percent_downloaded ⇒ Object
Returns torrent downloaded files percent.
-
#percent_ratio ⇒ Object
Returns torrent ratio percent if session ratio is set.
-
#percent_uploaded ⇒ Object
Returns torrent uploaded files percent.
-
#pretty_eta ⇒ Object
Returns prettified eta.
-
#pretty_length ⇒ Object
Returns prettified torrent size.
-
#pretty_valid ⇒ Object
Returns prettified downloaded files size.
-
#queued? ⇒ Boolean
Returns true if torrent is queued.
-
#ratio ⇒ Object
Returns torrent ratio.
- #re_announce! ⇒ Object
- #reload! ⇒ Object
-
#remove! ⇒ Object
Removes torrent.
-
#remove_data! ⇒ Object
Removes torrent and trashes data files.
- #save! ⇒ Object
-
#seeding? ⇒ Boolean
Returns true if torrent is seeding.
- #set_location(new_location, move = false) ⇒ Object
-
#smallest ⇒ Object
Returns torrent smallest file and size.
-
#speed_download ⇒ Object
Returns torrent download speed (in bytes).
-
#speed_upload ⇒ Object
Returns torrent upload speed (in bytes).
- #start! ⇒ Object
- #start_now! ⇒ Object
- #stop! ⇒ Object
- #to_json ⇒ Object
-
#trackers ⇒ Object
Returns torrent trackers.
-
#valid ⇒ Object
Returns torrent downloaded files size (in bytes).
- #verify! ⇒ Object
Constructor Details
#initialize(torrent_object, connector) ⇒ Torrent
Returns a new instance of Torrent.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/transmission/model/torrent.rb', line 11 def initialize(torrent_object, connector) if torrent_object.is_a? Array is_single = torrent_object.size == 1 @attributes = is_single ? torrent_object.first : {} @ids = is_single ? [@attributes['id'].to_i] : [] @torrents = torrent_object.inject([]) do |torrents, torrent| @ids << torrent['id'].to_i torrents << Torrent.new([torrent], connector) end unless is_single end @connector = connector end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/transmission/model/torrent.rb', line 96 def method_missing(symbol, *args) string = symbol.to_s if string[-1] == '=' string = string[0..-2] key = Transmission::Arguments::TorrentSet.real_key string return @attributes[key] = args.first unless key.nil? else key = Transmission::Fields::TorrentGet.real_key string return @attributes[key] unless key.nil? end super end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
9 10 11 |
# File 'lib/transmission/model/torrent.rb', line 9 def attributes @attributes end |
#connector ⇒ Object
Returns the value of attribute connector.
9 10 11 |
# File 'lib/transmission/model/torrent.rb', line 9 def connector @connector end |
#deleted ⇒ Object
Returns the value of attribute deleted.
9 10 11 |
# File 'lib/transmission/model/torrent.rb', line 9 def deleted @deleted end |
#ids ⇒ Object
Returns the value of attribute ids.
9 10 11 |
# File 'lib/transmission/model/torrent.rb', line 9 def ids @ids end |
#torrents ⇒ Object
Returns the value of attribute torrents.
9 10 11 |
# File 'lib/transmission/model/torrent.rb', line 9 def torrents @torrents end |
Class Method Details
.add(options = {}) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/transmission/model/torrent.rb', line 124 def add( = {}) rpc = [:connector] || connector body = rpc.add_torrent [:arguments] raise DuplicateTorrentError if body['torrent-duplicate'] find body['torrent-added']['id'], end |
.all(options = {}) ⇒ Object
110 111 112 113 114 |
# File 'lib/transmission/model/torrent.rb', line 110 def all( = {}) rpc = [:connector] || connector body = rpc.get_torrent nil, [:fields] Torrent.new body['torrents'], rpc end |
.connector ⇒ Object
141 142 143 |
# File 'lib/transmission/model/torrent.rb', line 141 def connector Transmission::Config.get_connector end |
.find(id, options = {}) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/transmission/model/torrent.rb', line 116 def find(id, = {}) rpc = [:connector] || connector ids = id.is_a?(Array) ? id : [id] body = rpc.get_torrent ids, [:fields] raise TorrentNotFoundError if body['torrents'].size.zero? Torrent.new body['torrents'], rpc end |
.start_all!(options = {}) ⇒ Object
131 132 133 134 |
# File 'lib/transmission/model/torrent.rb', line 131 def start_all!( = {}) rpc = [:connector] || connector rpc.start_torrent nil end |
.stop_all!(options = {}) ⇒ Object
136 137 138 139 |
# File 'lib/transmission/model/torrent.rb', line 136 def stop_all!( = {}) rpc = [:connector] || connector rpc.stop_torrent nil end |
Instance Method Details
#biggest ⇒ Object
Returns torrent biggest file and size.
96 97 98 99 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 96 def biggest b = attributes['files'].max_by { |f| f['length'] } [File.basename(b['name']), b['length']] end |
#completed? ⇒ Boolean
Returns true if torrent is completed.
35 36 37 |
# File 'lib/transmission/extensions/torrent_status.rb', line 35 def completed? attributes['percentDone'] >= 1 end |
#content ⇒ Object
Returns torrent files names.
91 92 93 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 91 def content files.map { |f| File.basename(f) } end |
#delete!(remove_local_data = false) ⇒ Object
24 25 26 27 |
# File 'lib/transmission/model/torrent.rb', line 24 def delete!(remove_local_data = false) connector.remove_torrent @ids, remove_local_data @deleted = true end |
#downloading? ⇒ Boolean
Returns true if torrent is downloading.
15 16 17 |
# File 'lib/transmission/extensions/torrent_status.rb', line 15 def downloading? attributes['status'] == 4 end |
#error ⇒ Object
Returns torrent error string.
123 124 125 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 123 def error attributes['errorString'] if error? end |
#error? ⇒ Boolean
Returns true if torrent has errors.
45 46 47 |
# File 'lib/transmission/extensions/torrent_status.rb', line 45 def error? attributes['error'] != 0 end |
#eta ⇒ Object
Returns torrent eta (in seconds).
68 69 70 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 68 def eta attributes['eta'] end |
#file? ⇒ Boolean
Returns true if torrent is a single file.
73 74 75 76 77 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 73 def file? name = attributes['name'] files = attributes['files'] files.size == 1 && files.first['name'] == name end |
#files ⇒ Object
Returns all torrent files full path.
85 86 87 88 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 85 def files dir = File.absolute_path(attributes['downloadDir']) attributes['files'].map { |f| File.join(dir, f['name']) } end |
#finished? ⇒ Boolean
Returns true if torrent is finished.
30 31 32 |
# File 'lib/transmission/extensions/torrent_status.rb', line 30 def finished? attributes['isFinished'] end |
#folder? ⇒ Boolean
Returns true if torrent is a folder.
80 81 82 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 80 def folder? !file? end |
#hash ⇒ Object
Returns torrent hash.
21 22 23 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 21 def hash attributes['hashString'] end |
#id ⇒ Object
Returns torrent id.
16 17 18 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 16 def id attributes['id'] end |
#incomplete? ⇒ Boolean
Returns true if torrent is not completed.
40 41 42 |
# File 'lib/transmission/extensions/torrent_status.rb', line 40 def incomplete? attributes['percentDone'] < 1 end |
#isolated? ⇒ Boolean
Returns true if torrent is isolated.
25 26 27 |
# File 'lib/transmission/extensions/torrent_status.rb', line 25 def isolated? attributes['status'] == 7 end |
#length ⇒ Object
Returns torrent size (in bytes).
26 27 28 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 26 def length attributes['totalSize'] end |
#magnet ⇒ Object
Returns torrent magnet link.
128 129 130 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 128 def magnet attributes['magnetLink'] end |
#move_bottom! ⇒ Object
46 47 48 |
# File 'lib/transmission/model/torrent.rb', line 46 def move_bottom! connector.move_bottom_torrent @ids end |
#move_down! ⇒ Object
38 39 40 |
# File 'lib/transmission/model/torrent.rb', line 38 def move_down! connector.move_down_torrent @ids end |
#move_top! ⇒ Object
42 43 44 |
# File 'lib/transmission/model/torrent.rb', line 42 def move_top! connector.move_top_torrent @ids end |
#move_up! ⇒ Object
34 35 36 |
# File 'lib/transmission/model/torrent.rb', line 34 def move_up! connector.move_up_torrent @ids end |
#multi? ⇒ Boolean
74 75 76 |
# File 'lib/transmission/model/torrent.rb', line 74 def multi? @ids.size > 1 end |
#name ⇒ Object
Returns torrent name.
5 6 7 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 5 def name attributes['name'] end |
#path ⇒ Object
Returns torrent full download path.
10 11 12 13 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 10 def path dir = File.(attributes['downloadDir']) File.join(dir, name) end |
#paused? ⇒ Boolean
Returns true if torrent is paused.
5 6 7 |
# File 'lib/transmission/extensions/torrent_status.rb', line 5 def paused? attributes['status'] == 0 end |
#peers ⇒ Object
Returns torrent peers.
108 109 110 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 108 def peers attributes['peersConnected'].size end |
#peers_leeching ⇒ Object
Returns torrent leeching peers.
118 119 120 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 118 def peers_leeching attributes['peersGettingFromUs'] end |
#peers_seeding ⇒ Object
Returns torrent seeding peers.
113 114 115 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 113 def peers_seeding attributes['peersSendingToUs'] end |
#percent_downloaded ⇒ Object
Returns torrent downloaded files percent.
36 37 38 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 36 def percent_downloaded (attributes['haveValid'] * 1.0 / attributes['totalSize']).round(2) end |
#percent_ratio ⇒ Object
Returns torrent ratio percent if session ratio is set.
46 47 48 49 50 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 46 def percent_ratio session = Transmission::Model::Session.get return unless session.seed_ratio_limited (attributes['uploadRatio'] * 1.0 / session.seed_ratio_limit).round(2) end |
#percent_uploaded ⇒ Object
Returns torrent uploaded files percent.
41 42 43 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 41 def percent_uploaded (attributes['uploadedEver'] * 1.0 / attributes['totalSize']).round(2) end |
#pretty_eta ⇒ Object
Returns prettified eta.
15 16 17 |
# File 'lib/transmission/extensions/torrent_prettify.rb', line 15 def pretty_eta pretty_time(attributes['eta']) if attributes['eta'] > 0 end |
#pretty_length ⇒ Object
Returns prettified torrent size.
5 6 7 |
# File 'lib/transmission/extensions/torrent_prettify.rb', line 5 def pretty_length pretty_size(length) end |
#pretty_valid ⇒ Object
Returns prettified downloaded files size.
10 11 12 |
# File 'lib/transmission/extensions/torrent_prettify.rb', line 10 def pretty_valid pretty_size(valid) end |
#queued? ⇒ Boolean
Returns true if torrent is queued.
10 11 12 |
# File 'lib/transmission/extensions/torrent_status.rb', line 10 def queued? attributes['status'] == 3 end |
#ratio ⇒ Object
Returns torrent ratio.
63 64 65 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 63 def ratio (attributes['uploadRatio'] * 1.0).round(2) end |
#re_announce! ⇒ Object
66 67 68 |
# File 'lib/transmission/model/torrent.rb', line 66 def re_announce! connector.re_announce_torrent @ids end |
#reload! ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/transmission/model/torrent.rb', line 78 def reload! torrents = Torrent.find @ids, connector: @connector @ids = torrents.ids @attributes = torrents.attributes @torrents = torrents.torrents self end |
#remove! ⇒ Object
Removes torrent.
5 6 7 8 |
# File 'lib/transmission/extensions/torrent_actions.rb', line 5 def remove! connector.remove_torrent @ids @deleted = true end |
#remove_data! ⇒ Object
Removes torrent and trashes data files.
11 12 13 14 |
# File 'lib/transmission/extensions/torrent_actions.rb', line 11 def remove_data! connector.remove_torrent @ids, true @deleted = true end |
#save! ⇒ Object
29 30 31 32 |
# File 'lib/transmission/model/torrent.rb', line 29 def save! filtered = Transmission::Arguments::TorrentSet.filter @attributes connector.set_torrent @ids, filtered end |
#seeding? ⇒ Boolean
Returns true if torrent is seeding.
20 21 22 |
# File 'lib/transmission/extensions/torrent_status.rb', line 20 def seeding? attributes['status'] == 6 end |
#set_location(new_location, move = false) ⇒ Object
70 71 72 |
# File 'lib/transmission/model/torrent.rb', line 70 def set_location(new_location, move = false) connector.torrent_set_location @ids, location: new_location, move: move end |
#smallest ⇒ Object
Returns torrent smallest file and size.
102 103 104 105 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 102 def smallest b = attributes['files'].min_by { |f| f['length'] } [File.basename(b['name']), b['length']] end |
#speed_download ⇒ Object
Returns torrent download speed (in bytes).
53 54 55 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 53 def speed_download attributes['rateDownload'] end |
#speed_upload ⇒ Object
Returns torrent upload speed (in bytes).
58 59 60 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 58 def speed_upload attributes['rateUpload'] end |
#start! ⇒ Object
50 51 52 |
# File 'lib/transmission/model/torrent.rb', line 50 def start! connector.start_torrent @ids end |
#start_now! ⇒ Object
54 55 56 |
# File 'lib/transmission/model/torrent.rb', line 54 def start_now! connector.start_torrent_now @ids end |
#stop! ⇒ Object
58 59 60 |
# File 'lib/transmission/model/torrent.rb', line 58 def stop! connector.stop_torrent @ids end |
#to_json ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/transmission/model/torrent.rb', line 86 def to_json if multi? @torrents.inject([]) do |torrents, torrent| torrents << torrent.to_json end else @attributes end end |
#trackers ⇒ Object
Returns torrent trackers.
133 134 135 136 137 138 139 140 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 133 def trackers url_regex = /([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b)(?:[-a-zA-Z0-9@:%_\+.~#?&\/=]*)/i attributes['trackers'].map do |r| if (a = r['announce'].match(url_regex)) a[1] end end.compact end |
#valid ⇒ Object
Returns torrent downloaded files size (in bytes).
31 32 33 |
# File 'lib/transmission/extensions/torrent_attributes.rb', line 31 def valid attributes['haveValid'] end |
#verify! ⇒ Object
62 63 64 |
# File 'lib/transmission/model/torrent.rb', line 62 def verify! connector.verify_torrent @ids end |