Class: Rack::Affiliates
- Inherits:
-
Object
- Object
- Rack::Affiliates
- Defined in:
- lib/rack-affiliates.rb
Overview
Constant Summary collapse
- COOKIE_TAG =
"aff_tag"
- COOKIE_FROM =
"aff_from"
- COOKIE_TIME =
"aff_time"
- COOKIE_EXTRA_VARS =
"aff_extra"
Instance Method Summary collapse
- #affiliate_info(req) ⇒ Object
- #call(env) ⇒ Object
- #cookie_info(req) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Affiliates
constructor
A new instance of Affiliates.
- #params_info(req) ⇒ Object
Constructor Details
#initialize(app, opts = {}) ⇒ Affiliates
Returns a new instance of Affiliates.
13 14 15 16 17 18 19 20 21 |
# 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 |
Instance Method Details
#affiliate_info(req) ⇒ Object
59 60 61 |
# File 'lib/rack-affiliates.rb', line 59 def affiliate_info(req) params_info(req) || (req) end |
#call(env) ⇒ Object
23 24 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 57 |
# File 'lib/rack-affiliates.rb', line 23 def call(env) req = Rack::Request.new(env) params_tag = req.params[@param] = req.[COOKIE_TAG] if tag, from, time, extras = (req) end if params_tag && params_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 != (headers, tag, from, time, extras) end [status, headers, body] end |
#cookie_info(req) ⇒ Object
70 71 72 73 74 |
# File 'lib/rack-affiliates.rb', line 70 def (req) extras = {} @extras.each { |key| extras[key] = req.["#{COOKIE_EXTRA_VARS}.#{key}"] } [req.[COOKIE_TAG], req.[COOKIE_FROM], req.[COOKIE_TIME].to_i, extras] end |
#params_info(req) ⇒ Object
63 64 65 66 67 68 |
# 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 |