rack-affiliates 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  Rack::Referrals
2
2
  =============
3
3
 
4
- Rack::Affiliates is a rack middleware that extracts information about the referrals came from an an affiliated site. Specifically, it looks up for parameter (eg. ref_id) in the request or a saved cookie. If found it persists referal tag in the cookie for later use.
5
-
4
+ Rack::Affiliates is a rack middleware that extracts information about the referrals came from an an affiliated site. Specifically, it looks up for parameter (eg. <code>ref</code> by default) in the request. If found it persists referal tag, referring url and time in a cookie for later use.
6
5
 
7
6
  Purpose
8
7
  -------
@@ -38,19 +37,17 @@ Add the middleware to your application stack:
38
37
 
39
38
  Now you can check any request to see who came to your site via an affiliated link and use this information in your application. Moreover, referrer_id is saved in the cookie and will come into play if user returns to your site later.
40
39
 
41
- class ExampleController < ApplicationController
42
-
43
- def index
44
- str = if request.env['affiliate.tag] && affiliate = User.find_by_affiliate_tag(request.env['affiliate.tag'])
45
- "Howdy, referral! You've been referred here by #{affiliate.name} and from #{request.env['affiliate.from']} @ #{Time.at(env['affiliate.time'])}"
46
- else
47
- "We're so glad you found us on your own!"
40
+ class ExampleController < ApplicationController
41
+ def index
42
+ str = if request.env['affiliate.tag] && affiliate = User.find_by_affiliate_tag(request.env['affiliate.tag'])
43
+ "Howdy, referral! You've been referred here by #{affiliate.name} and from #{request.env['affiliate.from']} @ #{Time.at(env['affiliate.time'])}"
44
+ else
45
+ "We're so glad you found us on your own!"
46
+ end
47
+
48
+ render :text => str
48
49
  end
49
-
50
- render :text => str
51
50
  end
52
-
53
- end
54
51
 
55
52
 
56
53
  Customization
@@ -59,9 +56,21 @@ Customization
59
56
  You can customize default parameter name <code>ref</code> by providing <code>:param</code> option.
60
57
  If you want to save your affiliate id for later use, you can specify time to live with <code>:ttl</code> option (default is 30 days).
61
58
 
59
+ #Rails 3 in config/application.rb
62
60
  class Application < Rails::Application
63
61
  ...
64
- config.middleware.use Rack::Referrals.new :param => 'aff_id', :ttl => 3.months
62
+ config.middleware.use Rack::Affiliates.new :param => 'aff_id', :ttl => 3.months
65
63
  ...
66
64
  end
67
65
 
66
+ The <code>:domain</code> option allows to customize cookie domain.
67
+
68
+ class Application < Rails::Application
69
+ ...
70
+ config.middleware.use Rack::Affiliates.new :domain => '.example.org'
71
+ ...
72
+ end
73
+
74
+ Middleware will set cookie on '.example.org' domain so it's accessible on 'www.example.org', 'app.example.org' etc.
75
+
76
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,18 +1,19 @@
1
1
  module Rack
2
2
  #
3
3
  # Rack Middleware for extracting information from the request params and cookies.
4
- # It populates +env['affiliate.tag']+ and
5
- # +env['affiliate.from']+ if it detects a request came from an affiliated link
4
+ # It populates +env['affiliate.tag']+, # +env['affiliate.from']+ and
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
- COOKIE_WHEN = "aff_time"
10
+ COOKIE_TIME = "aff_time"
11
11
 
12
12
  def initialize(app, opts = {})
13
13
  @app = app
14
14
  @param = opts[:param] || "ref"
15
15
  @cookie_ttl = opts[:ttl] || 60*60*24*30 # 30 days
16
+ @cookie_domain = opts[:domain] || nil
16
17
  end
17
18
 
18
19
  def call(env)
@@ -55,16 +56,19 @@ module Rack
55
56
  end
56
57
 
57
58
  def cookie_info(req)
58
- [req.cookies[COOKIE_TAG], req.cookies[COOKIE_FROM], req.cookies[COOKIE_WHEN].to_i]
59
+ [req.cookies[COOKIE_TAG], req.cookies[COOKIE_FROM], req.cookies[COOKIE_TIME].to_i]
59
60
  end
60
61
 
61
62
  protected
62
63
  def bake_cookies(res, tag, from, time)
63
64
  expires = Time.now + @cookie_ttl
64
-
65
- res.set_cookie(COOKIE_TAG, :value => tag, :path => "/", :expires => expires)
66
- res.set_cookie(COOKIE_FROM, :value => from, :path => "/", :expires => expires)
67
- res.set_cookie(COOKIE_WHEN, :value => time, :path => "/", :expires => expires)
65
+ { COOKIE_TAG => tag,
66
+ COOKIE_FROM => from,
67
+ COOKIE_TIME => time }.each do |key, value|
68
+ cookie_hash = {:value => value, :expires => expires}
69
+ cookie_hash[:domain] = @cookie_domain if @cookie_domain
70
+ res.set_cookie(key, cookie_hash)
71
+ end
68
72
  end
69
73
  end
70
74
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rack-affiliates"
8
- s.version = "0.1.1"
8
+ s.version = "0.2.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"]
12
- s.date = "2011-12-19"
12
+ s.date = "2011-12-20"
13
13
  s.description = "If the user clicked through from an affiliated site, this middleware will track affiliate tag, referring url and time."
14
14
  s.email = "experiment17@gmail.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-affiliates
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Levin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-19 00:00:00 Z
18
+ date: 2011-12-20 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  requirement: &id001 !ruby/object:Gem::Requirement