Class: AnotherApi::Configuration
- Inherits:
-
Object
- Object
- AnotherApi::Configuration
- Defined in:
- lib/another_api/configuration.rb
Instance Attribute Summary collapse
-
#default_page_size ⇒ Object
Returns the value of attribute default_page_size.
-
#error_status_map ⇒ Object
Returns the value of attribute error_status_map.
-
#max_page_size ⇒ Object
Returns the value of attribute max_page_size.
-
#rescue_registry ⇒ Object
readonly
Returns the value of attribute rescue_registry.
-
#scope_prefix ⇒ Object
Returns the value of attribute scope_prefix.
-
#token_model ⇒ Object
Returns the value of attribute token_model.
-
#token_prefix ⇒ Object
Returns the value of attribute token_prefix.
-
#token_secret ⇒ Object
Returns the value of attribute token_secret.
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #rescue_from(exception_class_name, as:) ⇒ Object
-
#token_model_class ⇒ Object
Resolved once and memoised: later writes to token_model cannot redirect authentication to a different class.
-
#validate! ⇒ Object
Fail fast at boot if the consumer has not configured required values.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/another_api/configuration.rb', line 7 def initialize @token_model = "ApiToken" @token_secret = nil @token_prefix = "aa" @scope_prefix = "api.v2." @default_page_size = 20 @max_page_size = 200 @error_status_map = { bad_request: :bad_request, validation_error: :bad_request, unprocessable_content: :unprocessable_entity, not_found: :not_found, forbidden: :forbidden, unauthorized: :unauthorized, conflict: :conflict, not_acceptable: :not_acceptable } @rescue_registry = [] end |
Instance Attribute Details
#default_page_size ⇒ Object
Returns the value of attribute default_page_size.
3 4 5 |
# File 'lib/another_api/configuration.rb', line 3 def default_page_size @default_page_size end |
#error_status_map ⇒ Object
Returns the value of attribute error_status_map.
3 4 5 |
# File 'lib/another_api/configuration.rb', line 3 def error_status_map @error_status_map end |
#max_page_size ⇒ Object
Returns the value of attribute max_page_size.
3 4 5 |
# File 'lib/another_api/configuration.rb', line 3 def max_page_size @max_page_size end |
#rescue_registry ⇒ Object (readonly)
Returns the value of attribute rescue_registry.
31 32 33 |
# File 'lib/another_api/configuration.rb', line 31 def rescue_registry @rescue_registry end |
#scope_prefix ⇒ Object
Returns the value of attribute scope_prefix.
3 4 5 |
# File 'lib/another_api/configuration.rb', line 3 def scope_prefix @scope_prefix end |
#token_model ⇒ Object
Returns the value of attribute token_model.
3 4 5 |
# File 'lib/another_api/configuration.rb', line 3 def token_model @token_model end |
#token_prefix ⇒ Object
Returns the value of attribute token_prefix.
3 4 5 |
# File 'lib/another_api/configuration.rb', line 3 def token_prefix @token_prefix end |
#token_secret ⇒ Object
Returns the value of attribute token_secret.
3 4 5 |
# File 'lib/another_api/configuration.rb', line 3 def token_secret @token_secret end |
Instance Method Details
#rescue_from(exception_class_name, as:) ⇒ Object
27 28 29 |
# File 'lib/another_api/configuration.rb', line 27 def rescue_from(exception_class_name, as:) @rescue_registry << {exception: exception_class_name, error_type: as} end |
#token_model_class ⇒ Object
Resolved once and memoised: later writes to token_model cannot redirect authentication to a different class. Call reset_configuration! to rebuild.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/another_api/configuration.rb', line 45 def token_model_class @token_model_class ||= begin klass = token_model.to_s.safe_constantize if klass.nil? raise ConfigurationError, "AnotherApi.configuration.token_model #{token_model.inspect} could not be resolved to a class" end unless klass.respond_to?(:find_by_token) raise ConfigurationError, "AnotherApi.configuration.token_model #{token_model.inspect} must respond to .find_by_token (see AnotherApi::ApiTokenContract)" end klass end end |
#validate! ⇒ Object
Fail fast at boot if the consumer has not configured required values. Missing token_secret is especially bad: TokenGeneration.digest raises at request time, triggering the StandardError rescue for every request.
36 37 38 39 40 41 |
# File 'lib/another_api/configuration.rb', line 36 def validate! raise ConfigurationError, "AnotherApi.configuration.token_secret must be set" if token_secret.nil? || token_secret.to_s.empty? raise ConfigurationError, "AnotherApi.configuration.token_model must be set" if token_model.nil? || token_model.to_s.empty? token_model_class nil end |