class Fieldhand::Options
A wrapper around Repository
and Paginator
options for backward-compatibility.
In short, this handles passing a Logger
directly or passing it and a timeout as a hash. Note this attempts to preserve the previous behaviour of passing nil/falsey values as a logger even though this will cause errors if someone tries to use it that way.
Attributes
Public Class Methods
Return a new, normalized set of options based on the given value.
This supports both a `Logger`-compatible object to use for logging directly and a hash of options:
-
:logger - A `Logger`-compatible class for logging the activity of the library, defaults to a platform-specific
null logger
-
:timeout - A `Numeric` number of seconds to wait for any HTTP requests, defaults to 60 seconds
-
:retries - A `Numeric` maximum number of times a request is retried before erroring, defaults to 0
-
:interval - A `Numeric` number of seconds before an erroring request is retried, defaults to 10
-
:bearer_token - A `String` bearer token to use when sending any HTTP requests, defaults to nil
-
:headers - A `Hash` containing custom HTTP headers, defaults to {}.
# File lib/fieldhand/options.rb, line 25 def initialize(logger_or_options = {}) @logger_or_options = logger_or_options end
Public Instance Methods
Return the current bearer token.
# File lib/fieldhand/options.rb, line 50 def bearer_token options[:bearer_token] end
Build custom headers
# File lib/fieldhand/options.rb, line 55 def headers headers = options.fetch(:headers, {}) headers['Authorization'] = "Bearer #{bearer_token}" if bearer_token headers end
Return the current retry interval in seconds.
# File lib/fieldhand/options.rb, line 45 def interval options.fetch(:interval, 10) end
Return the current logger.
# File lib/fieldhand/options.rb, line 35 def logger options.fetch(:logger) { Logger.null } end
Return the current maximum number of retries.
# File lib/fieldhand/options.rb, line 40 def retries options.fetch(:retries, 0) end
Return the current timeout in seconds.
# File lib/fieldhand/options.rb, line 30 def timeout options.fetch(:timeout, 60) end
Private Instance Methods
# File lib/fieldhand/options.rb, line 64 def options return logger_or_options if logger_or_options.is_a?(Hash) { :logger => logger_or_options } end