Class: Fieldhand::Options

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

Instance Method Summary collapse

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

  • :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 {}.



21
22
23
# File 'lib/fieldhand/options.rb', line 21

def initialize(logger_or_options = {})
  @logger_or_options = logger_or_options
end

Instance Attribute Details

#logger_or_optionsObject (readonly)

Returns the value of attribute logger_or_options.



10
11
12
# File 'lib/fieldhand/options.rb', line 10

def logger_or_options
  @logger_or_options
end

Instance Method Details

#bearer_tokenObject

Return the current bearer token.



36
37
38
# File 'lib/fieldhand/options.rb', line 36

def bearer_token
  options[:bearer_token]
end

#headersObject

Build custom headers



41
42
43
44
45
46
# File 'lib/fieldhand/options.rb', line 41

def headers
  headers = options.fetch(:headers, {})
  headers['Authorization'] = "Bearer #{bearer_token}" if bearer_token

  headers
end

#loggerObject

Return the current logger.



31
32
33
# File 'lib/fieldhand/options.rb', line 31

def logger
  options.fetch(:logger) { Logger.null }
end

#timeoutObject

Return the current timeout in seconds.



26
27
28
# File 'lib/fieldhand/options.rb', line 26

def timeout
  options.fetch(:timeout, 60)
end