Sha256: d159e8431f4d06296a9c617a63d9b2b3d35d49f5f7e0cb81acd07957640b4df0
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
require 'rack' require 'time' require 'active_support/time' require 'eeny-meeny/models/experiment' require 'eeny-meeny/models/encryptor' require 'eeny-meeny/models/cookie' module EenyMeeny class Middleware def initialize(app) @app = app @experiments = EenyMeeny::Experiment.find_all @cookie_config = EenyMeeny.config.cookies end def call(env) request = Rack::Request.new(env) cookies = request.cookies now = Time.zone.now new_cookies = {} existing_set_cookie_header = env['Set-Cookie'] # Prepare for experiments. @experiments.each do |experiment| # Skip inactive experiments next unless experiment.active?(now) # skip experiments that already have a cookie unless cookies.has_key?(EenyMeeny::Cookie.cookie_name(experiment)) env['Set-Cookie'] = '' cookie = EenyMeeny::Cookie.create_for_experiment(experiment, @cookie_config) # Set HTTP_COOKIE header to enable experiment on first pageview Rack::Utils.set_cookie_header!(env, cookie.name, cookie.to_h) env['HTTP_COOKIE'] = '' if env['HTTP_COOKIE'].nil? env['HTTP_COOKIE'] += '; ' unless env['HTTP_COOKIE'].empty? env['HTTP_COOKIE'] += env['Set-Cookie'] new_cookies[cookie.name] = cookie end end # Clean up 'Set-Cookie' header. if existing_set_cookie_header.nil? env.delete('Set-Cookie') else env['Set-Cookie'] = existing_set_cookie_header end # Delegate to app status, headers, body = @app.call(env) response = Rack::Response.new(body, status, headers) # Add new cookies to 'Set-Cookie' header new_cookies.each do |key, value| response.set_cookie(key,value.to_h) end response.finish end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
eeny-meeny-2.0.0 | lib/eeny-meeny/middleware.rb |