Class: Rack::MonetaCookies

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/moneta_cookies.rb

Overview

A Rack middleware that was made to reuse all moneta transformers on the cookie hash.

Examples:

config.ru

# Add Rack::MonetaCookies somewhere in your rack stack
use Rack::MonetaCookies

run lambda { |env| [200, {}, []] }
# But this doesn't do much

config.ru

# Give it some options
use Rack::MonetaCookies, domain: 'example.com', path: '/path'

config.ru

# Pass it a block like the one passed to Moneta.build
use Rack::MonetaCookies do
  use :Transformer, key: :prefix, prefix: 'moneta.'
  adapter :Cookie
end

run lambda { |env|
  req = Rack::Request.new(env)
  req.cookies #=> is now a Moneta store!
  env['rack.request.cookie_hash'] #=> is now a Moneta store!
  req.cookies['key'] #=> retrieves 'moneta.key'
  req.cookies['key'] = 'value' #=> sets 'moneta.key'
  req.cookies.delete('key') #=> removes 'moneta.key'
  [200, {}, []]
}

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ MonetaCookies

Returns a new instance of MonetaCookies.



38
39
40
41
42
43
44
45
46
# File 'lib/rack/moneta_cookies.rb', line 38

def initialize(app, options = {}, &block)
  @app, @pool = app, []
  if block
    raise ArgumentError, 'Use either block or options' unless options.empty?
    @builder = Moneta::Builder.new(&block)
  else
    @builder = Moneta::Builder.new { adapter :Cookie, options }
  end
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rack/moneta_cookies.rb', line 48

def call(env)
  stores = @pool.pop || @builder.build
  env['rack.moneta_cookies'] = env['rack.request.cookie_hash'] = stores.last
  env['rack.request.cookie_string'] = env['HTTP_COOKIE']
  stores.first.reset(Rack::Utils.parse_query(env['HTTP_COOKIE']))
  status, headers, body = @app.call(env)
  stores.first.cookies.each do |key, cookie|
    if cookie == nil
      Rack::Utils.delete_cookie_header!(headers, key)
    else
      Rack::Utils.set_cookie_header!(headers, key, cookie)
    end
  end
  @pool << stores
  [status, headers, body]
end