Class: Labkit::RateLimit::Identifier

Inherits:
Object
  • Object
show all
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).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Identifier

Returns a new instance of Identifier.



17
18
19
# File 'lib/labkit/rate_limit/identifier.rb', line 17

def initialize(attributes = {})
  @attributes = attributes.transform_keys(&:to_sym).freeze
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



15
16
17
# File 'lib/labkit/rate_limit/identifier.rb', line 15

def attributes
  @attributes
end

Class Method Details

.normalize_endpoint(value) ⇒ Object

Normalize an endpoint value: strip query string.



9
10
11
12
13
# File 'lib/labkit/rate_limit/identifier.rb', line 9

def self.normalize_endpoint(value)
  return value unless value.is_a?(String)

  value.split("?", 2).first
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
# File 'lib/labkit/rate_limit/identifier.rb', line 31

def ==(other)
  other.is_a?(Identifier) && other.attributes == @attributes
end

#[](key) ⇒ Object

Return the value for a characteristic key.



22
23
24
# File 'lib/labkit/rate_limit/identifier.rb', line 22

def [](key)
  @attributes[key.to_sym]
end

#to_hObject

Serialize to a plain Hash suitable for JSON logging.



27
28
29
# File 'lib/labkit/rate_limit/identifier.rb', line 27

def to_h
  @attributes.transform_keys(&:to_s)
end