Class: Limeade::JSON_RPC
- Inherits:
-
Object
- Object
- Limeade::JSON_RPC
- Defined in:
- lib/limeade/json_rpc.rb
Overview
An implementation of JSON RPC version 1. This is inspired by jsonrpc-client, which implements version 2 of the spec. This implementation adds retry capability via Faraday::Request::Retry.
Constant Summary collapse
- JSON_RPC_VERSION =
'1.0'
Instance Method Summary collapse
-
#initialize(endpoint, retry_options = {}) ⇒ JSON_RPC
constructor
Instantiate a client and setup a connection to the endpoint.
-
#invoke(method, *args) ⇒ Object
Send the request with the specified method and arguments.
Constructor Details
#initialize(endpoint, retry_options = {}) ⇒ JSON_RPC
Instantiate a client and setup a connection to the endpoint. Passes configuration for the Faraday::Request::Retry mechanism.
45 46 47 48 |
# File 'lib/limeade/json_rpc.rb', line 45 def initialize(endpoint, = {}) @uri = ::URI.parse(endpoint).to_s # Ensure that endpoint is a valid URI @retry_options = || {} # Ensure not nil end |
Instance Method Details
#invoke(method, *args) ⇒ Object
Send the request with the specified method and arguments
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/limeade/json_rpc.rb', line 54 def invoke(method, *args) Limeade.logger.debug "invoke(#{method}, #{args.inspect})" request_id = make_id post_data = ::MultiJson.encode({ 'jsonrpc' => JSON_RPC_VERSION, 'method' => method, 'params' => args, 'id' => request_id }) response = connection.post(@uri, post_data, STANDARD_HEADERS) Limeade.logger.debug "API response: #{response.inspect}" process_response(response, request_id) end |