Class: RTransmission::Client
- Inherits:
-
Object
- Object
- RTransmission::Client
- Defined in:
- lib/rtransmission/client.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #call(request) ⇒ Object
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(args = {}) ⇒ Client
37 38 39 40 41 42 43 |
# File 'lib/rtransmission/client.rb', line 37 def initialize(args = {}) @host = args[:host] || 'localhost' @port = args[:port] || 9091 @path = args[:path] || '/transmission/rpc' @user = args[:user] || nil @password = args[:password] || nil end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
9 10 11 |
# File 'lib/rtransmission/client.rb', line 9 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
13 14 15 |
# File 'lib/rtransmission/client.rb', line 13 def password @password end |
#path ⇒ Object
Returns the value of attribute path.
11 12 13 |
# File 'lib/rtransmission/client.rb', line 11 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/rtransmission/client.rb', line 10 def port @port end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
15 16 17 |
# File 'lib/rtransmission/client.rb', line 15 def session_id @session_id end |
#user ⇒ Object
Returns the value of attribute user.
12 13 14 |
# File 'lib/rtransmission/client.rb', line 12 def user @user end |
Class Method Details
.rpc_name_to_ruby_name(rpc_name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rtransmission/client.rb', line 26 def self.rpc_name_to_ruby_name(rpc_name) name = nil if rpc_name.index('-') name = rpc_name.gsub('-', '_') else name = rpc_name.gsub(/[A-Z]/) { |x| '_' + x.downcase } end name end |
.session(args = {}, &block) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/rtransmission/client.rb', line 17 def self.session(args = {}, &block) client = RTransmission::Client.new(args) session = RTransmission::Session.new(client) block.call(session) if block session end |
Instance Method Details
#call(request) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rtransmission/client.rb', line 45 def call(request) req = Net::HTTP::Post.new(@path) req.basic_auth(@user, @password) if @user || @password req['X-Transmission-Session-Id'] = @session_id if @session_id req.body = request.to_json res = Net::HTTP.start(@host, @port) do |http| http.request(req) end if res.code.to_i == 409 @session_id = res['X-Transmission-Session-Id'] return call(request) end return RTransmission::Response.new(res.body, request.response_prefix, request.response_block).parse end |