Class: TransmissionRSS::Client
Overview
Class for communication with transmission utilizing the RPC web interface.
Defined Under Namespace
Classes: Unauthorized
Instance Method Summary collapse
-
#add_torrent(file, type, options = {}) ⇒ Object
POST json packed torrent add command.
-
#get_session_id ⇒ Object
Get transmission session id.
-
#initialize(server = {}, login = nil, options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(server = {}, login = nil, options = {}) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 20 21 |
# File 'lib/transmission-rss/client.rb', line 13 def initialize(server = {}, login = nil, = {}) @host = server[:host] || 'localhost' @port = server[:port] || 9091 @rpc_path = server[:rpc_path] || '/transmission/rpc' @login = login @timeout = [:timeout] || 5 @log = TransmissionRSS::Log.instance end |
Instance Method Details
#add_torrent(file, type, options = {}) ⇒ Object
POST json packed torrent add command.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/transmission-rss/client.rb', line 24 def add_torrent(file, type, = {}) hash = { 'method' => 'torrent-add', 'arguments' => { 'paused' => [:paused], 'download-dir' => [:download_path] } } case type when :url hash.arguments.filename = file when :file hash.arguments. = Base64.encode64(File.read(file)) else raise ArgumentError.new('type has to be :url or :file.') end sid = get_session_id raise Unauthorized unless sid post = Net::HTTP::Post.new \ @rpc_path, { 'Content-Type' => 'application/json', 'X-Transmission-Session-Id' => sid } auth(post) post.body = hash.to_json response = request(post) result = JSON.parse(response.body).result @log.debug('add_torrent result: ' + result) end |
#get_session_id ⇒ Object
Get transmission session id.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/transmission-rss/client.rb', line 64 def get_session_id get = Net::HTTP::Get.new(@rpc_path) auth(get) response = request(get) id = response.header['x-transmission-session-id'] if id.nil? @log.debug("could not obtain session id (#{response.code}, " + "#{response.class})") else @log.debug('got session id ' + id) end id end |