class Rack::Affiliates
Rack
Middleware for extracting information from the request params and cookies. It populates +env+, # +env+ and +env if it detects a request came from an affiliated link
Constants
- COOKIE_EXTRA_VARS
- COOKIE_FROM
- COOKIE_TAG
- COOKIE_TIME
Public Class Methods
new(app, opts = {})
click to toggle source
# File lib/rack-affiliates.rb, line 13 def initialize(app, opts = {}) @app = app @param = opts[:param] || "ref" @cookie_ttl = opts[:ttl] || 60*60*24*30 # 30 days @cookie_domain = opts[:domain] || nil @allow_overwrite = opts[:overwrite].nil? ? true : opts[:overwrite] @cookie_path = opts[:path] || nil @extras = opts[:extra_params] || [] end
Public Instance Methods
affiliate_info(req)
click to toggle source
# File lib/rack-affiliates.rb, line 59 def affiliate_info(req) params_info(req) || cookie_info(req) end
call(env)
click to toggle source
# File lib/rack-affiliates.rb, line 23 def call(env) req = Rack::Request.new(env) params_tag = req.params[@param] cookie_tag = req.cookies[COOKIE_TAG] if cookie_tag tag, from, time, extras = cookie_info(req) end if params_tag && params_tag != cookie_tag if tag if @allow_overwrite tag, from, time, extras = params_info(req) end else tag, from, time, extras = params_info(req) end end if tag env["affiliate.tag"] = tag env['affiliate.from'] = from env['affiliate.time'] = time env['affiliate.extras'] = extras end status, headers, body = @app.call(env) if tag != cookie_tag bake_cookies(headers, tag, from, time, extras) end [status, headers, body] end
params_info(req)
click to toggle source
# File lib/rack-affiliates.rb, line 63 def params_info(req) extras = {} @extras.each { |key| extras[key] = req.params[key.to_s] } [req.params[@param], req.env["HTTP_REFERER"], Time.now.to_i, extras] end