Class: Transmission::Model::Torrent

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/transmission/model/torrent.rb', line 9

def attributes
  @attributes
end

#connectorObject

Returns the value of attribute connector.



9
10
11
# File 'lib/transmission/model/torrent.rb', line 9

def connector
  @connector
end

#deletedObject

Returns the value of attribute deleted.



9
10
11
# File 'lib/transmission/model/torrent.rb', line 9

def deleted
  @deleted
end

#idsObject

Returns the value of attribute ids.



9
10
11
# File 'lib/transmission/model/torrent.rb', line 9

def ids
  @ids
end

#torrentsObject

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(options = {})
  rpc = options[:connector] || connector
  body = rpc.add_torrent options[:arguments]
  raise DuplicateTorrentError if body['torrent-duplicate']
  find body['torrent-added']['id'], options
end

.all(options = {}) ⇒ Object



110
111
112
113
114
# File 'lib/transmission/model/torrent.rb', line 110

def all(options = {})
  rpc = options[:connector] || connector
  body = rpc.get_torrent nil, options[:fields]
  Torrent.new body['torrents'], rpc
end

.connectorObject



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, options = {})
  rpc = options[:connector] || connector
  ids = id.is_a?(Array) ? id : [id]
  body = rpc.get_torrent ids, options[: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!(options = {})
  rpc = options[: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!(options = {})
  rpc = options[:connector] || connector
  rpc.stop_torrent nil
end

Instance Method Details

#biggestObject

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.

Returns:

  • (Boolean)


35
36
37
# File 'lib/transmission/extensions/torrent_status.rb', line 35

def completed?
  attributes['percentDone'] >= 1
end

#contentObject

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.

Returns:

  • (Boolean)


15
16
17
# File 'lib/transmission/extensions/torrent_status.rb', line 15

def downloading?
  attributes['status'] == 4
end

#errorObject

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.

Returns:

  • (Boolean)


45
46
47
# File 'lib/transmission/extensions/torrent_status.rb', line 45

def error?
  attributes['error'] != 0
end

#etaObject

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.

Returns:

  • (Boolean)


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

#filesObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


80
81
82
# File 'lib/transmission/extensions/torrent_attributes.rb', line 80

def folder?
  !file?
end

#hashObject

Returns torrent hash.



21
22
23
# File 'lib/transmission/extensions/torrent_attributes.rb', line 21

def hash
  attributes['hashString']
end

#idObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


25
26
27
# File 'lib/transmission/extensions/torrent_status.rb', line 25

def isolated?
  attributes['status'] == 7
end

#lengthObject

Returns torrent size (in bytes).



26
27
28
# File 'lib/transmission/extensions/torrent_attributes.rb', line 26

def length
  attributes['totalSize']
end

#magnetObject

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

Returns:

  • (Boolean)


74
75
76
# File 'lib/transmission/model/torrent.rb', line 74

def multi?
  @ids.size > 1
end

#nameObject

Returns torrent name.



5
6
7
# File 'lib/transmission/extensions/torrent_attributes.rb', line 5

def name
  attributes['name']
end

#pathObject

Returns torrent full download path.



10
11
12
13
# File 'lib/transmission/extensions/torrent_attributes.rb', line 10

def path
  dir = File.expand_path(attributes['downloadDir'])
  File.join(dir, name)
end

#paused?Boolean

Returns true if torrent is paused.

Returns:

  • (Boolean)


5
6
7
# File 'lib/transmission/extensions/torrent_status.rb', line 5

def paused?
  attributes['status'] == 0
end

#peersObject

Returns torrent peers.



108
109
110
# File 'lib/transmission/extensions/torrent_attributes.rb', line 108

def peers
  attributes['peersConnected'].size
end

#peers_leechingObject

Returns torrent leeching peers.



118
119
120
# File 'lib/transmission/extensions/torrent_attributes.rb', line 118

def peers_leeching
  attributes['peersGettingFromUs']
end

#peers_seedingObject

Returns torrent seeding peers.



113
114
115
# File 'lib/transmission/extensions/torrent_attributes.rb', line 113

def peers_seeding
  attributes['peersSendingToUs']
end

#percent_downloadedObject

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_ratioObject

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_uploadedObject

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_etaObject

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_lengthObject

Returns prettified torrent size.



5
6
7
# File 'lib/transmission/extensions/torrent_prettify.rb', line 5

def pretty_length
  pretty_size(length)
end

#pretty_validObject

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.

Returns:

  • (Boolean)


10
11
12
# File 'lib/transmission/extensions/torrent_status.rb', line 10

def queued?
  attributes['status'] == 3
end

#ratioObject

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.

Returns:

  • (Boolean)


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

#smallestObject

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_downloadObject

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_uploadObject

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_jsonObject



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

#trackersObject

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

#validObject

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