rack-search_terms 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +25 -0
- data/MIT-LICENSE.txt +19 -0
- data/README.md +57 -0
- data/Rakefile +2 -0
- data/lib/rack-search_terms.rb +39 -0
- data/rack-search_terms.gemspec +23 -0
- data/spec/middleware_spec.rb +37 -0
- data/spec/spec_helper.rb +22 -0
- metadata +110 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rack-search_terms (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
rspec (2.1.0)
|
11
|
+
rspec-core (~> 2.1.0)
|
12
|
+
rspec-expectations (~> 2.1.0)
|
13
|
+
rspec-mocks (~> 2.1.0)
|
14
|
+
rspec-core (2.1.0)
|
15
|
+
rspec-expectations (2.1.0)
|
16
|
+
diff-lcs (~> 1.1.2)
|
17
|
+
rspec-mocks (2.1.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
bundler (>= 1.0.0)
|
24
|
+
rack-search_terms!
|
25
|
+
rspec (~> 2.1.0)
|
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Paul Bowsher
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
Rack::SearchTerms
|
2
|
+
=================
|
3
|
+
|
4
|
+
* Repository: [http://github.com/boffbowsh/rack-search_terms](http://github.com/boffbowsh/rack-search_terms)
|
5
|
+
|
6
|
+
Description
|
7
|
+
-----------
|
8
|
+
|
9
|
+
Rack::SearchTerms gives your app access to the search terms that drove the visitor to your site. Simply grab `env['search_terms']` in your Rack app and you're away!
|
10
|
+
|
11
|
+
Rails 3 Quick Start
|
12
|
+
-------------------
|
13
|
+
|
14
|
+
Add to your Gemfile:
|
15
|
+
|
16
|
+
gem 'rack-search_terms'
|
17
|
+
|
18
|
+
In config/environment.rb, add:
|
19
|
+
|
20
|
+
config.middleware.use "Rack::SearchTerms"
|
21
|
+
|
22
|
+
Using with non-Rails Rack apps
|
23
|
+
------------------------------
|
24
|
+
|
25
|
+
Just `use Rack::SearchTerms` as any other middleware.
|
26
|
+
|
27
|
+
Use Cases
|
28
|
+
---------
|
29
|
+
|
30
|
+
Our use case for this is to help our 404 page. If someone is referred from a Search Engine to an unknown page (it happens!), then we do a lookup against our internal search engine and hopefully return what the user was looking for.
|
31
|
+
|
32
|
+
You could also use it to highlight search terms within your page, but this [isn't advised](http://gilesbowkett.blogspot.com/2007/01/stop-highlighting-referrer-search-terms.html) ;)
|
33
|
+
|
34
|
+
Contributing to Rack::SearchTerms
|
35
|
+
-----------------------
|
36
|
+
|
37
|
+
I encourage you to:
|
38
|
+
|
39
|
+
* Fork the project.
|
40
|
+
* Make your feature addition, bug fix, or support for additional search engine.
|
41
|
+
* Add specs for it.
|
42
|
+
* Send me a pull request. Bonus points for topic branches.
|
43
|
+
|
44
|
+
Author
|
45
|
+
------
|
46
|
+
|
47
|
+
Maintained by [Paul Bowsher](http://www.twitter.com/boffbowsh/)
|
48
|
+
|
49
|
+
Thanks
|
50
|
+
------
|
51
|
+
|
52
|
+
Thanks to Bryan Helmkamp for the Quick Start section of this README :)
|
53
|
+
|
54
|
+
License
|
55
|
+
-------
|
56
|
+
|
57
|
+
See MIT-LICENSE.txt in this directory.
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class SearchTerms
|
5
|
+
include Rack::Utils
|
6
|
+
|
7
|
+
def initialize app
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call env
|
12
|
+
@env = env
|
13
|
+
@env.merge! 'search_terms' => search_terms
|
14
|
+
@app.call @env
|
15
|
+
end
|
16
|
+
|
17
|
+
def referer
|
18
|
+
@env['HTTP_REFERER']
|
19
|
+
end
|
20
|
+
|
21
|
+
def search_terms
|
22
|
+
case referer
|
23
|
+
when /^http:\/\/www\.google\..*?\/imgres/
|
24
|
+
prev = terms_from_param 'prev'
|
25
|
+
parse_query(prev.split('?')[1])['q']
|
26
|
+
when /^http:\/\/www\.google\..*?\//
|
27
|
+
terms_from_param 'q'
|
28
|
+
when /^http:\/\/www.bing.com/
|
29
|
+
terms_from_param 'q'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def terms_from_param param
|
34
|
+
parse_query(URI.parse(referer).query)[param]
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.version; '0.1.0'; end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/rack-search_terms", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "rack-search_terms"
|
6
|
+
s.version = Rack::SearchTerms.version
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ['Paul Bowsher']
|
9
|
+
s.email = ['paul.bowsher@gmail.com']
|
10
|
+
s.homepage = "http://rubygems.org/gems/rack-search_terms"
|
11
|
+
s.summary = "Rack Middleware to extract search terms from the referring search engine"
|
12
|
+
s.description = "Rack::SearchTerms gives your app access to the search terms that drove the visitor to your site"
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
s.rubyforge_project = "rack-search_terms"
|
16
|
+
|
17
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
18
|
+
s.add_development_dependency "rspec", "~> 2.1.0"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
22
|
+
s.require_path = 'lib'
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Rack::SearchTerms do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
@app = mock('app')
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should pass through environment to the underlying app" do
|
10
|
+
expect_env 'HTTP_REFERER' => 'http://www.example.com/', 'x-foo' => 'x-bar'
|
11
|
+
Rack::SearchTerms.new(@app).call({'HTTP_REFERER' => 'http://www.example.com/', 'x-foo' => 'x-bar'})
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should pass the search terms and engine on to underlying app" do
|
15
|
+
expect_env 'search_terms' => 'how now brown cow'
|
16
|
+
mock_request "http://www.google.com/?q=how%20now%20brown%20cow" do |middleware|
|
17
|
+
middleware.stub(:search_terms) {'how now brown cow'}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "term parsing" do
|
22
|
+
it_should_work_for "Google", :from => "http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBUQFjAA&url=http%3A%2F%2Fwww.rawnet.com%2Fblog%2Fmake-my-logo-bigger&rct=j&q=rawnet%20logo&ei=6CPXTJSmCMq6jAehivCwCQ&usg=AFQjCNFa4cEzkMUJXaEpTWedLibVVVOz-w&sig2=eOu7V345cOW9xh9HjG36hQ", :as => 'rawnet logo'
|
23
|
+
it_should_work_for "Google UK", :from => "http://www.google.co.uk/url?sa=t&source=web&cd=1&sqi=2&ved=0CBUQFjAA&url=http%3A%2F%2Fwww.rawnet.com%2Fblog%2Fmake-my-logo-bigger&rct=j&q=rawnet%20logo&ei=sSPXTKn1OIK6hAex86n5BA&usg=AFQjCNFa4cEzkMUJXaEpTWedLibVVVOz-w&sig2=NfPNOdDvdT_u4JBsxH-OTQ", :as => 'rawnet logo'
|
24
|
+
it_should_work_for "Google Images", :from => "http://www.google.com/imgres?imgurl=http://www.rawnet.com/system/logos/18/thumb/logo.jpg%3F1286466114&imgrefurl=http://www.rawnet.com/news&usg=__Ag-l2Kt0yNDIUoOby8Q8bEKbWQ8=&h=66&w=95&sz=18&hl=en&start=0&sig2=YrkGnaNORciIKLuxBZm3_w&zoom=1&tbnid=OmO0aEU9ZGSuUM:&tbnh=61&tbnw=88&ei=YyPXTJGtLIOzhAef7sTFBQ&prev=/images%3Fq%3Drawnet%2Blogo%26hl%3Den%26safe%3Doff%26qscrl%3D1%26biw%3D1196%26bih%3D888%26tbs%3Disch:1&itbs=1&iact=hc&vpx=206&vpy=314&dur=1089&hovh=61&hovw=88&tx=107&ty=17&oei=YyPXTJGtLIOzhAef7sTFBQ&esq=1&page=1&ndsp=26&ved=1t:429,r:6,s:0", :as => 'rawnet logo'
|
25
|
+
it_should_work_for "Bing", :from => 'http://www.bing.com/search?q=rawnet+logo&go=&form=QBLH&filt=all&qs=n&sk=', :as => 'rawnet logo'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should fail silently if not a search engine" do
|
29
|
+
expect_env 'HTTP_REFERER' => 'http://www.example.com/', 'search_terms' => nil
|
30
|
+
mock_request "http://www.example.com/"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should fail silently if a search engine with no terms" do
|
34
|
+
expect_env 'HTTP_REFERER' => 'http://www.google.com/', 'search_terms' => nil
|
35
|
+
mock_request "http://www.google.com/"
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
Rspec.configure do |c|
|
4
|
+
c.mock_with :rspec
|
5
|
+
end
|
6
|
+
|
7
|
+
def expect_env hash={}
|
8
|
+
@app.should_receive(:call).with( hash_including(hash) )
|
9
|
+
end
|
10
|
+
|
11
|
+
def mock_request referer
|
12
|
+
middleware = Rack::SearchTerms.new(@app)
|
13
|
+
yield middleware if block_given?
|
14
|
+
middleware.call({'HTTP_REFERER' => referer})
|
15
|
+
end
|
16
|
+
|
17
|
+
def it_should_work_for name, options
|
18
|
+
it "should work for #{name}" do
|
19
|
+
expect_env 'search_terms' => options[:as]
|
20
|
+
mock_request options[:from]
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-search_terms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Paul Bowsher
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-07 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: bundler
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 11
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 1
|
49
|
+
- 0
|
50
|
+
version: 2.1.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: Rack::SearchTerms gives your app access to the search terms that drove the visitor to your site
|
54
|
+
email:
|
55
|
+
- paul.bowsher@gmail.com
|
56
|
+
executables: []
|
57
|
+
|
58
|
+
extensions: []
|
59
|
+
|
60
|
+
extra_rdoc_files: []
|
61
|
+
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- MIT-LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/rack-search_terms.rb
|
70
|
+
- rack-search_terms.gemspec
|
71
|
+
- spec/middleware_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
has_rdoc: true
|
74
|
+
homepage: http://rubygems.org/gems/rack-search_terms
|
75
|
+
licenses: []
|
76
|
+
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
hash: 23
|
97
|
+
segments:
|
98
|
+
- 1
|
99
|
+
- 3
|
100
|
+
- 6
|
101
|
+
version: 1.3.6
|
102
|
+
requirements: []
|
103
|
+
|
104
|
+
rubyforge_project: rack-search_terms
|
105
|
+
rubygems_version: 1.3.7
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: Rack Middleware to extract search terms from the referring search engine
|
109
|
+
test_files: []
|
110
|
+
|