Class: DedupeRequests::Configuration

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

Constant Summary collapse

MODES =
%i[off observe enforce].freeze
DEFAULT_CONFLICT_BODY =
{
  "errors" => [{
    "error_key" => "base",
    "category" => "duplicate_operation",
    "message" => "Duplicate request detected. A matching request is in-flight or recently completed."
  }]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dedupe_requests/configuration.rb', line 33

def initialize
  @redis = nil
  @store = nil
  @mode = :enforce
  @ttl = 90
  @digest = :sha256
  @namespace = "dedupe_requests"
  @caller_id = nil
  @fingerprint = nil
  @conflict_status = 409
  @logger = nil
  @on_duplicate_detected = nil
  @on_duplicate_rejected = nil
  @conflict_body = nil
end

Instance Attribute Details

#caller_idObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def caller_id
  @caller_id
end

#conflict_bodyObject



66
67
68
# File 'lib/dedupe_requests/configuration.rb', line 66

def conflict_body
  @conflict_body || DEFAULT_CONFLICT_BODY
end

#conflict_statusObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def conflict_status
  @conflict_status
end

#digestObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def digest
  @digest
end

#fingerprintObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def fingerprint
  @fingerprint
end

#loggerObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def logger
  @logger
end

#modeObject

Returns the value of attribute mode.



31
32
33
# File 'lib/dedupe_requests/configuration.rb', line 31

def mode
  @mode
end

#namespaceObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def namespace
  @namespace
end

#on_duplicate_detectedObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def on_duplicate_detected
  @on_duplicate_detected
end

#on_duplicate_rejectedObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def on_duplicate_rejected
  @on_duplicate_rejected
end

#redisObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def redis
  @redis
end

#storeObject



62
63
64
# File 'lib/dedupe_requests/configuration.rb', line 62

def store
  @store ||= (RedisStore.new(@redis, namespace: @namespace, logger: @logger) if @redis)
end

#ttlObject

Per-caller identity. There is NO default — you MUST configure ‘caller_id` with a callable that returns a stable, non-secret identifier for the caller (a user id, a JWT `sub`, an API-client id). Do NOT use a raw bearer token or API key: it’s secret and it rotates, so the same caller would look like different callers and de-duplication would silently weaken. The callable is given the CONTROLLER, so it can read ‘current_user`, a helper, or a header via `controller.request`. Examples:

c.caller_id = ->(controller) { controller.current_user&.id }
c.caller_id = ->(controller) { controller.request.get_header("HTTP_X_API_KEY") }

When ‘caller_id` is unset or returns nil, de-duplication is skipped for the request (and a warning is logged), rather than risk treating different callers as one.



27
28
29
# File 'lib/dedupe_requests/configuration.rb', line 27

def ttl
  @ttl
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/dedupe_requests/configuration.rb', line 58

def enabled?
  @mode != :off
end