rack-affiliates 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ gem 'rack'
3
+
4
+ group :development do
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.6.4"
7
+ gem "rcov", ">= 0"
8
+ gem "rack-test"
9
+ gem "minitest"
10
+ gem "timecop"
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Alex Levin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ Rack::Referrals
2
+ =============
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
+
6
+
7
+ Purpose
8
+ -------
9
+
10
+ Affiliate links tracking is very common task if you want to promote your online business. This middleware helps you to do that.
11
+
12
+ Installation
13
+ ------------
14
+
15
+ Piece a cake:
16
+
17
+ gem install rack-affiliates
18
+
19
+
20
+ Rails 3 Example Usage
21
+ ---------------------
22
+
23
+ Add the middleware to your application stack:
24
+
25
+ # Rails 3 App - in config/application.rb
26
+ class Application < Rails::Application
27
+ ...
28
+ config.middleware.use Rack::Affiliates
29
+ ...
30
+ end
31
+
32
+ # Rails 2 App - in config/environment.rb
33
+ Rails::Initializer.run do |config|
34
+ ...
35
+ config.middleware.use "Rack::Affiliates"
36
+ ...
37
+ end
38
+
39
+ 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
+
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!"
48
+ end
49
+
50
+ render :text => str
51
+ end
52
+
53
+ end
54
+
55
+
56
+ Customization
57
+ -------------
58
+
59
+ You can customize default parameter name <code>ref</code> by providing <code>:param</code> option.
60
+ 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
+
62
+ class Application < Rails::Application
63
+ ...
64
+ config.middleware.use Rack::Referrals.new :param => 'aff_id', :ttl => 3.months
65
+ ...
66
+ end
67
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rack-affiliates"
18
+ gem.homepage = "http://github.com/alexlevin/rack-affiliates"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Tracks referrals came via an affiliated links.}
21
+ gem.description = %Q{If the user clicked through from an affiliated site, this middleware will track affiliate tag, referring url and time.}
22
+ gem.email = "experiment17@gmail.com"
23
+ gem.authors = ["Alex Levin"]
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:spec) do |test|
29
+ test.libs << 'lib' << 'spec'
30
+ test.pattern = 'spec/**/*_spec.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ require 'rcov/rcovtask'
35
+ Rcov::RcovTask.new do |test|
36
+ test.libs << 'spec'
37
+ test.pattern = 'spec/**/*_spec.rb'
38
+ test.verbose = true
39
+ test.rcov_opts << '--exclude "gems/*"'
40
+ end
41
+
42
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,70 @@
1
+ module Rack
2
+ #
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
6
+ #
7
+ class Affiliates
8
+ COOKIE_TAG = "aff_tag"
9
+ COOKIE_FROM = "aff_from"
10
+ COOKIE_WHEN = "aff_time"
11
+
12
+ def initialize(app, opts = {})
13
+ @app = app
14
+ @param = opts[:param] || "ref"
15
+ @cookie_ttl = opts[:ttl] || 60*60*24*30 # 30 days
16
+ end
17
+
18
+ def call(env)
19
+ req = Rack::Request.new(env)
20
+
21
+ params_tag = req.params[@param]
22
+ cookie_tag = req.cookies[COOKIE_TAG]
23
+
24
+ if cookie_tag
25
+ tag, from, time = cookie_info(req)
26
+ end
27
+
28
+ if params_tag && params_tag != cookie_tag
29
+ tag, from, time = params_info(req)
30
+ end
31
+
32
+ if tag
33
+ env["affiliate.tag"] = tag
34
+ env['affiliate.from'] = from
35
+ env['affiliate.time'] = time
36
+ end
37
+
38
+ status, headers, body = @app.call(env)
39
+
40
+ response = Rack::Response.new body, status, headers
41
+
42
+ if tag != cookie_tag
43
+ bake_cookies(response, tag, from, time)
44
+ end
45
+
46
+ response.finish
47
+ end
48
+
49
+ def affiliate_info(req)
50
+ params_info(req) || cookie_info(req)
51
+ end
52
+
53
+ def params_info(req)
54
+ [req.params[@param], req.env["HTTP_REFERER"], Time.now.to_i]
55
+ end
56
+
57
+ def cookie_info(req)
58
+ [req.cookies[COOKIE_TAG], req.cookies[COOKIE_FROM], req.cookies[COOKIE_WHEN].to_i]
59
+ end
60
+
61
+ protected
62
+ def bake_cookies(res, tag, from, time)
63
+ 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)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "rack-affiliates"
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alex Levin"]
12
+ s.date = "2011-12-19"
13
+ s.description = "If the user clicked through from an affiliated site, this middleware will track affiliate tag, referring url and time."
14
+ s.email = "experiment17@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rack-affiliates.rb",
27
+ "rack-affiliates.gemspec",
28
+ "spec/helper.rb",
29
+ "spec/rack_affiliates_spec.rb"
30
+ ]
31
+ s.homepage = "http://github.com/alexlevin/rack-affiliates"
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = "1.8.10"
35
+ s.summary = "Tracks referrals came via an affiliated links."
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
42
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
43
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
44
+ s.add_development_dependency(%q<rcov>, [">= 0"])
45
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
46
+ s.add_development_dependency(%q<minitest>, [">= 0"])
47
+ s.add_development_dependency(%q<timecop>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<rack>, [">= 0"])
50
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
52
+ s.add_dependency(%q<rcov>, [">= 0"])
53
+ s.add_dependency(%q<rack-test>, [">= 0"])
54
+ s.add_dependency(%q<minitest>, [">= 0"])
55
+ s.add_dependency(%q<timecop>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rack>, [">= 0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ s.add_dependency(%q<rack-test>, [">= 0"])
63
+ s.add_dependency(%q<minitest>, [">= 0"])
64
+ s.add_dependency(%q<timecop>, [">= 0"])
65
+ end
66
+ end
67
+
data/spec/helper.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ require "minitest/spec"
13
+ require "minitest/autorun"
14
+ require "rack/test"
15
+ require 'timecop'
16
+
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+ require 'rack-affiliates'
20
+
21
+ class MiniTest::Unit::TestCase
22
+ include Rack::Test::Methods
23
+
24
+ def app
25
+ hello_world_app = lambda do |env|
26
+ [200, {}, "Hello World"]
27
+ end
28
+
29
+ @app = Rack::Affiliates.new(hello_world_app)
30
+ end
31
+ end
32
+
@@ -0,0 +1,88 @@
1
+ require 'helper'
2
+
3
+ describe "RackAffiliates" do
4
+ before :each do
5
+ clear_cookies
6
+ end
7
+
8
+ it "should handle empty affiliate info" do
9
+ get '/'
10
+
11
+ last_request.env['affiliate.tag'].must_equal nil
12
+ last_request.env['affiliate.from'].must_equal nil
13
+ last_request.env['affiliate.time'].must_equal nil
14
+ end
15
+
16
+ it "should set affiliate info from params" do
17
+ Timecop.freeze do
18
+ @time = Time.now
19
+ get '/', {'ref'=>'123'}, 'HTTP_REFERER' => "http://www.foo.com"
20
+ end
21
+
22
+ last_request.env['affiliate.tag'].must_equal "123"
23
+ last_request.env['affiliate.from'].must_equal "http://www.foo.com"
24
+ last_request.env['affiliate.time'].must_equal @time.to_i
25
+ end
26
+
27
+ it "should save affiliate info in a cookie" do
28
+ Timecop.freeze do
29
+ @time = Time.now
30
+ get '/', {'ref'=>'123'}, 'HTTP_REFERER' => "http://www.foo.com"
31
+ end
32
+
33
+ rack_mock_session.cookie_jar["aff_tag"].must_equal "123"
34
+ rack_mock_session.cookie_jar["aff_from"].must_equal "http://www.foo.com"
35
+ rack_mock_session.cookie_jar["aff_time"].must_equal "#{@time.to_i}"
36
+ end
37
+
38
+ describe "when cookie exists" do
39
+ before :each do
40
+ @time = Time.now
41
+ clear_cookies
42
+ set_cookie("aff_tag=123")
43
+ set_cookie("aff_from=http://www.foo.com")
44
+ set_cookie("aff_time=#{@time.to_i}")
45
+ end
46
+
47
+ it "should restore affiliate info from cookie" do
48
+ Timecop.freeze do
49
+ get '/', {}, 'HTTP_REFERER' => "http://www.bar.com"
50
+ end
51
+
52
+ last_request.env['affiliate.tag'].must_equal "123"
53
+ last_request.env['affiliate.from'].must_equal "http://www.foo.com"
54
+ last_request.env['affiliate.time'].must_equal @time.to_i
55
+ end
56
+
57
+ it 'should not update existing cookie' do
58
+ Timecop.freeze(60*60*24) do #1 day later
59
+ get '/', {}, 'HTTP_REFERER' => "http://www.bar.com"
60
+ end
61
+
62
+ last_request.env['affiliate.tag'].must_equal "123"
63
+ last_request.env['affiliate.from'].must_equal "http://www.foo.com"
64
+
65
+ # should not change timestamp of older cookie
66
+ last_request.env['affiliate.time'].must_equal @time.to_i
67
+
68
+ rack_mock_session.cookie_jar["aff_tag"].must_equal "123"
69
+ rack_mock_session.cookie_jar["aff_from"].must_equal "http://www.foo.com"
70
+ rack_mock_session.cookie_jar["aff_time"].must_equal "#{@time.to_i}"
71
+ end
72
+
73
+ it "should use newer affiliate from params" do
74
+ Timecop.freeze(60*60*24) do #1 day later
75
+ @new_time = Time.now
76
+ get '/', {'ref' => 456}, 'HTTP_REFERER' => "http://www.bar.com"
77
+ end
78
+
79
+ rack_mock_session.cookie_jar["aff_tag"].must_equal "456"
80
+ rack_mock_session.cookie_jar["aff_from"].must_equal "http://www.bar.com"
81
+ rack_mock_session.cookie_jar["aff_time"].must_equal "#{@new_time.to_i}"
82
+
83
+ last_request.env['affiliate.tag'].must_equal "456"
84
+ last_request.env['affiliate.from'].must_equal "http://www.bar.com"
85
+ last_request.env['affiliate.time'].must_equal @new_time.to_i
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-affiliates
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Alex Levin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-19 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ version_requirements: *id001
31
+ name: rack
32
+ prerelease: false
33
+ type: :runtime
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ hash: 23
41
+ segments:
42
+ - 1
43
+ - 0
44
+ - 0
45
+ version: 1.0.0
46
+ version_requirements: *id002
47
+ name: bundler
48
+ prerelease: false
49
+ type: :development
50
+ - !ruby/object:Gem::Dependency
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 7
57
+ segments:
58
+ - 1
59
+ - 6
60
+ - 4
61
+ version: 1.6.4
62
+ version_requirements: *id003
63
+ name: jeweler
64
+ prerelease: false
65
+ type: :development
66
+ - !ruby/object:Gem::Dependency
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ version_requirements: *id004
77
+ name: rcov
78
+ prerelease: false
79
+ type: :development
80
+ - !ruby/object:Gem::Dependency
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ version_requirements: *id005
91
+ name: rack-test
92
+ prerelease: false
93
+ type: :development
94
+ - !ruby/object:Gem::Dependency
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ version_requirements: *id006
105
+ name: minitest
106
+ prerelease: false
107
+ type: :development
108
+ - !ruby/object:Gem::Dependency
109
+ requirement: &id007 !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ version_requirements: *id007
119
+ name: timecop
120
+ prerelease: false
121
+ type: :development
122
+ description: If the user clicked through from an affiliated site, this middleware will track affiliate tag, referring url and time.
123
+ email: experiment17@gmail.com
124
+ executables: []
125
+
126
+ extensions: []
127
+
128
+ extra_rdoc_files:
129
+ - LICENSE.txt
130
+ - README.md
131
+ files:
132
+ - .document
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - VERSION
138
+ - lib/rack-affiliates.rb
139
+ - rack-affiliates.gemspec
140
+ - spec/helper.rb
141
+ - spec/rack_affiliates_spec.rb
142
+ homepage: http://github.com/alexlevin/rack-affiliates
143
+ licenses:
144
+ - MIT
145
+ post_install_message:
146
+ rdoc_options: []
147
+
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ hash: 3
156
+ segments:
157
+ - 0
158
+ version: "0"
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ hash: 3
165
+ segments:
166
+ - 0
167
+ version: "0"
168
+ requirements: []
169
+
170
+ rubyforge_project:
171
+ rubygems_version: 1.8.10
172
+ signing_key:
173
+ specification_version: 3
174
+ summary: Tracks referrals came via an affiliated links.
175
+ test_files: []
176
+