Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 691a0a3
Showing
7 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.gem | ||
.bundle | ||
Gemfile.lock | ||
pkg/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in rack-referrals.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "bundler/gem_tasks" | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |task| | ||
task.test_files = FileList['test/test_*.rb'] | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "rack-referrals/version" | ||
|
||
module Rack | ||
class Referrals | ||
|
||
def initialize(app, options = {}) | ||
@app, @options = app, options | ||
|
||
@engines = { | ||
:google => [/^http:\/\/(www\.)?google.*/, 'q'], | ||
:yahoo => [/^http:\/\/search\.yahoo.*/, 'p'], | ||
:msn => [/^http:\/\/search\.msn.*/, 'q'], | ||
:aol => [/^http:\/\/search\.aol.*/, 'userQuery'], | ||
:altavista => [/^http:\/\/(www\.)?altavista.*/, 'q'], | ||
:feedster => [/^http:\/\/(www\.)?feedster.*/, 'q'], | ||
:lycos => [/^http:\/\/search\.lycos.*/, 'query'], | ||
:alltheweb => [/^http:\/\/(www\.)?alltheweb.*/, 'q'] | ||
} | ||
|
||
end | ||
|
||
def call(env) | ||
request = Rack::Request.new(env) | ||
from = request.env["HTTP_REFERER"] | ||
|
||
if from.to_s.length > 0 | ||
if engine = @engines.detect {|name, data| from =~ data[0] } | ||
env["referring.search_engine"] = engine[0].to_s | ||
reg, param_name = engine[1] | ||
|
||
# Pull out the query string from the referring search engine | ||
query_string = begin | ||
URI.split(from)[7] | ||
rescue URI::InvalidURIError; nil | ||
end | ||
|
||
env["referring.search_phrase"] = query_string && Rack::Utils.parse_query(query_string)[param_name] | ||
end | ||
end | ||
|
||
@app.call(env) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Rack | ||
class Referrals | ||
VERSION = "0.0.1" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- encoding: utf-8 -*- | ||
$:.push File.expand_path("../lib", __FILE__) | ||
require "rack-referrals/version" | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "rack-referrals" | ||
s.version = Rack::Referrals::VERSION | ||
s.authors = ["Kali Donovan"] | ||
s.email = ["kali@deviantech.com"] | ||
s.homepage = "" | ||
s.summary = %q{TODO: Write a gem summary} | ||
s.description = %q{TODO: Write a gem description} | ||
|
||
s.rubyforge_project = "rack-referrals" | ||
|
||
s.files = `git ls-files`.split("\n") | ||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } | ||
s.require_paths = ["lib"] | ||
|
||
# specify any dependencies here; for example: | ||
s.add_development_dependency "test-unit" | ||
s.add_development_dependency "rack-test" | ||
s.add_development_dependency "mocha" | ||
s.add_development_dependency "rake" | ||
# s.add_runtime_dependency "rest-client" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "test/unit" | ||
require "rack/test" | ||
|
||
require File.expand_path(File.dirname(__FILE__) + '/../lib/rack-referrals') | ||
|
||
class ReferralTest < Test::Unit::TestCase | ||
include Rack::Test::Methods | ||
|
||
def app | ||
hello_world_app = lambda do |env| | ||
[200, {}, "Hello World"] | ||
end | ||
|
||
Rack::Referrals.new(hello_world_app) | ||
end | ||
|
||
def test_parses_google | ||
get '/', nil, 'HTTP_REFERER' => "http://www.google.com/search?q=ruby+on+rails&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a" | ||
|
||
assert_equal "google", last_request.env['referring.search_engine'] | ||
assert_equal "ruby on rails", last_request.env['referring.search_phrase'] | ||
end | ||
|
||
def test_handles_missing_params | ||
get '/', nil, 'HTTP_REFERER' => "http://www.google.com/search?ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a" | ||
|
||
assert_equal "google", last_request.env['referring.search_engine'] | ||
assert_equal nil, last_request.env['referring.search_phrase'] | ||
end | ||
|
||
def test_handles_no_referer | ||
get '/' | ||
|
||
assert_equal nil, last_request.env['referring.search_engine'] | ||
assert_equal nil, last_request.env['referring.search_phrase'] | ||
end | ||
|
||
|
||
end |