rack-affiliates 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f235b3dda5e49f7479173870577df0d1395be2ba
4
- data.tar.gz: ec21042f606fed14b1f4279d30696c6f90f84cce
3
+ metadata.gz: 6ed7aa209d8a8ae9ea5d49fd5123d4232e1f742d
4
+ data.tar.gz: 45f606cb82272f5a6e0d4ce139ed0aedeaab137f
5
5
  SHA512:
6
- metadata.gz: ce1c76fb4708658fd508a88bb51226f2e01dc81bd266c5cd0c26cec8779a34d306377b0ef4c73f36cc30f7bd268ff1566e4c6eef485c7ead903c1eaa3f4ab38d
7
- data.tar.gz: 7f788ac693dae917858b26022f04badc8792b0dad4f63fbe213d799ddaeb699f789a2149d0e7ce069cb3fb040cf45a0b2902b7aec229eaa0fdfec954e7b7f49c
6
+ metadata.gz: bc5d01fc4c7a1fe3f5dd142ace8b5c01ea4d3ef4e5d75fab3709dabbd89da74a368f96d9ad35a8c8927084e459ce7c90d79683a9fa09bab0771865c148482da5
7
+ data.tar.gz: 0a4a1abea36a3c949ff312489be860dbbac420736d32b029a4152c03e885c2b276af46d915453aa2cd0a46d08a1a43f2c7cc99df7bb979bfa2ff2c2943fb9744
data/README.md CHANGED
@@ -92,6 +92,17 @@ Middleware will set cookie on <code>.example.org</code> so it's accessible on <c
92
92
 
93
93
  The <code>:overwrite</code> option allows to set whether to overwrite the existing affiliate tag previously stored in cookies. By default it is set to `true`.
94
94
 
95
+ If you want to capture more attributes from the query string whenever it comes from an affiliate you can define those with the <code>extra_params</code> value.
96
+
97
+ #Rails 3/4 in config/application.rb
98
+ class Application < Rails::Application
99
+ ...
100
+ config.middleware.use Rack::Affiliates, { :extra_params => [:great_query_parameter] }
101
+ ...
102
+ end
103
+
104
+ These will be availble through <code>env['affiliate.extras']</code> as a hash with the same keys.
105
+
95
106
  Credits
96
107
  =======
97
108
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -2,20 +2,22 @@ module Rack
2
2
  #
3
3
  # Rack Middleware for extracting information from the request params and cookies.
4
4
  # It populates +env['affiliate.tag']+, # +env['affiliate.from']+ and
5
- # +env['affiliate.time'] if it detects a request came from an affiliated link
5
+ # +env['affiliate.time'] if it detects a request came from an affiliated link
6
6
  #
7
7
  class Affiliates
8
8
  COOKIE_TAG = "aff_tag"
9
9
  COOKIE_FROM = "aff_from"
10
10
  COOKIE_TIME = "aff_time"
11
+ COOKIE_EXTRA_VARS = "aff_extra"
11
12
 
12
13
  def initialize(app, opts = {})
13
14
  @app = app
14
15
  @param = opts[:param] || "ref"
15
16
  @cookie_ttl = opts[:ttl] || 60*60*24*30 # 30 days
16
17
  @cookie_domain = opts[:domain] || nil
17
- @allow_overwrite = opts[:overwrite].nil? ? true : opts[:overwrite]
18
+ @allow_overwrite = opts[:overwrite].nil? ? true : opts[:overwrite]
18
19
  @cookie_path = opts[:path] || nil
20
+ @extras = opts[:extra_params] || []
19
21
  end
20
22
 
21
23
  def call(env)
@@ -25,16 +27,16 @@ module Rack
25
27
  cookie_tag = req.cookies[COOKIE_TAG]
26
28
 
27
29
  if cookie_tag
28
- tag, from, time = cookie_info(req)
30
+ tag, from, time, extras = cookie_info(req)
29
31
  end
30
32
 
31
33
  if params_tag && params_tag != cookie_tag
32
34
  if tag
33
35
  if @allow_overwrite
34
- tag, from, time = params_info(req)
36
+ tag, from, time, extras = params_info(req)
35
37
  end
36
38
  else
37
- tag, from, time = params_info(req)
39
+ tag, from, time, extras = params_info(req)
38
40
  end
39
41
  end
40
42
 
@@ -42,40 +44,51 @@ module Rack
42
44
  env["affiliate.tag"] = tag
43
45
  env['affiliate.from'] = from
44
46
  env['affiliate.time'] = time
47
+ env['affiliate.extras'] = extras
45
48
  end
46
-
49
+
47
50
  status, headers, body = @app.call(env)
48
51
 
49
52
  if tag != cookie_tag
50
- bake_cookies(headers, tag, from, time)
53
+ bake_cookies(headers, tag, from, time, extras)
51
54
  end
52
55
 
53
56
  [status, headers, body]
54
57
  end
55
58
 
56
59
  def affiliate_info(req)
57
- params_info(req) || cookie_info(req)
60
+ params_info(req) || cookie_info(req)
58
61
  end
59
62
 
60
63
  def params_info(req)
61
- [req.params[@param], req.env["HTTP_REFERER"], Time.now.to_i]
64
+ extras = {}
65
+ @extras.each { |key| extras[key] = req.params[key.to_s] }
66
+
67
+ [req.params[@param], req.env["HTTP_REFERER"], Time.now.to_i, extras]
62
68
  end
63
69
 
64
70
  def cookie_info(req)
65
- [req.cookies[COOKIE_TAG], req.cookies[COOKIE_FROM], req.cookies[COOKIE_TIME].to_i]
71
+ extras = {}
72
+ @extras.each { |key| extras[key] = req.cookies["#{COOKIE_EXTRA_VARS}.#{key}"] }
73
+ [req.cookies[COOKIE_TAG], req.cookies[COOKIE_FROM], req.cookies[COOKIE_TIME].to_i, extras]
66
74
  end
67
75
 
68
76
  protected
69
- def bake_cookies(headers, tag, from, time)
77
+ def bake_cookies(headers, tag, from, time, extras)
70
78
  expires = Time.now + @cookie_ttl
71
- { COOKIE_TAG => tag,
72
- COOKIE_FROM => from,
73
- COOKIE_TIME => time }.each do |key, value|
74
- cookie_hash = {:value => value, :expires => expires}
75
- cookie_hash[:domain] = @cookie_domain if @cookie_domain
76
- cookie_hash[:path] = @cookie_path if @cookie_path
77
- Rack::Utils.set_cookie_header!(headers, key, cookie_hash)
78
- end
79
+ data_hash = {
80
+ COOKIE_TAG => tag,
81
+ COOKIE_FROM => from,
82
+ COOKIE_TIME => time
83
+ }
84
+ extras.each { |key, value| data_hash["#{COOKIE_EXTRA_VARS}.#{key}"] = value }
85
+
86
+ data_hash.each do |key, value|
87
+ cookie_hash = {:value => value, :expires => expires}
88
+ cookie_hash[:domain] = @cookie_domain if @cookie_domain
89
+ cookie_hash[:path] = @cookie_path if @cookie_path
90
+ Rack::Utils.set_cookie_header!(headers, key, cookie_hash)
91
+ end
79
92
  end
80
93
  end
81
94
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rack-affiliates"
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Levin"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-affiliates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Levin