class P3::Transmission::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/p3-transmission/client.rb, line 41 def initialize(opts) @url = opts[:url] || "http://#{opts[:host]}:#{opts[:port]}/transmission/rpc" @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/p3-transmission/client.rb, line 49 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/p3-transmission/client.rb, line 120 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
remove the torrent and delete the file
# File lib/p3-transmission/client.rb, line 151 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/p3-transmission/client.rb, line 63 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/p3-transmission/client.rb, line 176 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/p3-transmission/client.rb, line 202 def log(message) Kernel.puts "[P3::Transmission #{Time.now.strftime( "%F %T" )}] #{message}" if debug_mode end
log_response(response)
click to toggle source
# File lib/p3-transmission/client.rb, line 206 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
pause(id)
click to toggle source
# File lib/p3-transmission/client.rb, line 99 def pause(id) log "pause_torrent: #{id}" response = post( :method => "torrent-stop", :arguments => { :ids => [id] } ) end
pause_all()
click to toggle source
# File lib/p3-transmission/client.rb, line 111 def pause_all() log "pause_all_torrents" response = post( :method => "torrent-stop", ) end
post(opts)
click to toggle source
# File lib/p3-transmission/client.rb, line 166 def post(opts) response_parsed = JSON::parse( http_post(opts).body ) if response_parsed["result"] != "success" raise Transmission::Exception, response_parsed["result"] end response_parsed end
remove(id)
click to toggle source
remove the torrent but keep the file
# File lib/p3-transmission/client.rb, line 135 def remove(id) log "remove_torrent: #{id}" response = post( :method => "torrent-remove", :arguments => { :ids => [id], :"delete-local-data" => false } ) response end
resume(id)
click to toggle source
# File lib/p3-transmission/client.rb, line 78 def resume(id) log "resume_torrent: #{id}" response = post( :method => "torrent-start", :arguments => { :ids => [id] } ) end
resume_all()
click to toggle source
# File lib/p3-transmission/client.rb, line 90 def resume_all() log "resume_all_torrents" response = post( :method => "torrent-start", ) end