Class: Labkit::RateLimit::Identifier
- Inherits:
-
Object
- Object
- Labkit::RateLimit::Identifier
- Defined in:
- lib/labkit/rate_limit/identifier.rb
Overview
Identifier is a value object wrapping a hash of key-value pairs that describe the caller (e.g. user, ip, endpoint). Endpoint values are normalised at construction time (query string stripped).
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.normalize_endpoint(value) ⇒ Object
Normalize an endpoint value: strip query string.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ Object
Return the value for a characteristic key.
-
#initialize(attributes = {}) ⇒ Identifier
constructor
A new instance of Identifier.
-
#to_h ⇒ Object
Serialize to a plain Hash suitable for JSON logging.
Constructor Details
#initialize(attributes = {}) ⇒ Identifier
Returns a new instance of Identifier.
18 19 20 21 22 |
# File 'lib/labkit/rate_limit/identifier.rb', line 18 def initialize(attributes = {}) normalised = attributes.transform_keys(&:to_sym) normalised[:endpoint] = self.class.normalize_endpoint(normalised[:endpoint]) if normalised.key?(:endpoint) @attributes = normalised.freeze end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
16 17 18 |
# File 'lib/labkit/rate_limit/identifier.rb', line 16 def attributes @attributes end |
Class Method Details
.normalize_endpoint(value) ⇒ Object
Normalize an endpoint value: strip query string.
10 11 12 13 14 |
# File 'lib/labkit/rate_limit/identifier.rb', line 10 def self.normalize_endpoint(value) return value unless value.is_a?(String) value.split("?", 2).first end |
Instance Method Details
#==(other) ⇒ Object
34 35 36 |
# File 'lib/labkit/rate_limit/identifier.rb', line 34 def ==(other) other.is_a?(Identifier) && other.attributes == @attributes end |
#[](key) ⇒ Object
Return the value for a characteristic key.
25 26 27 |
# File 'lib/labkit/rate_limit/identifier.rb', line 25 def [](key) @attributes[key.to_sym] end |
#to_h ⇒ Object
Serialize to a plain Hash suitable for JSON logging.
30 31 32 |
# File 'lib/labkit/rate_limit/identifier.rb', line 30 def to_h @attributes.transform_keys(&:to_s) end |