Class: Shift::Api::Core::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/shift/api/core/config.rb

Overview

Global configuration

A simple config might look like this:-

Shift::Api::Core.config do

config.shift_root_url = ENV["SHIFT_ROOT_URL"]
config.logger = Rails.logger
config.timeout = ENV["SHIFT_TIMEOUT"]
config.open_timeout = ENV["SHIFT_OPEN_TIMEOUT"]

end

But, for testing, where the test suite wants to see data going both ways, and have no timeouts, we may have something like this :-

Shift::Api::Core.config do

config.shift_root_url = test_root_url
config.logger = Rails.logger
config.before_request -> (request) { do_something_with_request }
config.after_response -> (request, response) { do_something_with_request_or_response }
config.adapter = [:rack, application_under_test]
config.timeout = 0
config.open_timeout = 0

end

See detailed documentation below for more …

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



67
68
69
70
71
72
73
74
75
76
# File 'lib/shift/api/core/config.rb', line 67

def initialize
  @before_request_handlers = []
  @after_response_handlers = []
  @allow_reconfigure = true
  @adapter = :default
  @logger = :disabled
  @headers = {}
  @timeout = :default
  @open_timeout = :default
end

Instance Attribute Details

#adapterObject

The adapter to use for faraday or :default for the default



65
# File 'lib/shift/api/core/config.rb', line 65

attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout

#after_response_handlersObject (readonly)

An array of callable objects (lambda etc..) that are called with the request and response after it has been receive from the server defaults to []



65
# File 'lib/shift/api/core/config.rb', line 65

attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout

#before_request_handlersObject (readonly)

An array of callable objects (lambda etc..) that are called with the request before it goes out defaults to []



65
# File 'lib/shift/api/core/config.rb', line 65

attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout

#headersObject

Extra headers to add to every request defaults to {} Can also be an object responding to :call (i.e. a proc or lambda etc..) which must return a hash of headers to add



65
# File 'lib/shift/api/core/config.rb', line 65

attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout

#loggerObject

The logger to use or nil for none. Must be compatible with an ActiveSupport logger



65
# File 'lib/shift/api/core/config.rb', line 65

attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout

#open_timeoutObject

Returns the value of attribute open_timeout.



65
# File 'lib/shift/api/core/config.rb', line 65

attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout

#shift_root_urlObject

The root url for the shift server



65
66
67
# File 'lib/shift/api/core/config.rb', line 65

def shift_root_url
  @shift_root_url
end

#timeoutObject

The connection read timeout in seconds. If data is not received in this time, an error is raised. defaults to :default (15 seconds)



65
# File 'lib/shift/api/core/config.rb', line 65

attr_reader :shift_root_url, :logger, :before_request_handlers, :after_response_handlers, :adapter, :headers, :timeout, :open_timeout

Instance Method Details

#after_response(handler) ⇒ Object

Registers a handler that is to be called after the response is returned from the server. Multiple handlers get called in the sequence they were registered The handlers are called with the request and the response as the parameters

Parameters:

  • handler (#call)

    A handler that responds to call



117
118
119
120
# File 'lib/shift/api/core/config.rb', line 117

def after_response(handler)
  @after_response_handlers << handler
  reconfigure
end

#batch_configureObject

As every change to the config forces all models to reconfigure, this method allows for batch changes where the reconfigure is done at the end of the passed block.



125
126
127
128
129
130
131
# File 'lib/shift/api/core/config.rb', line 125

def batch_configure
  disable_reconfigure
  yield self
ensure
  enable_reconfigure
  reconfigure
end

#before_request(handler) ⇒ Object

Registers a handler that is to be called before the request is made to the server. Multiple handlers get called in the sequence they were registered The handlers are called with the request as the parameter

Parameters:

  • handler (#call)

    A handler that responds to call



107
108
109
110
# File 'lib/shift/api/core/config.rb', line 107

def before_request(handler)
  @before_request_handlers << handler
  reconfigure
end