Class: ActiveRecordProxyAdapters::Middleware

Inherits:
Object
  • Object
show all
Extended by:
ActiveRecordProxyAdapters::Mixin::Configuration
Includes:
Contextualizer, ActiveRecordProxyAdapters::Mixin::Configuration
Defined in:
lib/active_record_proxy_adapters/middleware.rb

Overview

rubocop:disable Style/Documentation

Constant Summary collapse

"arpa_context"
5.seconds.freeze
{
  path: "/",
  http_only: true
}.freeze
lambda do |rack_env|
  rack_request = Rack::Request.new(rack_env)
  arpa_cookie  = rack_request.cookies[COOKIE_NAME]
  JSON.parse(arpa_cookie || "{}")
rescue JSON::ParserError
  {}
end.freeze
lambda do |headers, cookie_hash, options|
  cookie           = DEFAULT_COOKIE_OPTIONS.merge(options)
  max_value        = cookie_hash.values.max || 0
  then_time        = Time.at(max_value).utc
  expires          = then_time + proxy_delay(cookie_hash.key(max_value)) + COOKIE_BUFFER
  max_age          = expires - then_time
  cookie[:expires] = expires
  cookie[:max_age] = max_age
  cookie[:value]   = cookie_hash.to_json

  Rack::Utils.set_cookie_header!(headers, COOKIE_NAME, cookie)
end.freeze

Instance Method Summary collapse

Methods included from ActiveRecordProxyAdapters::Mixin::Configuration

cache_key_for, cache_store, checkout_timeout, context_store, log_subscriber_prefix, logger, proxy_delay, regexp_timeout_strategy

Methods included from Contextualizer

current_context, current_context=

Constructor Details

#initialize(app, cookie_options = {}) ⇒ Middleware

Returns a new instance of Middleware.



46
47
48
49
# File 'lib/active_record_proxy_adapters/middleware.rb', line 46

def initialize(app, cookie_options = {})
  @app = app
  @cookie_options = cookie_options
end

Instance Method Details

#call(env) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_record_proxy_adapters/middleware.rb', line 51

def call(env)
  return @app.call(env) if ignore_request?(env)

  self.current_context = context_store.new(COOKIE_READER.call(env))

  status, headers, body = @app.call(env)

  update_cookie_from_context(headers)

  [status, headers, body]
end