Class: JsonRpc
- Inherits:
-
Object
- Object
- JsonRpc
- Defined in:
- lib/jsonrpc/jsonrpc.rb
Class Method Summary collapse
- .debug(value) ⇒ Object
-
.debug? ⇒ Boolean
add a global debug switch - why? why not?.
Instance Method Summary collapse
-
#debug? ⇒ Boolean
private helpers.
-
#initialize(uri) ⇒ JsonRpc
constructor
A new instance of JsonRpc.
- #request(method, params = []) ⇒ Object
Constructor Details
#initialize(uri) ⇒ JsonRpc
Returns a new instance of JsonRpc.
21 22 23 24 |
# File 'lib/jsonrpc/jsonrpc.rb', line 21 def initialize( uri ) @uri = uri ## assume uri always as string for now @request_id = 1 end |
Class Method Details
.debug(value) ⇒ Object
14 |
# File 'lib/jsonrpc/jsonrpc.rb', line 14 def self.debug( value ) @debug = value; end |
.debug? ⇒ Boolean
add a global debug switch - why? why not?
13 |
# File 'lib/jsonrpc/jsonrpc.rb', line 13 def self.debug?() (@debug || false) == true; end |
Instance Method Details
#debug? ⇒ Boolean
private helpers
17 |
# File 'lib/jsonrpc/jsonrpc.rb', line 17 def debug?() self.class.debug?; end |
#request(method, params = []) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jsonrpc/jsonrpc.rb', line 27 def request( method, params=[] ) data = { jsonrpc: '2.0', method: method, params: params, id: @request_id } @request_id += 1 if debug? puts "json_rpc POST payload:" puts data.to_json end response = Webclient.post( @uri, json: data ) if response.status.nok? raise "Error code #{response.status.code} on request #{@uri} #{data}" end if debug? puts "json_rpc response.body:" puts response.body end 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 |