Class: Rack::UtmCookies

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/utm_cookies.rb,
lib/rack/utm_cookies/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ UtmCookies

Returns a new instance of UtmCookies.



5
6
7
8
9
# File 'lib/rack/utm_cookies.rb', line 5

def initialize(app, options = {})
  @app = app
  @domain = options[:domain] || nil
  @ttl = options[:ttl] || 60*60*24*30  # 30 days
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack/utm_cookies.rb', line 11

def call(env)
  req = Rack::Request.new(env)

  # tack them on so they're available down
  # the middleware stack for this current request
  env['HTTP_COOKIE'] = '' unless env['HTTP_COOKIE']
  utm_cookies_to_set(req).each do |name, value|
    env['HTTP_COOKIE'] += "; #{name}=#{value}"
  end

  # pass the call down the middleware stack
  status, headers, body = @app.call(env)

  # make sure the proper set-cookie response headers
  # are set so we have the cookies for future requests.
  utm_cookies_to_set(req).each do |name, value|
    cookie_hash = {
      :value => value,
      :expires => Time.now + @ttl,
      :path => '/',
    }
    cookie_hash[:domain] = @domain if @domain
    Rack::Utils.set_cookie_header!(headers, name, cookie_hash)
  end

  [status, headers, body]
end