Class: TransmissionSimple::Api
- Inherits:
-
Object
- Object
- TransmissionSimple::Api
- Defined in:
- lib/transmission-simple/api.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#initialize(endpoint) ⇒ Api
constructor
A new instance of Api.
- #method_missing(method, *arguments) ⇒ Object
- #send_request(request_name, options) ⇒ Object
Constructor Details
#initialize(endpoint) ⇒ Api
Returns a new instance of Api.
6 7 8 |
# File 'lib/transmission-simple/api.rb', line 6 def initialize( endpoint ) @endpoint = URI.parse(endpoint) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments) ⇒ Object
46 47 48 |
# File 'lib/transmission-simple/api.rb', line 46 def method_missing(method, *arguments) send_request(transmissionize(method), arguments.shift ) end |
Instance Attribute Details
#endpoint ⇒ Object
Returns the value of attribute endpoint.
3 4 5 |
# File 'lib/transmission-simple/api.rb', line 3 def endpoint @endpoint end |
#session ⇒ Object
Returns the value of attribute session.
4 5 6 |
# File 'lib/transmission-simple/api.rb', line 4 def session @session end |
Instance Method Details
#send_request(request_name, options) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/transmission-simple/api.rb', line 10 def send_request(request_name, ) http = Net::HTTP.new(endpoint.host, endpoint.port) http.use_ssl = ( endpoint.scheme == 'https' ) http.verify_mode = OpenSSL::SSL::VERIFY_NONE # http.set_debug_output STDOUT http.start do |http| request = Net::HTTP::Post.new(endpoint.path, {'X-Transmission-Session-Id' => (session or ''), 'Content-Type' => 'application/json' }) request.body = {:method => request_name}.merge(:arguments => ).to_json request.basic_auth *endpoint.userinfo.split(':') if endpoint.userinfo results = http.request(request) if results.code == '409' @session = results['x-transmission-session-id'] return send_request(request_name, ) end json_results = JSON.parse(results.body) if json_results['result'] && json_results['result'] == 'success' if json_results['arguments'] if json_results['arguments']['torrents'] [].tap do |torrents| json_results['arguments']['torrents'].each {|t| torrents << Torrent.new(t) } end else json_results['arguments'] end else {} end else raise TransmissionSimple::Error.new( json_results['result'] ) end end end |