Class: Aria2Driver::JsonRpc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aria2_driver/json_rpc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
# File 'lib/aria2_driver/json_rpc/client.rb', line 16

def initialize(host, options={})
  @id = options[:id] || generate_uuid
  @token = options[:token]
  options.delete :id
  options.delete :token
  @connection = Aria2Driver::JsonRpc::Connection.new host, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/aria2_driver/json_rpc/client.rb', line 43

def method_missing(method, *args)
  if supported_request?(method)
    rpc_method = snake_lower_camel method.to_s
    if args.any?
      request Aria2Driver::JsonRpc::Request.new "aria2.#{rpc_method}", args[0]
    else
      request Aria2Driver::JsonRpc::Request.new "aria2.#{rpc_method}"
    end
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/aria2_driver/json_rpc/client.rb', line 14

def connection
  @connection
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/aria2_driver/json_rpc/client.rb', line 14

def id
  @id
end

#tokenObject (readonly)

Returns the value of attribute token.



14
15
16
# File 'lib/aria2_driver/json_rpc/client.rb', line 14

def token
  @token
end

Instance Method Details

#request(request) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aria2_driver/json_rpc/client.rb', line 24

def request(request)
  req_hash = request_to_hash(request)
  http = Net::HTTP.new(connection.host, connection.port)
  begin
    http_response = http.request_post(
        request.path,
        JSON.generate(req_hash),
        {
            'Accept' => 'application/json',
            'Content-Type' => 'application/json'
        }
    )
    Aria2Driver::JsonRpc::Response.new(JSON.parse(http_response.body))
  rescue Exception => ex
    raise Aria2Driver::JsonRpc::ResponseException.new "Connection Error"
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/aria2_driver/json_rpc/client.rb', line 54

def respond_to_missing?(method, include_private = false)
  supported_request?(method)
end