Class: JsonRpc

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonrpc/jsonrpc.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ JsonRpc

Returns a new instance of JsonRpc.



11
12
13
14
# File 'lib/jsonrpc/jsonrpc.rb', line 11

def initialize( uri )
  @uri        = uri    ## assume uri always as string for now
  @request_id = 1
end

Instance Method Details

#request(method, params = []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jsonrpc/jsonrpc.rb', line 17

def request( method, params=[] )

    data = { jsonrpc: '2.0',
             method:  method,
             params:  params,
             id: @request_id }

    @request_id += 1

    puts "json_rpc POST payload:"
    puts data.to_json

    response = Webclient.post( @uri, json: data )


    if response.status.nok?
      raise "Error code #{response.status.code} on request #{@uri} #{data}"
    end

    puts "json_rpc response.body:"
    puts response.body


    body = JSON.parse( response.body, max_nesting: 1500 )

    if body['result']
      body['result']
    elsif body['error']
      raise "Error #{@uri} #{body['error']} on request #{@uri} #{data}"
    else
      raise "No response on request #{@uri} #{data}"
    end
end