lib/transmission/model/torrent.rb in transmission-rpc-ruby-0.2.1 vs lib/transmission/model/torrent.rb in transmission-rpc-ruby-0.3.0
- old
+ new
@@ -4,69 +4,94 @@
class TorrentError < StandardError; end
class TorrentNotFoundError < StandardError; end
class MissingAttributesError < StandardError; end
class DuplicateTorrentError < StandardError; end
- attr_accessor :attributes, :deleted, :connector
+ attr_accessor :attributes, :deleted, :connector, :torrents, :ids
def initialize(torrent_object, connector)
- @attributes = torrent_object
+ 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
def delete!(remove_local_data = false)
- connector.remove_torrent [self.id], remove_local_data
+ connector.remove_torrent @ids, remove_local_data
@deleted = true
end
def save!
filtered = Transmission::Arguments::TorrentSet.filter @attributes
- connector.set_torrent [self.id], filtered
+ connector.set_torrent @ids, filtered
end
def move_up!
- connector.move_up_torrent [self.id]
+ connector.move_up_torrent @ids
end
def move_down!
- connector.move_down_torrent [self.id]
+ connector.move_down_torrent @ids
end
def move_top!
- connector.move_top_torrent [self.id]
+ connector.move_top_torrent @ids
end
def move_bottom!
- connector.move_bottom_torrent [self.id]
+ connector.move_bottom_torrent @ids
end
def start!
- connector.start_torrent [self.id]
+ connector.start_torrent @ids
end
def start_now!
- connector.start_torrent_now [self.id]
+ connector.start_torrent_now @ids
end
def stop!
- connector.stop_torrent [self.id]
+ connector.stop_torrent @ids
end
def verify!
- connector.verify_torrent [self.id]
+ connector.verify_torrent @ids
end
def re_announce!
- connector.re_announce_torrent [self.id]
+ connector.re_announce_torrent @ids
end
+ def is_multi?
+ @ids.size > 1
+ end
+
def finished?
+ self.percent_done == 1
+ end
+ def reload!
+ torrents = Torrent.find @ids, connector: @connector
+ @ids = torrents.ids
+ @attributes = torrents.attributes
+ @torrents = torrents.torrents
end
def to_json
-
+ if is_multi?
+ @torrents.inject([]) do |torrents, torrent|
+ torrents << torrent.to_json
+ end
+ else
+ @attributes
+ end
end
def method_missing(symbol, *args)
string = symbol.to_s
if string[-1] == '='
@@ -82,26 +107,35 @@
class << self
def all(options = {})
rpc = options[:connector] || connector
body = rpc.get_torrent nil, options[:fields]
- body['torrents'].inject([]) do |torrents, torrent|
- torrents << Torrent.new(torrent, rpc)
- end
+ Torrent.new body['torrents'], rpc
end
def find(id, options = {})
rpc = options[:connector] || connector
- body = rpc.get_torrent [id], options[:fields]
+ ids = id.is_a?(Array) ? id : [id]
+ body = rpc.get_torrent ids, options[:fields]
raise TorrentNotFoundError if body['torrents'].size == 0
- Torrent.new body['torrents'].first, rpc
+ Torrent.new body['torrents'], rpc
end
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
+
+ def start_all!(options = {})
+ rpc = options[:connector] || connector
+ rpc.start_torrent nil
+ end
+
+ def stop_all!(options = {})
+ rpc = options[:connector] || connector
+ rpc.stop_torrent nil
end
def connector
Transmission::Config.get_connector
end
\ No newline at end of file