Class: Transmission::Connection
- Inherits:
-
Object
- Object
- Transmission::Connection
- Includes:
- Singleton
- Defined in:
- lib/transmission-client/connection.rb,
lib/transmission-client/em-connection.rb
Instance Method Summary collapse
- #build_json(method, attributes = {}) ⇒ Object
- #build_request ⇒ Object
-
#init(host, port) ⇒ Object
include EM::Deferrable.
- #request(method, attributes = {}, &cb) ⇒ Object
- #send(method, attributes = {}, &cb) ⇒ Object
Instance Method Details
#build_json(method, attributes = {}) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/transmission-client/connection.rb', line 60 def build_json(method,attributes = {}) if attributes.length == 0 {'method' => method}.to_json else {'method' => method, 'arguments' => attributes }.to_json end end |
#build_request ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/transmission-client/connection.rb', line 27 def build_request() req = Net::HTTP::Post.new('/transmission/rpc') if @username != "" req.basic_auth @username, @password end if ! @header['x-transmission-session-id'].nil? req.add_field 'x-transmission-session-id', @header['x-transmission-session-id'] end req end |
#init(host, port) ⇒ Object
include EM::Deferrable
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/transmission-client/em-connection.rb', line 6 def init(host, port, username = "", password = "") @host = host @port = port @username = username unless username == "" @password = password unless password == "" @header = {} uri = URI.parse("http://#{@host}:#{@port}") Net::HTTP.start(uri.host, uri.port) do |http| @conn = http; resp = http.request(build_request(), build_json('session_get')) if resp.class == Net::HTTPUnauthorized raise SecurityError, 'The client was not able to authenticate, is your username or password wrong?' elsif resp.class == Net::HTTPConflict && @header['x-transmission-session-id'].nil? @header['x-transmission-session-id'] = resp['x-transmission-session-id'] elsif resp.class == Net::HTTPOK true else false end end end |
#request(method, attributes = {}, &cb) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/transmission-client/connection.rb', line 38 def request(method, attributes={}) res = @conn.request(build_request, build_json(method, attributes)) if res.class == Net::HTTPConflict && @header['x-transmission-session-id'].nil? @header['x-transmission-session-id'] = res['x-transmission-session-id'] request(method,attributes) elsif res.class == Net::HTTPOK resp = JSON.parse(res.body) if resp["result"] == 'success' resp['arguments'] else resp end else raise RuntimeError, 'Reponse received from the daemon was something transmission-client cant deal with! Panic!' end end |
#send(method, attributes = {}, &cb) ⇒ Object
55 56 57 58 |
# File 'lib/transmission-client/connection.rb', line 55 def send(method, attributes={}) data = request(method, attributes)['result'] data.nil? end |