google_cse 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/CHANGELOG.rdoc +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +56 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/google_cse.gemspec +52 -0
- data/init.rb +1 -0
- data/lib/google_cse.rb +104 -0
- data/test/google_cse_test.rb +6 -0
- data/test/test_helper.rb +4 -0
- metadata +76 -0
data/.gitignore
ADDED
data/CHANGELOG.rdoc
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Igor Vodafon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
= Google Custom Search
|
2
|
+
|
3
|
+
This project is a Ruby API to Google's Custom Search Engine (http://www.google.com/cse).
|
4
|
+
This plugin is rewrite plugin from Alex Reisner (http://github.com/alexreisner/google_custom_search.git),
|
5
|
+
because the old plugin not working.
|
6
|
+
|
7
|
+
== 1. Install
|
8
|
+
|
9
|
+
Install either as a Rails plugin:
|
10
|
+
|
11
|
+
rails plugin install git://github.com/vodafon/google_cse.git
|
12
|
+
|
13
|
+
or as a gem:
|
14
|
+
|
15
|
+
# add to Gemfile:
|
16
|
+
gem "google_cse"
|
17
|
+
|
18
|
+
# at command prompt:
|
19
|
+
bundle install
|
20
|
+
|
21
|
+
or as a standalone gem (outside of Rails):
|
22
|
+
|
23
|
+
sudo gem install google_cse
|
24
|
+
|
25
|
+
|
26
|
+
== 2. Configure
|
27
|
+
|
28
|
+
You *must* define a constant in your application called <tt>CX_GOOGLE_CSE</tt>. For example, if you're using Rails, create a file <tt>config/initializers/google_cse.rb</tt>:
|
29
|
+
|
30
|
+
CX_GOOGLE_CSE = "..."
|
31
|
+
|
32
|
+
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).
|
33
|
+
|
34
|
+
== 3. Use
|
35
|
+
|
36
|
+
To perform a search:
|
37
|
+
|
38
|
+
results = GoogleCustomSearch.search("Hank Aaron", 0)
|
39
|
+
|
40
|
+
The second parameter is a start parameter for search query. 0 - first page (1-10 result), 10 - second page, etc.
|
41
|
+
|
42
|
+
The +results+ variable is now a GoogleCustomSearch::ResultSet object:
|
43
|
+
|
44
|
+
results.pages # array of pages data
|
45
|
+
results.results # array of result objects
|
46
|
+
|
47
|
+
Iterate through the results:
|
48
|
+
|
49
|
+
results.results.each do |result|
|
50
|
+
result.title # result title
|
51
|
+
result.url # result URL
|
52
|
+
result.description # excerpt, with terms highlighted
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
Copyright (c) 2010 Igor Vodafon, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "google_cse"
|
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 = "vodafon.ua@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/vodafon/google_cse"
|
12
|
+
gem.authors = ["Igor Vodafon"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :test => :check_dependencies
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
if File.exist?('VERSION')
|
46
|
+
version = File.read('VERSION')
|
47
|
+
else
|
48
|
+
version = ""
|
49
|
+
end
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "Google CSE #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/google_cse.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{google_cse}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Igor Vodafon"]
|
12
|
+
s.date = %q{2010-09-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{vodafon.ua@gmail.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_cse.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"lib/google_cse.rb",
|
29
|
+
"test/google_cse_test.rb",
|
30
|
+
"test/test_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/vodafon/google_cse}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.7}
|
36
|
+
s.summary = %q{Ruby API to Google Custom Search Engine.}
|
37
|
+
s.test_files = [
|
38
|
+
"test/test_helper.rb",
|
39
|
+
"test/google_cse_test.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::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'google_cse'
|
data/lib/google_cse.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module GoogleCustomSearch
|
2
|
+
|
3
|
+
##
|
4
|
+
# Search result data.
|
5
|
+
#
|
6
|
+
class ResultSet < Struct.new(:pages, :results); end
|
7
|
+
|
8
|
+
##
|
9
|
+
# Single search result data.
|
10
|
+
#
|
11
|
+
class Result < Struct.new(:url, :title, :description); end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Pages data
|
15
|
+
#
|
16
|
+
|
17
|
+
class Start < Struct.new(:start, :label); end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Search the site.
|
21
|
+
#
|
22
|
+
def self.search(query, start)
|
23
|
+
# Get and parse results.
|
24
|
+
url = url(query, start)
|
25
|
+
json = fetch_json(url)
|
26
|
+
data = Crack::JSON.parse(json)
|
27
|
+
|
28
|
+
# Extract and return pages data and search result data.
|
29
|
+
if data['responseData']
|
30
|
+
if data['responseData']['cursor']['pages']
|
31
|
+
ResultSet.new(
|
32
|
+
parse_start(data['responseData']['cursor']['pages']),
|
33
|
+
parse_results(data['responseData']['results'])
|
34
|
+
)
|
35
|
+
else
|
36
|
+
ResultSet.new(
|
37
|
+
false, #return false if pages < 1
|
38
|
+
parse_results(data['responseData']['results'])
|
39
|
+
)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
ResultSet.new(0, [])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
##
|
50
|
+
# Build search request URL.
|
51
|
+
#
|
52
|
+
def self.url(query, start)
|
53
|
+
query = CGI::escape(query)
|
54
|
+
"http://www.google.com/uds/GwebSearch?context=0&lstkp=0&rsz=filtered_cse&hl=ru&source=gcsc&gss=.com&cx=#{CX_GOOGLE_CSE}&q=#{query}&start=#{start}&v=1.0"
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Query Google.
|
59
|
+
#
|
60
|
+
def self.fetch_json(url)
|
61
|
+
begin
|
62
|
+
resp = nil
|
63
|
+
timeout(3) do
|
64
|
+
resp = Net::HTTP.get_response(URI.parse(url))
|
65
|
+
end
|
66
|
+
rescue SocketError, TimeoutError;
|
67
|
+
end
|
68
|
+
(resp and resp.code == "200") ? resp.body : nil
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Transform an array of Google search results into
|
73
|
+
# a more useful format.
|
74
|
+
#
|
75
|
+
def self.parse_results(results)
|
76
|
+
out = []
|
77
|
+
results = results
|
78
|
+
results.each do |r|
|
79
|
+
out << Result.new(
|
80
|
+
r['url'], # url
|
81
|
+
r['title'], # title
|
82
|
+
r['content'] # desciption
|
83
|
+
)
|
84
|
+
end
|
85
|
+
out
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Transform an array of Google pages info into
|
90
|
+
# a mare useful format.
|
91
|
+
|
92
|
+
def self.parse_start(results)
|
93
|
+
out = []
|
94
|
+
results = results
|
95
|
+
results.each do |r|
|
96
|
+
out << Start.new(
|
97
|
+
r['start'], # pages start
|
98
|
+
r['label'] # pages label
|
99
|
+
)
|
100
|
+
end
|
101
|
+
out
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_cse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Igor Vodafon
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-14 00:00:00 +03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Ruby API to Google Custom Search Engine. Works with the paid version of CSE where you get results in XML format.
|
22
|
+
email: vodafon.ua@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- CHANGELOG.rdoc
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- google_cse.gemspec
|
38
|
+
- init.rb
|
39
|
+
- lib/google_cse.rb
|
40
|
+
- test/google_cse_test.rb
|
41
|
+
- test/test_helper.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/vodafon/google_cse
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Ruby API to Google Custom Search Engine.
|
74
|
+
test_files:
|
75
|
+
- test/test_helper.rb
|
76
|
+
- test/google_cse_test.rb
|