Class: AnotherApi::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/another_api/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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_sizeObject

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_mapObject

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_sizeObject

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_registryObject (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_prefixObject

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_modelObject

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_prefixObject

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_secretObject

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_classObject

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.

Raises:



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