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.

Parameters:

  • endpoint (String)

    The URI for your account's API.

  • username (String)

    The username for your account.

  • password (String)

    The password for your account.

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

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


44
45
46
47
48
49
# File 'lib/limeade/client.rb', line 44

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.

Parameters:

  • method (Symbol)

    the method invoked

  • arguments (Array)

    the arguments to the method

Returns:

  • (Object)

    the result of invoking the method 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



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

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?

Returns:

  • (Boolean)

    true if the client is ready; false otherwise.



53
54
55
# File 'lib/limeade/client.rb', line 53

def connected?
  @session_key && @json_rpc
end

#disconnectBoolean

Release resources for this client locally and at the endpoint.

Returns:

  • (Boolean)

    true if the resources were released; false if client has already disconnected.



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

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

Returns:

  • (Boolean)


86
87
88
# File 'lib/limeade/client.rb', line 86

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