Class: Aspera::JsonRpc::Client

Inherits:
BasicObject
Defined in:
lib/aspera/json_rpc/client.rb

Overview

JSON-RPC 2.0 client over an Aspera::Rest::Client HTTP endpoint. Methods are dispatched dynamically via method_missing. Based on BasicObject, so that methods of Object are sent as JSON-RPC requests. Example:

client = JsonRpc::Client.new(Rest::Client.new(base_url: 'http://127.0.0.1:33024'))
client.get_info
client.start_transfer(app_id: '...', transfer_spec: {...})

Instance Method Summary collapse

Constructor Details

#initialize(api, namespace = nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api (Rest::Client) —

    Aspera REST object pointing at the JSON-RPC endpoint

  • namespace (String, nil) (defaults to: nil) —

    optional method prefix, e.g. "myns."



55
56
57
58
59
60
# File 'lib/aspera/json_rpc/client.rb', line 55

def initialize(api, namespace = nil)
  super()
  @api        = api
  @namespace  = namespace
  @request_id = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Dispatch any Ruby method call as a JSON-RPC request

Parameters:

  • method (Symbol) —

    JSON-RPC method, without namespace

  • args (Array) —

    Arguments of call: a single Hash is sent as named params, else positional params

Returns:

  • (Object) —

    result of response

Raises:



79
# File 'lib/aspera/json_rpc/client.rb', line 79

def method_missing(method, *args) = JsonRpc.request(@api, "#{@namespace}#{method}", args, @request_id += 1)

Instance Method Details

#inspect ⇒ String

Returns Class and URL of JSON-RPC endpoint.

Returns:

  • (String) —

    Class and URL of JSON-RPC endpoint



63
# File 'lib/aspera/json_rpc/client.rb', line 63

def inspect = "#<#{Client.name} #{@api.base_url}>"

#respond_to_missing?(_sym, _include_private = false) ⇒ false

Ruby checks it before implicit conversions (to_ary, to_str, ...): those are not JSON-RPC methods

Parameters:

  • _sym (Symbol) —

    Method name

  • _include_private (Boolean) (defaults to: false) —

    Also private methods

Returns:

  • (false)


72
# File 'lib/aspera/json_rpc/client.rb', line 72

def respond_to_missing?(_sym, _include_private = false) = false

#to_s ⇒ String

Returns Same as #inspect.

Returns:



66
# File 'lib/aspera/json_rpc/client.rb', line 66

def to_s = inspect