Class: Limeade::JSON_RPC

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

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.

Parameters:

  • retry_options (Hash) (defaults to: {})

    The options for Faraday::Request::Retry.

Options Hash (retry_options):

  • :max (Integer) — default: 2

    Maximum number of retries.

  • :interval (Float) — default: 0

    Pause in seconds between retries.

  • :interval_randomness (Float) — default: 0

    The maximum random interval amount expressed as a float between 0 and 1 to use in addition to the interval.

  • :max_interval (Float) — default: Float::MAX

    An upper limit for the interval.

  • :backoff_factor (Float) — default: 1

    The amount to multiply each successive retry’s interval amount by in order to provide backoff.

  • :exceptions (Array<Error, String>) — default: [Errno::ETIMEDOUT, 'Timeout::Error', Error::TimeoutError, Faraday::Error::RetriableResponse]

    The list of exceptions to handle. Exceptions can be given as Class, Module, or String.

  • :methods (Array<Symbol>) — default: Faraday::Request::Retry::IDEMPOTENT_METHODS

    A list of HTTP methods to retry without calling retry_if. Pass an empty Array to call retry_if for all exceptions.

  • :retry_if (Proc) — default: return false

    Block that will receive the env object and the exception raised and decides whether to retry regardless of the retry count. This would be useful if the exception produced is non-recoverable or if the the HTTP method called is not idempotent.

  • :retry_block (Proc)

    Block that is executed after every retry. Request environment, middleware options, current number of retries and the exception are passed to the block as parameters.

Raises:

  • (ServerError)

    The API endpoint server is having issues. An error code and message are included.

  • (InvalidResponseError)

    The API endpoint is returning malformed JSON RPC responses. A descriptive message details the problem.

  • (URI::InvalidURIError)

    The API endpoint URI is not valid.

See Also:

  • Faraday::Request::Retry


45
46
47
48
# File 'lib/limeade/json_rpc.rb', line 45

def initialize(endpoint, retry_options = {})
  @uri = ::URI.parse(endpoint).to_s     # Ensure that endpoint is a valid URI
  @retry_options = retry_options || {}  # Ensure not nil
end

Instance Method Details

#invoke(method, *args) ⇒ Object

Send the request with the specified method and arguments

Parameters:

  • method (Symbol)

    the method to invoke

  • args (Array)

    the arguments to the method

Returns:

  • the results of the call



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