Class: Aspera::JsonRpcClient

Inherits:
BlankSlate
  • Object
show all
Defined in:
lib/aspera/json_rpc.rb

Overview

a very simple JSON RPC client

Constant Summary collapse

JSON_RPC_VERSION =
'2.0'

Instance Method Summary collapse

Constructor Details

#initialize(api, namespace = nil) ⇒ JsonRpcClient

Returns a new instance of JsonRpcClient.



19
20
21
22
23
24
# File 'lib/aspera/json_rpc.rb', line 19

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aspera/json_rpc.rb', line 30

def method_missing(method, *args, &block)
  args = args.first if args.size == 1 && args.first.is_a?(Hash)
  data = @api.create('', {
    jsonrpc: JSON_RPC_VERSION,
    method:  "#{@namespace}#{method}",
    params:  args,
    id:      @request_id += 1
  })[:data]
  Aspera.assert_type(data, Hash){'response'}
  Aspera.assert(data['jsonrpc'] == JSON_RPC_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)


26
27
28
# File 'lib/aspera/json_rpc.rb', line 26

def respond_to_missing?(sym, include_private = false)
  true
end