Class: Common::Client::Configuration::Base
- Inherits:
-
Object
- Object
- Common::Client::Configuration::Base
- Includes:
- Singleton
- Defined in:
- lib/common/client/configuration/base.rb
Overview
Class Attributes collapse
-
#base_request_headers ⇒ Object
headers to include in all requests.
-
#open_timeout ⇒ Object
rubocop:disable ThreadSafety/ClassAndModuleAttributes These attributes are inherited by subclasses so that they can change their own value without impacting the parent class timeout for opening the connection.
-
#read_timeout ⇒ Object
timeout for reading a response.
-
#request_types ⇒ Object
allowed requests types e.g.
-
#user_agent ⇒ Object
value for the User-Agent header.
Class Attributes collapse
-
#base_path ⇒ Object
The base path to use for all requests.
-
#breakers_error_threshold ⇒ Integer
The percentage of errors over which an outage will be reported as part of breakers gem.
- #breakers_exception_handler ⇒ Object
- #breakers_matcher ⇒ Object
-
#breakers_service ⇒ Object
Default request options, sets the read and open timeouts.
- #create_new_breakers_service(matcher, exception_handler) ⇒ Object
-
#current_module ⇒ Object
private
deconstantize fetches “AA::BB::” from AA::BB::ClassName, and constantize returns that as a constant.
-
#request_options ⇒ Object
Default request options, sets the read and open timeouts.
-
#service_exception ⇒ Object
Creates a custom service exception with the same namespace as the implementing class.
-
#service_name ⇒ Object
The service name that shows up in breakers metrics and logs.
Instance Attribute Details
#base_request_headers ⇒ Object
headers to include in all requests
39 |
# File 'lib/common/client/configuration/base.rb', line 39 class_attribute :base_request_headers |
#open_timeout ⇒ Object
rubocop:disable ThreadSafety/ClassAndModuleAttributes These attributes are inherited by subclasses so that they can change their own value without impacting the parent class timeout for opening the connection
19 |
# File 'lib/common/client/configuration/base.rb', line 19 class_attribute :open_timeout |
#read_timeout ⇒ Object
timeout for reading a response
24 |
# File 'lib/common/client/configuration/base.rb', line 24 class_attribute :read_timeout |
#request_types ⇒ Object
allowed requests types e.g. ‘%i[get put post delete].freeze`
29 |
# File 'lib/common/client/configuration/base.rb', line 29 class_attribute :request_types |
#user_agent ⇒ Object
value for the User-Agent header
34 |
# File 'lib/common/client/configuration/base.rb', line 34 class_attribute :user_agent |
Instance Method Details
#base_path ⇒ Object
The base path to use for all requests. Implemented by sub classes.
55 56 57 |
# File 'lib/common/client/configuration/base.rb', line 55 def base_path raise NotImplementedError, "Subclass #{self.class.name} of Configuration must implement base_path" end |
#breakers_error_threshold ⇒ Integer
The percentage of errors over which an outage will be reported as part of breakers gem
137 138 139 |
# File 'lib/common/client/configuration/base.rb', line 137 def breakers_error_threshold 50 end |
#breakers_exception_handler ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/common/client/configuration/base.rb', line 110 def breakers_exception_handler proc do |exception| case exception when Common::Exceptions::BackendServiceException (500..599).cover?(exception.response_values[:status]) when Common::Client::Errors::HTTPError (500..599).cover?(exception.status) when Faraday::ServerError (500..599).cover?(exception.response&.[](:status)) else false end end end |
#breakers_matcher ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/common/client/configuration/base.rb', line 102 def breakers_matcher base_uri = URI.parse(base_path) proc do |request_env| request_env.url.host == base_uri.host && request_env.url.port == base_uri.port && request_env.url.path =~ /^#{base_uri.path}/ end end |
#breakers_service ⇒ Object
Default request options, sets the read and open timeouts.
96 97 98 99 100 |
# File 'lib/common/client/configuration/base.rb', line 96 def breakers_service return @service if defined?(@service) @service = create_new_breakers_service(breakers_matcher, breakers_exception_handler) end |
#create_new_breakers_service(matcher, exception_handler) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/common/client/configuration/base.rb', line 125 def create_new_breakers_service(matcher, exception_handler) Breakers::Service.new( name: service_name, request_matcher: matcher, error_threshold: breakers_error_threshold, exception_handler: ) end |
#current_module ⇒ Object (private)
deconstantize fetches “AA::BB::” from AA::BB::ClassName, and constantize returns that as a constant.
144 145 146 |
# File 'lib/common/client/configuration/base.rb', line 144 def current_module self.class.name.deconstantize.constantize end |
#request_options ⇒ Object
Default request options, sets the read and open timeouts.
84 85 86 87 88 89 |
# File 'lib/common/client/configuration/base.rb', line 84 def { open_timeout:, timeout: read_timeout } end |
#service_exception ⇒ Object
Creates a custom service exception with the same namespace as the implementing class.
71 72 73 74 75 76 77 |
# File 'lib/common/client/configuration/base.rb', line 71 def service_exception if current_module.const_defined?('ServiceException') current_module.const_get('ServiceException') else current_module.const_set('ServiceException', Class.new(Common::Exceptions::BackendServiceException)) end end |
#service_name ⇒ Object
The service name that shows up in breakers metrics and logs. Implemented by sub classes.
62 63 64 |
# File 'lib/common/client/configuration/base.rb', line 62 def service_name raise NotImplementedError, "Subclass #{self.class.name} of Configuration must implement service_name" end |