Class: RTransmission::Torrent

Inherits:
Object
  • Object
show all
Defined in:
lib/rtransmission/torrent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, id) ⇒ Torrent

Returns a new instance of Torrent.



135
136
137
138
# File 'lib/rtransmission/torrent.rb', line 135

def initialize(session, id)
  @session = session
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/rtransmission/torrent.rb', line 11

def id
  @id
end

Class Method Details

.add(session, args = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rtransmission/torrent.rb', line 13

def self.add(session, args = {})
  pargs = {}

  raise RTransmission::Exception.new('Torrent#add: only one of remote_file, url, or torrent must be specified') if (args.keys & [:remote_file, :url, :torrent]).count != 1
  pargs['filename'] = args[:remote_file] if args[:remote_file]
  pargs['filename'] = args[:url] if args[:url]
  pargs['metainfo'] = Base64::encode64(args[:torrent]) if args[:torrent]

  pargs['cookies'] = args[:cookies] if args[:cookies]
  pargs['download-dir'] = args[:download_dir] if args[:download_dir]
  pargs['paused'] = args[:paused] if args[:paused]
  pargs['peer-limit'] = args[:peer_limit] if args[:peer_limit]
  pargs['bandwidthPriority'] = RTransmission::Fields::Priority.map(args[:bandwidth_priority]) if args[:bandwidth_priority]

  request = RTransmission::Request.new('torrent-add', pargs, 'Torrent#add') do |arguments|
    RTransmission::Torrent.new(session, arguments['torrent-added']['id'])
  end

  session.client.call(request)
end

.attribute(rpc_name, args = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rtransmission/torrent.rb', line 47

def self.attribute(rpc_name, args = {})
  name = args[:name] || RTransmission::Client.rpc_name_to_ruby_name(rpc_name)

  self.send :define_method, name.to_s do
    request = RTransmission::Request.new('torrent-get', {'ids' => @id, 'fields' => [rpc_name]}, 'Torrent.' + name.to_s) do |arguments|
      value = arguments['torrents'][0][rpc_name]
      RTransmission::Type.unmap(value, args[:type])
    end

    @session.client.call(request)
  end

  if args[:writeable] == true
    self.send :define_method, name.to_s.gsub('?', '') + '=' do |value|
      rpc_value = RTransmission::Type.map(value, args[:type])
      request = RTransmission::Request.new('torrent-set', {'ids' => @id, rpc_name => rpc_value}, 'Torrent.' + name.to_s + '=') do
        value
      end

      @session.client.call(request)
    end
  end
end

.list(session) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rtransmission/torrent.rb', line 34

def self.list(session)
  request = RTransmission::Request.new('torrent-get', {'fields' => ['id']}, 'Torrent#list') do |arguments|
    torrents = []
    arguments['torrents'].each do |torrent|
      torrents << RTransmission::Torrent.new(session, torrent['id'])
    end

    torrents
  end

  session.client.call(request)
end

Instance Method Details

#move(args = {}) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/rtransmission/torrent.rb', line 164

def move(args = {})
  pargs = {}

  pargs['location'] = args[:location] if args[:location]
  pargs['move'] = args[:move] if args[:move]

  @session.client.call(RTransmission::Request.new('torrent-set-location', {'ids' => @id}.merge(pargs), 'Torrent.move'))
end

#piecesObject



173
174
175
176
177
178
179
180
# File 'lib/rtransmission/torrent.rb', line 173

def pieces
  request = RTransmission::Request.new('torrent-get', {'ids' => @id, 'fields' => ['pieces']}, 'Torrent.pieces') do |arguments|
    pieces = arguments['torrents'][0]['pieces']
    Base64::decode64(pieces).unpack('B*')[0][0 .. piece_count - 1]
  end

  @session.client.call(request)
end

#priorities=(priorities) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rtransmission/torrent.rb', line 182

def priorities=(priorities)
  phigh = []
  pnormal = []
  plow = []
  0.upto(priorities.size - 1) do |i|
    phigh << i if priorities[i] == :high
    pnormal << i if priorities[i] == :normal
    plow << i if priorities[i] == :low
  end

  pargs = {}
  pargs.merge!({'priority-high' => phigh}) if phigh.size != 0
  pargs.merge!({'priority-normal' => pnormal}) if pnormal.size != 0
  pargs.merge!({'priority-low' => plow}) if plow.size != 0

  request = RTransmission::Request.new('torrent-set', {'ids' => @id}.merge(pargs), 'Torrent.files_priorities=') do
    priorities
  end

  @session.client.call(request)
end

#reannounceObject



152
153
154
# File 'lib/rtransmission/torrent.rb', line 152

def reannounce
  @session.client.call(RTransmission::Request.new('torrent-reannounce', {'ids' => @id}, 'Torrent.reannounce'))
end

#remove(args = {}) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/rtransmission/torrent.rb', line 156

def remove(args = {})
  pargs = {}

  pargs['delete-local-data'] = args[:delete_local_data] if args[:delete_local_data]

  @session.client.call(RTransmission::Request.new('torrent-remove', {'ids' => @id}.merge(pargs), 'Torrent.remove'))
end

#startObject



140
141
142
# File 'lib/rtransmission/torrent.rb', line 140

def start
  @session.client.call(RTransmission::Request.new('torrent-start', {'ids' => @id}, 'Torrent.start'))
end

#stopObject



144
145
146
# File 'lib/rtransmission/torrent.rb', line 144

def stop
  @session.client.call(RTransmission::Request.new('torrent-stop', {'ids' => @id}, 'Torrent.stop'))
end

#verifyObject



148
149
150
# File 'lib/rtransmission/torrent.rb', line 148

def verify
  @session.client.call(RTransmission::Request.new('torrent-verify', {'ids' => @id}, 'Torrent.verify'))
end

#wanted=(wanted) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rtransmission/torrent.rb', line 204

def wanted=(wanted)
  pwanted = []
  punwanted = []
  0.upto(wanted.size - 1) do |i|
    pwanted << i if wanted[i]
    punwanted << i unless wanted[i]
  end

  pargs = {}
  pargs.merge!({'files-wanted' => pwanted}) if pwanted.size != 0
  pargs.merge!({'files-unwanted' => punwanted}) if punwanted.size != 0

  request = RTransmission::Request.new('torrent-set', {'ids' => @id}.merge(pargs), 'Torrent.wanted=') do
    wanted
  end

  @session.client.call(request)
end