search-sniffer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in search-sniffer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 ATimofeev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Search::Sniffer
2
+
3
+ Simple plugin to sniff inbound search terms from popular search engines
4
+
5
+ ## Installation
6
+
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'search-sniffer'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install search-sniffer
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ class ApplicationController < ActionController::Base
26
+ before_filter :sniff_referring_search
27
+ ...
28
+ end
29
+ ```
30
+
31
+ The plugin populates the @@referring_search@ object containing info that can be passed to a keyword highlighter or internal site search engine to pull related content. For an HTTP referer of @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@@
32
+
33
+ ```ruby
34
+ @referring_search.search_terms
35
+ => "ruby rails houston"
36
+ @referring_search.raw
37
+ => "ruby on rails houston"
38
+ @referring_search.engine
39
+ => "google"
40
+ ```
41
+
42
+ Copyright (c) 2008 Squeejee, released under the MIT license
43
+
44
+ Converted to Rails gem by Alexander Timofeev (https://github.com/ATimofeev) 2012
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,91 @@
1
+ # Adapted from http://www.insiderpages.com/rubyonrails/2007/01/get-referring-search-engine-keywords.html
2
+
3
+ module Search
4
+ module Sniffer
5
+
6
+ module ControllerMethods
7
+ # Creates @referring_search containing any referring search engine query minus stop words
8
+ #
9
+ # eg. If the HTTP_REFERER header indicates page referer as:
10
+ # 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
11
+ #
12
+ # then this function will create:
13
+ # @referring_search = "Ruby Rails Houston"
14
+ #
15
+ def sniff_referring_search
16
+ # Check whether referring URL was a search engine result
17
+ # uncomment out the line below to test
18
+ # 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"
19
+ referer = request.env["HTTP_REFERER"]
20
+ @referring_search = ReferringSearch.new(referer)
21
+ true
22
+ end # sniff_referring_search
23
+ end # Module Controller Methods
24
+
25
+ class ReferringSearch
26
+
27
+ attr_reader :search_terms # sanitized search terms
28
+ attr_reader :raw # original terms as typed by user
29
+ attr_reader :engine # search engine
30
+
31
+ def initialize(referer)
32
+
33
+ @search_referers = {
34
+ :google => [/^http:\/\/(www\.)?google.*/, 'q'],
35
+ :yahoo => [/^http:\/\/search\.yahoo.*/, 'p'],
36
+ :msn => [/^http:\/\/search\.msn.*/, 'q'],
37
+ :aol => [/^http:\/\/search\.aol.*/, 'userQuery'],
38
+ :altavista => [/^http:\/\/(www\.)?altavista.*/, 'q'],
39
+ :feedster => [/^http:\/\/(www\.)?feedster.*/, 'q'],
40
+ :lycos => [/^http:\/\/search\.lycos.*/, 'query'],
41
+ :alltheweb => [/^http:\/\/(www\.)?alltheweb.*/, 'q']
42
+ }
43
+
44
+ @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
45
+
46
+ # Get query args
47
+ query_args =
48
+ begin
49
+ URI.split(referer)[7]
50
+ rescue URI::InvalidURIError
51
+ nil
52
+ end
53
+
54
+ # Determine the referring search that was used
55
+ unless referer.blank?
56
+ @search_referers.each do |k, v|
57
+ reg, query_param_name = v
58
+ # Check if the referrer is a search engine we are targetting
59
+ if (reg.match(referer))
60
+
61
+ # set the search engine
62
+ @engine = k
63
+
64
+ unless query_args.empty?
65
+ query_args.split("&").each do |arg|
66
+ pieces = arg.split('=')
67
+ if pieces.length == 2 && pieces.first == query_param_name
68
+ unstopped_keywords = CGI.unescape(pieces.last)
69
+ @raw = unstopped_keywords
70
+ @search_terms = unstopped_keywords.gsub(@stop_words, '').squeeze(' ')
71
+ #logger.info("Referring Search Keywords: #{search_terms}")
72
+ return true
73
+ end
74
+ end
75
+ end # unless
76
+
77
+ return true # because we found a match
78
+ end # if
79
+ end # do
80
+ end #unless
81
+ end #initialize
82
+
83
+ # Return the referring search string instead of the object serialized into a string
84
+ def to_s
85
+ @search_terms
86
+ end
87
+
88
+ end # ReferringSearch
89
+
90
+ end
91
+ end
@@ -0,0 +1,5 @@
1
+ module Search
2
+ module Sniffer
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require "search-sniffer/version"
2
+ require "search-sniffer/search-sniffer"
3
+
4
+
5
+ ActionController::Base.send(:include, Search::Sniffer::ControllerMethods)
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'search-sniffer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "search-sniffer"
8
+ gem.version = Search::Sniffer::VERSION
9
+ gem.authors = ["ATimofeev"]
10
+ gem.email = ["atimofeev@reactant.ru"]
11
+ gem.description = %q{Simple plugin to sniff inbound search terms from popular search engines}
12
+ gem.summary = %q{Squeejee search_sniffer plugin implementation}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search-sniffer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ATimofeev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple plugin to sniff inbound search terms from popular search engines
15
+ email:
16
+ - atimofeev@reactant.ru
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/search-sniffer.rb
26
+ - lib/search-sniffer/search-sniffer.rb
27
+ - lib/search-sniffer/version.rb
28
+ - search-sniffer.gemspec
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Squeejee search_sniffer plugin implementation
53
+ test_files: []
54
+ has_rdoc: