Class: Rack::ParamToCookie
- Inherits:
-
Object
- Object
- Rack::ParamToCookie
- Defined in:
- lib/rack/param_to_cookie.rb,
lib/rack/param_to_cookie/version.rb
Overview
Rack middleware. See README.
Constant Summary collapse
- VERSION_MAJOR =
1- VERSION_MINOR =
0- VERSION_PATCH =
0- VERSION =
[VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH].join('.')
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, param_cookies) ⇒ ParamToCookie
constructor
A new instance of ParamToCookie.
Constructor Details
#initialize(app, param_cookies) ⇒ ParamToCookie
Returns a new instance of ParamToCookie.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rack/param_to_cookie.rb', line 14 def initialize app, @app = app @param_cookies = @param_cookies.each do |param, | [:cookie_name] ||= param [:env_name] ||= param [:ttl] ||= 60*60*24*30 # 30 days [:set_cookie_options] ||= {} end end |
Instance Method Details
#call(env) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rack/param_to_cookie.rb', line 25 def call env req = Rack::Request.new(env) = {} @param_cookies.each do |param, | # get the value from a previously set cookie = req.[[:cookie_name]] # check whether there's a new value for the cookie with this request params_value = req.params[param] rescue nil value = params_value || env[[:env_name]] = value if value # once we handle the response, set the new cookie value if params_value [[:cookie_name]] = [:set_cookie_options].merge( value: params_value, expires: Time.now + [:ttl]) end end status, headers, body = @app.call(env) response = Rack::Response.new body, status, headers .each do |, | response. , end response.finish end |