class RemoTransmission::Remote

Driver for all API calls

Public Class Methods

new(options = {}) click to toggle source

Initialize a remote transmission

Arguments:

options: hash

Options:

:host: host to connect to (String)
:port: port to connect to (Integer)
:user: username to authenticate to (String)
:password: password to authenticate to (String)
:debug: flag to turn on debugging (Integer)
# File lib/remotransmission/remote.rb, line 14
def initialize(options = {})
  defaults = RemoTransmission::DEFAULT_OPTIONS
  options = defaults.merge(options)
  @host = options[:host]
  @port = options[:port]
  @user = options[:user]
  @password = options[:password]
  @debug = options[:debug]
end

Public Instance Methods

add(url) click to toggle source

Add a torrent to the transmission client and returns output.

Arguments:

url: magnet URL or URL of torrent file

Example:

>> transmission.add("magnet://..."")
{ "result" => "success" }
# File lib/remotransmission/remote.rb, line 33
def add(url)
  rpc(
    method: "torrent-add",
    tag: 8,
    arguments: { filename: url }
  )
end
list() click to toggle source

Prints all active torrents

Example:

>> transmission.list
{ "arguments" => { "torrents" => { "leftUntilDone" => ... } }
# File lib/remotransmission/remote.rb, line 46
def list
  rpc(
    method: "torrent-get",
    tag: 4,
    arguments: {
      #fields: ["error","errorString","eta","id","isFinished","leftUntilDone","name","peersGettingFromUs","peersSendingToUs","rateDownload","rateUpload","sizeWhenDone","status","uploadRatio"],
      fields: ["isFinished","leftUntilDone","sizeWhenDone", "name"],
    }
  )
end

Private Instance Methods

rpc(options = {}) click to toggle source
# File lib/remotransmission/remote.rb, line 59
def rpc(options = {})
  uri = URI("http://#{@host}:#{@port}/transmission/rpc")
  response = Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Post.new(uri.path)
    request.body = options.to_json
    request.basic_auth(@user, @password)
    http.request(request)
  end

  json = JSON.parse(response.body)
  $stderr.puts json if @debug
  json
end