class Transmission::RPC::Connector
Attributes
credentials[RW]
host[RW]
path[RW]
port[RW]
response[RW]
session_id[RW]
ssl[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/transmission/rpc/connector.rb, line 12 def initialize(options = {}) @host = options[:host] || 'localhost' @port = options[:port] || 9091 @ssl = options[:ssl] || false @credentials = options[:credentials] || nil @path = options[:path] || '/transmission/rpc' @session_id = options[:session_id] || '' end
Public Instance Methods
post(params = {})
click to toggle source
# File lib/transmission/rpc/connector.rb, line 21 def post(params = {}) response = connection.post do |req| req.url @path req.headers['X-Transmission-Session-Id'] = @session_id req.headers['Content-Type'] = 'application/json' req.body = JSON.generate(params) end handle_response response, params end
Private Instance Methods
connection()
click to toggle source
# File lib/transmission/rpc/connector.rb, line 51 def connection @connection ||= begin connection = Faraday.new(url: "#{scheme}://#{@host}:#{@port}", ssl: { verify: false }) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter end connection.basic_auth(@credentials[:username], @credentials[:password]) if @credentials connection end end
handle_response(response, params)
click to toggle source
# File lib/transmission/rpc/connector.rb, line 39 def handle_response(response, params) @response = response if response.status == 409 @session_id = response.headers['x-transmission-session-id'] return post(params) end body = json_body response raise AuthError if response.status == 401 raise ConnectionError, body['result'] unless response.status == 200 && body['result'] == 'success' body['arguments'] end
json_body(response)
click to toggle source
# File lib/transmission/rpc/connector.rb, line 33 def json_body(response) JSON.parse response.body rescue {} end
scheme()
click to toggle source
# File lib/transmission/rpc/connector.rb, line 62 def scheme @ssl ? 'https' : 'http' end