Class: Fieldhand::Options
- Inherits:
-
Object
- Object
- Fieldhand::Options
- Defined in:
- lib/fieldhand/options.rb
Overview
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.
Instance Attribute Summary collapse
-
#logger_or_options ⇒ Object
readonly
Returns the value of attribute logger_or_options.
Instance Method Summary collapse
-
#bearer_token ⇒ Object
Return the current bearer token.
-
#headers ⇒ Object
Build custom headers.
-
#initialize(logger_or_options = {}) ⇒ Options
constructor
Return a new, normalized set of options based on the given value.
-
#interval ⇒ Object
Return the current retry interval in seconds.
-
#logger ⇒ Object
Return the current logger.
-
#retries ⇒ Object
Return the current maximum number of retries.
-
#timeout ⇒ Object
Return the current timeout in seconds.
Constructor Details
#initialize(logger_or_options = {}) ⇒ Options
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 {}.
25 26 27 |
# File 'lib/fieldhand/options.rb', line 25 def initialize( = {}) @logger_or_options = end |
Instance Attribute Details
#logger_or_options ⇒ Object (readonly)
Returns the value of attribute logger_or_options.
12 13 14 |
# File 'lib/fieldhand/options.rb', line 12 def @logger_or_options end |
Instance Method Details
#bearer_token ⇒ Object
Return the current bearer token.
50 51 52 |
# File 'lib/fieldhand/options.rb', line 50 def bearer_token [:bearer_token] end |
#headers ⇒ Object
Build custom headers
55 56 57 58 59 60 |
# File 'lib/fieldhand/options.rb', line 55 def headers headers = .fetch(:headers, {}) headers['Authorization'] = "Bearer #{bearer_token}" if bearer_token headers end |
#interval ⇒ Object
Return the current retry interval in seconds.
45 46 47 |
# File 'lib/fieldhand/options.rb', line 45 def interval .fetch(:interval, 10) end |
#logger ⇒ Object
Return the current logger.
35 36 37 |
# File 'lib/fieldhand/options.rb', line 35 def logger .fetch(:logger) { Logger.null } end |
#retries ⇒ Object
Return the current maximum number of retries.
40 41 42 |
# File 'lib/fieldhand/options.rb', line 40 def retries .fetch(:retries, 0) end |
#timeout ⇒ Object
Return the current timeout in seconds.
30 31 32 |
# File 'lib/fieldhand/options.rb', line 30 def timeout .fetch(:timeout, 60) end |