Skip to content

Commit

Permalink
Initial functioning version
Browse files Browse the repository at this point in the history
  • Loading branch information
kdonovan committed Oct 7, 2011
0 parents commit 691a0a3
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in rack-referrals.gemspec
gemspec
7 changes: 7 additions & 0 deletions Rakefile
@@ -0,0 +1,7 @@
require "bundler/gem_tasks"
require 'rake/testtask'

Rake::TestTask.new do |task|
task.test_files = FileList['test/test_*.rb']
end

45 changes: 45 additions & 0 deletions lib/rack-referrals.rb
@@ -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
5 changes: 5 additions & 0 deletions lib/rack-referrals/version.rb
@@ -0,0 +1,5 @@
module Rack
class Referrals
VERSION = "0.0.1"
end
end
27 changes: 27 additions & 0 deletions rack-referrals.gemspec
@@ -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
39 changes: 39 additions & 0 deletions test/test_referrals.rb
@@ -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

0 comments on commit 691a0a3

Please sign in to comment.