Class: Aspera::JsonRpc::Client

Inherits:
BlankSlate
  • Object
show all
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. 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."



27
28
29
30
31
32
# File 'lib/aspera/json_rpc/client.rb', line 27

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, &block) ⇒ Object

Dispatch any Ruby method call as a JSON-RPC request



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aspera/json_rpc/client.rb', line 39

def method_missing(method, *args, &block)
  args = args.first if args.size == 1 && args.first.is_a?(Hash)
  data = @api.create('', {
    jsonrpc: VERSION,
    method:  "#{@namespace}#{method}",
    params:  args,
    id:      @request_id += 1
  })
  Aspera.assert_type(data, Hash) { 'response' }
  Aspera.assert(data['jsonrpc'] == VERSION, 'bad version in response')
  Aspera.assert(data.key?('id'), 'missing id in response')
  Aspera.assert(!(data.key?('error') && data.key?('result')), 'both error and response')
  Aspera.assert(
    !data.key?('error') ||
    data['error'].is_a?(Hash) &&
    data['error']['code'].is_a?(Integer) &&
    data['error']['message'].is_a?(String),
    'bad error response'
  )
  return data['result']
end

Instance Method Details

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/aspera/json_rpc/client.rb', line 34

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