Class: Limeade::Client

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

Overview

Client for accessing the LimeSurvey RemoteControl API from Ruby.

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, username, password, retry_options = {}) ⇒ Client

Instantiate a client and setup a connection to the LimeSurvey RemoteControl API. Passes configuration for the Faraday::Request::Retry mechanism.

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:

  • (InvalidCredentialsError)

    The username and password combination is not valid.

  • (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


42
43
44
45
46
47
# File 'lib/limeade/client.rb', line 42

def initialize(endpoint, username, password, retry_options = {})
  @json_rpc = Limeade::JSON_RPC.new(endpoint, retry_options)
  @username = username
  @password = password
  @session_key = get_session_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object

Handle LimeSurvey RemoteControl API calls invoked on the client by invoking them on the endpoint.

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



76
77
78
79
80
81
82
# File 'lib/limeade/client.rb', line 76

def method_missing(method, *arguments)
  if API_METHODS.include? method
    process_request(method, *arguments)
  else
    super
  end
end

Instance Method Details

#connected?Boolean

Is the client ready to send requests to the endpoint?



51
52
53
# File 'lib/limeade/client.rb', line 51

def connected?
  @session_key && @json_rpc
end

#disconnectBoolean

Release resources for this client locally and at the endpoint.



57
58
59
60
61
62
63
64
65
66
# File 'lib/limeade/client.rb', line 57

def disconnect
  if connected?
    release_session_key
    @json_rpc = nil
    @session_key = nil
    true
  else
    false
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean



84
85
86
# File 'lib/limeade/client.rb', line 84

def respond_to_missing?(method_name, include_private = false)
  API_METHODS.include?(method_name) || super
end