google_custom_search 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -5
- data/CHANGELOG.rdoc +6 -3
- data/README.rdoc +20 -8
- data/Rakefile +4 -41
- data/lib/google_custom_search.rb +12 -7
- metadata +16 -18
- data/google_custom_search.gemspec +0 -51
- data/init.rb +0 -1
data/.gitignore
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
pkg
|
1
|
+
pkg/*
|
2
|
+
rdoc/*
|
3
|
+
*.gem
|
4
|
+
.bundle
|
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
= Google Custom Search
|
2
2
|
|
3
|
-
Ruby API to Google Custom Search Engine (http://www.google.com/cse).
|
3
|
+
This project is a Ruby API to Google's Custom Search Engine (http://www.google.com/cse).
|
4
|
+
|
5
|
+
If you want a Google-like search engine for your web site, why not use Google? For $100/yr (more if you have over 1,000 pages) you can get access to Google search results for your site in XML format. The google_custom_search gem helps you access this web service and publish the results on your site however you like (all covered under Google's acceptable use policy).
|
6
|
+
|
7
|
+
<b>Google Custom Search is currently compatible with Rails 2.x and Rails 3.</b>
|
4
8
|
|
5
9
|
|
6
10
|
== 1. Install
|
@@ -30,6 +34,13 @@ You *must* define a constant in your application called <tt>GOOGLE_SEARCH_CX</tt
|
|
30
34
|
|
31
35
|
You can find the CX value for your custom search engine via the search control panel on Google's site (click the "Get code" link and you'll see a hidden "cx" field in the sample HTML form).
|
32
36
|
|
37
|
+
Optionally, you can set default Google search params, such as encoding, by setting up the <tt>GOOGLE_SEARCH_PARAMS</tt> hash in the same initializer:
|
38
|
+
|
39
|
+
GOOGLE_SEARCH_PARAMS = {
|
40
|
+
:ie => 'utf8',
|
41
|
+
:oe => 'utf8'
|
42
|
+
}
|
43
|
+
|
33
44
|
If you're working outside of Rails you'll also need some +require+ statements:
|
34
45
|
|
35
46
|
require 'rubygems'
|
@@ -45,21 +56,22 @@ To perform a search:
|
|
45
56
|
|
46
57
|
The +results+ variable is now a GoogleCustomSearch::ResultSet object:
|
47
58
|
|
48
|
-
results.total
|
49
|
-
results.pages
|
50
|
-
results.suggestion
|
59
|
+
results.total # number of results (integer)
|
60
|
+
results.pages # array of result objects
|
61
|
+
results.suggestion # suggested search term, if any
|
51
62
|
|
52
63
|
Iterate through the results:
|
53
64
|
|
54
|
-
results.pages.each do |
|
55
|
-
|
56
|
-
|
57
|
-
|
65
|
+
results.pages.each do |result|
|
66
|
+
result.title # result title
|
67
|
+
result.url # result URL
|
68
|
+
result.description # excerpt, with terms highlighted
|
58
69
|
end
|
59
70
|
|
60
71
|
|
61
72
|
== Future
|
62
73
|
|
74
|
+
* prevent NameError when GOOGLE_SEARCH_CX is missing: show nice msg
|
63
75
|
* access to all data returned by Google
|
64
76
|
* support for features of CSE free version
|
65
77
|
* support for multiple CSEs in one app (GOOGLE_SEARCH_CX should be a hash)
|
data/Rakefile
CHANGED
@@ -1,21 +1,5 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'jeweler'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = "google_custom_search"
|
8
|
-
gem.summary = %Q{Ruby API to Google Custom Search Engine.}
|
9
|
-
gem.description = %Q{Ruby API to Google Custom Search Engine. Works with the paid version of CSE where you get results in XML format.}
|
10
|
-
gem.email = "alex@alexreisner.com"
|
11
|
-
gem.homepage = "http://github.com/alexreisner/google_custom_search"
|
12
|
-
gem.authors = ["Alex Reisner"]
|
13
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
-
end
|
15
|
-
Jeweler::GemcutterTasks.new
|
16
|
-
rescue LoadError
|
17
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
-
end
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
19
3
|
|
20
4
|
require 'rake/testtask'
|
21
5
|
Rake::TestTask.new(:test) do |test|
|
@@ -24,33 +8,12 @@ Rake::TestTask.new(:test) do |test|
|
|
24
8
|
test.verbose = true
|
25
9
|
end
|
26
10
|
|
27
|
-
begin
|
28
|
-
require 'rcov/rcovtask'
|
29
|
-
Rcov::RcovTask.new do |test|
|
30
|
-
test.libs << 'test'
|
31
|
-
test.pattern = 'test/**/*_test.rb'
|
32
|
-
test.verbose = true
|
33
|
-
end
|
34
|
-
rescue LoadError
|
35
|
-
task :rcov do
|
36
|
-
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
task :test => :check_dependencies
|
41
|
-
|
42
11
|
task :default => :test
|
43
12
|
|
44
13
|
require 'rake/rdoctask'
|
45
14
|
Rake::RDocTask.new do |rdoc|
|
46
|
-
if File.exist?('VERSION')
|
47
|
-
version = File.read('VERSION')
|
48
|
-
else
|
49
|
-
version = ""
|
50
|
-
end
|
51
|
-
|
52
15
|
rdoc.rdoc_dir = 'rdoc'
|
53
|
-
rdoc.title = "Google Custom Search #{
|
54
|
-
rdoc.rdoc_files.include('
|
16
|
+
rdoc.title = "Google Custom Search #{GoogleCustomSearch::VERSION}"
|
17
|
+
rdoc.rdoc_files.include('*.rdoc')
|
55
18
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
19
|
end
|
data/lib/google_custom_search.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
# http://www.google.com/coop/docs/cse/resultsxml.html
|
4
4
|
#
|
5
5
|
module GoogleCustomSearch
|
6
|
+
extend self
|
6
7
|
|
7
8
|
##
|
8
9
|
# Quick Struct-based class to hold a collection of search result data.
|
@@ -17,7 +18,7 @@ module GoogleCustomSearch
|
|
17
18
|
##
|
18
19
|
# Search the site.
|
19
20
|
#
|
20
|
-
def
|
21
|
+
def search(query, offset = 0, length = 20)
|
21
22
|
|
22
23
|
# Get and parse results.
|
23
24
|
url = url(query, offset, length)
|
@@ -42,7 +43,7 @@ module GoogleCustomSearch
|
|
42
43
|
##
|
43
44
|
# Build search request URL.
|
44
45
|
#
|
45
|
-
def
|
46
|
+
def url(query, offset = 0, length = 20)
|
46
47
|
params = {
|
47
48
|
:q => query,
|
48
49
|
:start => offset,
|
@@ -51,13 +52,17 @@ module GoogleCustomSearch
|
|
51
52
|
:output => "xml_no_dtd",
|
52
53
|
:cx => GOOGLE_SEARCH_CX
|
53
54
|
}
|
55
|
+
begin
|
56
|
+
params.merge!(GOOGLE_SEARCH_PARAMS)
|
57
|
+
rescue NameError
|
58
|
+
end
|
54
59
|
"http://www.google.com/search?" + params.to_query
|
55
60
|
end
|
56
61
|
|
57
62
|
##
|
58
63
|
# Query Google, and make sure it responds.
|
59
64
|
#
|
60
|
-
def
|
65
|
+
def fetch_xml(url)
|
61
66
|
begin
|
62
67
|
resp = nil
|
63
68
|
timeout(3) do
|
@@ -71,14 +76,14 @@ module GoogleCustomSearch
|
|
71
76
|
# Transform an array of Google search results (XML parsed by REXML) into
|
72
77
|
# a more useful format.
|
73
78
|
#
|
74
|
-
def
|
79
|
+
def parse_results(results)
|
75
80
|
out = []
|
76
81
|
results = [results] if results.is_a?(Hash) # no array if only one result
|
77
82
|
results.each do |r|
|
78
83
|
out << Result.new(
|
79
|
-
r['U'],
|
80
|
-
r['T'].sub
|
81
|
-
r['S'].gsub
|
84
|
+
r['U'], # url
|
85
|
+
r['T'].try(:sub, / \[[^\]]*\]$/, ''), # title
|
86
|
+
r['S'].try(:gsub, '<br>', '') # desciption
|
82
87
|
)
|
83
88
|
end
|
84
89
|
out
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_custom_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease:
|
5
|
+
version: 0.3.1
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Alex Reisner
|
@@ -9,19 +10,19 @@ autorequire:
|
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date:
|
13
|
+
date: 2011-05-12 00:00:00 -04:00
|
13
14
|
default_executable:
|
14
15
|
dependencies: []
|
15
16
|
|
16
|
-
description: Ruby
|
17
|
-
email:
|
17
|
+
description: Ruby interface to Google Custom Search Engine. Works with the paid version of CSE where you get results in XML format.
|
18
|
+
email:
|
19
|
+
- alex@alexreisner.com
|
18
20
|
executables: []
|
19
21
|
|
20
22
|
extensions: []
|
21
23
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
24
|
-
- README.rdoc
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
25
26
|
files:
|
26
27
|
- .gitignore
|
27
28
|
- CHANGELOG.rdoc
|
@@ -29,8 +30,6 @@ files:
|
|
29
30
|
- README.rdoc
|
30
31
|
- Rakefile
|
31
32
|
- VERSION
|
32
|
-
- google_custom_search.gemspec
|
33
|
-
- init.rb
|
34
33
|
- lib/google_custom_search.rb
|
35
34
|
- test/google_custom_search_test.rb
|
36
35
|
- test/test_helper.rb
|
@@ -39,29 +38,28 @@ homepage: http://github.com/alexreisner/google_custom_search
|
|
39
38
|
licenses: []
|
40
39
|
|
41
40
|
post_install_message:
|
42
|
-
rdoc_options:
|
43
|
-
|
41
|
+
rdoc_options: []
|
42
|
+
|
44
43
|
require_paths:
|
45
44
|
- lib
|
46
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
47
|
requirements:
|
48
48
|
- - ">="
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: "0"
|
51
|
-
version:
|
52
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
53
|
requirements:
|
54
54
|
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
56
|
version: "0"
|
57
|
-
version:
|
58
57
|
requirements: []
|
59
58
|
|
60
59
|
rubyforge_project:
|
61
|
-
rubygems_version: 1.
|
60
|
+
rubygems_version: 1.6.1
|
62
61
|
signing_key:
|
63
62
|
specification_version: 3
|
64
|
-
summary: Ruby
|
65
|
-
test_files:
|
66
|
-
|
67
|
-
- test/test_helper.rb
|
63
|
+
summary: Ruby interface to Google Custom Search Engine.
|
64
|
+
test_files: []
|
65
|
+
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{google_custom_search}
|
8
|
-
s.version = "0.3.0"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Alex Reisner"]
|
12
|
-
s.date = %q{2009-10-14}
|
13
|
-
s.description = %q{Ruby API to Google Custom Search Engine. Works with the paid version of CSE where you get results in XML format.}
|
14
|
-
s.email = %q{alex@alexreisner.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.rdoc"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".gitignore",
|
21
|
-
"CHANGELOG.rdoc",
|
22
|
-
"LICENSE",
|
23
|
-
"README.rdoc",
|
24
|
-
"Rakefile",
|
25
|
-
"VERSION",
|
26
|
-
"google_custom_search.gemspec",
|
27
|
-
"init.rb",
|
28
|
-
"lib/google_custom_search.rb",
|
29
|
-
"test/google_custom_search_test.rb",
|
30
|
-
"test/test_helper.rb"
|
31
|
-
]
|
32
|
-
s.homepage = %q{http://github.com/alexreisner/google_custom_search}
|
33
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
-
s.require_paths = ["lib"]
|
35
|
-
s.rubygems_version = %q{1.3.5}
|
36
|
-
s.summary = %q{Ruby API to Google Custom Search Engine.}
|
37
|
-
s.test_files = [
|
38
|
-
"test/google_custom_search_test.rb",
|
39
|
-
"test/test_helper.rb"
|
40
|
-
]
|
41
|
-
|
42
|
-
if s.respond_to? :specification_version then
|
43
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
-
s.specification_version = 3
|
45
|
-
|
46
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
-
else
|
48
|
-
end
|
49
|
-
else
|
50
|
-
end
|
51
|
-
end
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'google_custom_search'
|