Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit c8faa45ec8cdecf8d7d6b319629e0758aa413c13 Wynn Netherland committed Jul 15, 2008
Showing with 143 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +23 −0 README.textile
  3. +22 −0 Rakefile
  4. +2 −0 init.rb
  5. +62 −0 lib/search_sniffer.rb
  6. +4 −0 tasks/search_sniffer_tasks.rake
  7. +9 −0 test/search_sniffer_test.rb
  8. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
+h2. SearchSniffer
+
+Simple plugin to sniff inbound search terms from popular search engines
+
+h3. Installation
+
+Installation is easy from Github
+
+
+
+h3. Usage
+
+
+ class ApplicationController < ActionController::Base
+ before_filter :sniff_referring_search
+
+ ...
+ end
+
+The plugin populates the @@referring_search@ variable which can then be passed to a keyword highlighter or internal site search engine to pull related content.
+
+
+Copyright (c) 2008 Wynn Netherland, Squeejee, released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the search_sniffer plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the search_sniffer plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'SearchSniffer'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,2 @@
+require 'search_sniffer'
+ActionController::Base.send(:include, Squeejee::SearchSniffer)
@@ -0,0 +1,62 @@
+# Adapted from http://www.insiderpages.com/rubyonrails/2007/01/get-referring-search-engine-keywords.html
+
+module Squeejee #:nodoc:
+ module SearchSniffer #:nodoc:
+ # Creates @referring_search containing any referring search engine query minus stop words
+ #
+ # eg. If the HTTP_REFERER header indicates page referer as:
+ # http://www.google.com/search?q=ruby+on+rails+houston&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
+ #
+ # then this function will create:
+ # @referring_search = "Ruby Rails Houston"
+ #
+ def sniff_referring_search
+ # Check whether referring URL was a search engine result
+ # uncomment out the line below to test
+ #request.env["HTTP_REFERER"] = "http://www.google.com/search?q=ruby+on+rails+houston&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a"
+ referer = request.env["HTTP_REFERER"]
+ unless referer.blank?
+ search_referers = [
+ [/^http:\/\/(www\.)?google.*/, 'q'],
+ [/^http:\/\/search\.yahoo.*/, 'p'],
+ [/^http:\/\/search\.msn.*/, 'q'],
+ [/^http:\/\/search\.aol.*/, 'userQuery'],
+ [/^http:\/\/(www\.)?altavista.*/, 'q'],
+ [/^http:\/\/(www\.)?feedster.*/, 'q'],
+ [/^http:\/\/search\.lycos.*/, 'query'],
+ [/^http:\/\/(www\.)?alltheweb.*/, 'q']
+ ]
+ query_args =
+ begin
+ URI.split(referer)[7]
+ rescue URI::InvalidURIError
+ nil
+ end
+ search_referers.each do |reg, query_param_name|
+ # Check if the referrer is a search engine we are targetting
+ if (reg.match(referer))
+
+ # Highlight the Search Term Keywords on the page
+ #@javascripts.push('keyword_highlighter')
+
+ # Create a globally scoped variable (@referring_search) containing the referring Search Engine Query
+ unless query_args.empty?
+ query_args.split("&").each do |arg|
+ pieces = arg.split('=')
+ if pieces.length == 2 && pieces.first == query_param_name
+ unstopped_keywords = CGI.unescape(pieces.last)
+ stop_words = /\b(\d+|\w|about|after|also|an|and|are|as|at|be|because|before|between|but|by|can|com|de|do|en|for|from|has|how|however|htm|html|if|i|in|into|is|it|la|no|of|on|or|other|out|since|site|such|than|that|the|there|these|this|those|to|under|upon|vs|was|what|when|where|whether|which|who|will|with|within|without|www|you|your)\b/i
+ @referring_search = unstopped_keywords.gsub(stop_words, '').squeeze(' ')
+ logger.info("Referring Search Keywords: #{@referring_search}")
+ return true
+ end
+ end
+ end
+ return true
+ end
+ end
+ end
+ true
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :search_sniffer do
+# # Task goes here
+# end
@@ -0,0 +1,9 @@
+require 'test/unit'
+
+class SearchSnifferTest < Test::Unit::TestCase
+
+ # Replace this with your real tests.
+ def test_this_plugin
+ true
+ end
+end
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit c8faa45

Please sign in to comment.