Permalink
Browse files

Initial functioning version

  • Loading branch information...
0 parents commit 691a0a350761a4318da883609f91bbd55d175841 Kali Donovan committed Oct 7, 2011
Showing with 131 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +7 −0 Rakefile
  4. +45 −0 lib/rack-referrals.rb
  5. +5 −0 lib/rack-referrals/version.rb
  6. +27 −0 rack-referrals.gemspec
  7. +39 −0 test/test_referrals.rb
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in rack-referrals.gemspec
+gemspec
@@ -0,0 +1,7 @@
+require "bundler/gem_tasks"
+require 'rake/testtask'
+
+Rake::TestTask.new do |task|
+ task.test_files = FileList['test/test_*.rb']
+end
+
@@ -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
@@ -0,0 +1,5 @@
+module Rack
+ class Referrals
+ VERSION = "0.0.1"
+ end
+end
@@ -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
@@ -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.