class Shift::Api::Core::Config

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 …

Attributes

adapter[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)
after_response_handlers[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)
before_request_handlers[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)
headers[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)
logger[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)
open_timeout[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)
shift_root_url[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)
timeout[R]

@!attribute [rw] shift_root_url

The root url for the shift server

@!attribute [rw] logger

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

@!attribute [rw] adapter

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

@!attribute [r] before_request_handlers

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

@!attribute [r] after_response_handlers

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 []

@!attribute [rw] headers

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

@!attribute [rw] timeout

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

@!attribute [rw] open_timeout

The connection open timeout in seconds - i.e. if it takes longer than
this to open connection, an error is raised
defaults to :default (15 seconds)

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

adapter=(adapter) click to toggle source
# File lib/shift/api/core/config.rb, line 86
def adapter=(adapter)
  @adapter = adapter.tap { reconfigure }
end
after_response(handler) click to toggle source

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 @param handler [#call] A handler that responds to call

# File lib/shift/api/core/config.rb, line 117
def after_response(handler)
  @after_response_handlers << handler
  reconfigure
end
batch_configure() { |self| ... } click to toggle source

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.

# File lib/shift/api/core/config.rb, line 125
def batch_configure
  disable_reconfigure
  yield self
ensure
  enable_reconfigure
  reconfigure
end
before_request(handler) click to toggle source

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 @param handler [#call] A handler that responds to call

# File lib/shift/api/core/config.rb, line 107
def before_request(handler)
  @before_request_handlers << handler
  reconfigure
end
headers=(headers) click to toggle source
# File lib/shift/api/core/config.rb, line 90
def headers=(headers)
  @headers = headers.deep_dup.tap { reconfigure }
end
logger=(logger) click to toggle source
# File lib/shift/api/core/config.rb, line 82
def logger=(logger)
  @logger = logger.tap { reconfigure }
end
open_timeout=(open_timeout) click to toggle source
# File lib/shift/api/core/config.rb, line 98
def open_timeout=(open_timeout)
  @open_timeout = open_timeout.tap { reconfigure }
end
shift_root_url=(url) click to toggle source
# File lib/shift/api/core/config.rb, line 78
def shift_root_url=(url)
  @shift_root_url = url.tap { reconfigure }
end
timeout=(timeout) click to toggle source
# File lib/shift/api/core/config.rb, line 94
def timeout=(timeout)
  @timeout = timeout.tap { reconfigure }
end

Private Instance Methods

disable_reconfigure() click to toggle source
# File lib/shift/api/core/config.rb, line 135
def disable_reconfigure
  @allow_reconfigure = false
end
enable_reconfigure() click to toggle source
# File lib/shift/api/core/config.rb, line 139
def enable_reconfigure
  @allow_reconfigure = true
end
reconfigure() click to toggle source
# File lib/shift/api/core/config.rb, line 143
def reconfigure
  return unless @allow_reconfigure
  Shift::Api::Core::Model.reconfigure(self)
end