Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
37 README.md
@@ -1,8 +1,7 @@
Rack::Referrals
=============
-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.
-
+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.
Purpose
-------
@@ -38,19 +37,17 @@ Add the middleware to your application stack:
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.
- class ExampleController < ApplicationController
-
- def index
- str = if request.env['affiliate.tag] && affiliate = User.find_by_affiliate_tag(request.env['affiliate.tag'])
- "Howdy, referral! You've been referred here by #{affiliate.name} and from #{request.env['affiliate.from']} @ #{Time.at(env['affiliate.time'])}"
- else
- "We're so glad you found us on your own!"
+ class ExampleController < ApplicationController
+ def index
+ str = if request.env['affiliate.tag] && affiliate = User.find_by_affiliate_tag(request.env['affiliate.tag'])
+ "Howdy, referral! You've been referred here by #{affiliate.name} and from #{request.env['affiliate.from']} @ #{Time.at(env['affiliate.time'])}"
+ else
+ "We're so glad you found us on your own!"
+ end
+
+ render :text => str
end
-
- render :text => str
end
-
- end
Customization
@@ -59,9 +56,21 @@ Customization
You can customize default parameter name <code>ref</code> by providing <code>:param</code> option.
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).
+ #Rails 3 in config/application.rb
class Application < Rails::Application
...
- config.middleware.use Rack::Referrals.new :param => 'aff_id', :ttl => 3.months
+ config.middleware.use Rack::Affiliates.new :param => 'aff_id', :ttl => 3.months
...
end
+The <code>:domain</code> option allows to customize cookie domain.
+
+ class Application < Rails::Application
+ ...
+ config.middleware.use Rack::Affiliates.new :domain => '.example.org'
+ ...
+ end
+
+Middleware will set cookie on '.example.org' domain so it's accessible on 'www.example.org', 'app.example.org' etc.
+
+
View
2  VERSION
@@ -1 +1 @@
-0.1.1
+0.2.0
View
20 lib/rack-affiliates.rb
@@ -1,18 +1,19 @@
module Rack
#
# Rack Middleware for extracting information from the request params and cookies.
- # It populates +env['affiliate.tag']+ and
- # +env['affiliate.from']+ if it detects a request came from an affiliated link
+ # It populates +env['affiliate.tag']+, # +env['affiliate.from']+ and
+ # +env['affiliate.time'] if it detects a request came from an affiliated link
#
class Affiliates
COOKIE_TAG = "aff_tag"
COOKIE_FROM = "aff_from"
- COOKIE_WHEN = "aff_time"
+ COOKIE_TIME = "aff_time"
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
end
def call(env)
@@ -55,16 +56,19 @@ def params_info(req)
end
def cookie_info(req)
- [req.cookies[COOKIE_TAG], req.cookies[COOKIE_FROM], req.cookies[COOKIE_WHEN].to_i]
+ [req.cookies[COOKIE_TAG], req.cookies[COOKIE_FROM], req.cookies[COOKIE_TIME].to_i]
end
protected
def bake_cookies(res, tag, from, time)
expires = Time.now + @cookie_ttl
-
- res.set_cookie(COOKIE_TAG, :value => tag, :path => "/", :expires => expires)
- res.set_cookie(COOKIE_FROM, :value => from, :path => "/", :expires => expires)
- res.set_cookie(COOKIE_WHEN, :value => time, :path => "/", :expires => expires)
+ { COOKIE_TAG => tag,
+ COOKIE_FROM => from,
+ COOKIE_TIME => time }.each do |key, value|
+ cookie_hash = {:value => value, :expires => expires}
+ cookie_hash[:domain] = @cookie_domain if @cookie_domain
+ res.set_cookie(key, cookie_hash)
+ end
end
end
end
View
4 rack-affiliates.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = "rack-affiliates"
- s.version = "0.1.1"
+ s.version = "0.2.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Alex Levin"]
- s.date = "2011-12-19"
+ s.date = "2011-12-20"
s.description = "If the user clicked through from an affiliated site, this middleware will track affiliate tag, referring url and time."
s.email = "experiment17@gmail.com"
s.extra_rdoc_files = [

No commit comments for this range

Something went wrong with that request. Please try again.