class TransmissionApi::Client

Constants

TORRENT_FIELDS

Attributes

basic_auth[RW]
debug_mode[RW]
fields[RW]
session_id[RW]
url[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/transmission_api/client.rb, line 20
def initialize(opts)
  @url = opts[:url]
  @fields = opts[:fields] || TORRENT_FIELDS
  @basic_auth = { :username => opts[:username], :password => opts[:password] } if opts[:username]
  @session_id = "NOT-INITIALIZED"
  @debug_mode = opts[:debug_mode] || false
end

Public Instance Methods

all() click to toggle source
# File lib/transmission_api/client.rb, line 28
def all
  log "get_torrents"

  response =
    post(
      :method => "torrent-get",
      :arguments => {
        :fields => fields
      }
    )

  response["arguments"]["torrents"]
end
create(filename) click to toggle source
# File lib/transmission_api/client.rb, line 57
def create(filename)
  log "add_torrent: #{filename}"

  response =
    post(
      :method => "torrent-add",
      :arguments => {
        :filename => filename
      }
    )

  response["arguments"]["torrent-added"]
end
destroy(id) click to toggle source
# File lib/transmission_api/client.rb, line 71
def destroy(id)
  log "remove_torrent: #{id}"

  response =
    post(
      :method => "torrent-remove",
      :arguments => {
        :ids => [id],
        :"delete-local-data" => true
      }
    )

  response
end
find(id) click to toggle source
# File lib/transmission_api/client.rb, line 42
def find(id)
  log "get_torrent: #{id}"

  response =
    post(
      :method => "torrent-get",
      :arguments => {
        :fields => fields,
        :ids => [id]
      }
    )

  response["arguments"]["torrents"].first
end
http_post(opts) click to toggle source
# File lib/transmission_api/client.rb, line 96
def http_post(opts)
  post_options = {
    :body => opts.to_json,
    :headers => { "x-transmission-session-id" => session_id }
  }
  post_options.merge!( :basic_auth => basic_auth ) if basic_auth

  log "url: #{url}"
  log "post_body:"
  log JSON.parse(post_options[:body]).to_yaml
  log "------------------"

  response = HTTParty.post( url, post_options )

  log_response response

  # retry connection if session_id incorrect
  if( response.code == 409 )
    log "changing session_id"
    @session_id = response.headers["x-transmission-session-id"]
    response = http_post(opts)
  end

  response
end
log(message) click to toggle source
# File lib/transmission_api/client.rb, line 122
def log(message)
  Kernel.puts "[TransmissionApi #{Time.now.strftime( "%F %T" )}] #{message}" if debug_mode
end
log_response(response) click to toggle source
# File lib/transmission_api/client.rb, line 126
def log_response(response)
  body = nil
  begin
    body = JSON.parse(response.body).to_yaml
  rescue
    body = response.body
  end

  headers = response.headers.to_yaml

  log "response.code: #{response.code}"
  log "response.message: #{response.message}"

  log "response.body_raw:"
  log response.body
  log "-----------------"

  log "response.body:"
  log body
  log "-----------------"

  log "response.headers:"
  log headers
  log "------------------"
end
post(opts) click to toggle source
# File lib/transmission_api/client.rb, line 86
def post(opts)
  response_parsed = JSON::parse( http_post(opts).body )

  if response_parsed["result"] != "success"
    raise TransmissionApi::Exception, response_parsed["result"]
  end

  response_parsed
end