google_search 1.1.0 → 1.2.0
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/Rakefile +20 -26
- data/VERSION +1 -1
- data/google_search.gemspec +25 -28
- data/lib/google_search.rb +1 -2
- data/lib/google_search/google_search.rb +44 -32
- data/spec/google_search_paginated_spec.rb +3 -3
- data/spec/spec_helper.rb +0 -31
- metadata +40 -20
- data/.gitignore +0 -22
- data/spec/spec.opts +0 -1
data/Rakefile
CHANGED
@@ -1,37 +1,31 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'rake'
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
-
end
|
17
|
-
Jeweler::GemcutterTasks.new
|
18
|
-
rescue LoadError
|
19
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
2
|
+
require 'jeweler'
|
3
|
+
|
4
|
+
Jeweler::Tasks.new do |gem|
|
5
|
+
gem.name = "google_search"
|
6
|
+
gem.summary = %Q{Tiny wrapper for Google Search API}
|
7
|
+
# gem.description = %Q{TODO: longer description of your gem}
|
8
|
+
gem.email = "sasa@hakeraj.com"
|
9
|
+
gem.homepage = "http://github.com/fox/google_search"
|
10
|
+
gem.authors = ["Sasa Brankovic"]
|
11
|
+
gem.add_development_dependency "rspec", ">= 2.1.0"
|
12
|
+
gem.add_dependency "json"
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
20
14
|
end
|
21
15
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
Jeweler::RubygemsDotOrgTasks.new
|
17
|
+
|
18
|
+
require 'rspec/core'
|
19
|
+
require 'rspec/core/rake_task'
|
20
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
21
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
26
22
|
end
|
27
23
|
|
28
|
-
|
29
|
-
spec.libs << 'lib' << 'spec'
|
24
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
30
25
|
spec.pattern = 'spec/**/*_spec.rb'
|
31
26
|
spec.rcov = true
|
32
27
|
end
|
33
28
|
|
34
|
-
task :spec => :check_dependencies
|
35
29
|
task :default => :spec
|
36
30
|
|
37
31
|
require 'rake/rdoctask'
|
@@ -39,7 +33,7 @@ Rake::RDocTask.new do |rdoc|
|
|
39
33
|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
34
|
|
41
35
|
rdoc.rdoc_dir = 'rdoc'
|
42
|
-
rdoc.title = "
|
36
|
+
rdoc.title = "test #{version}"
|
43
37
|
rdoc.rdoc_files.include('README*')
|
44
38
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
39
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/google_search.gemspec
CHANGED
@@ -1,61 +1,58 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{google_search}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sasa Brankovic"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-21}
|
13
13
|
s.email = %q{sasa@hakeraj.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
16
|
-
|
16
|
+
"README.rdoc"
|
17
17
|
]
|
18
18
|
s.files = [
|
19
19
|
".document",
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
"spec/spec.opts",
|
32
|
-
"spec/spec_helper.rb"
|
20
|
+
"LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"google_search.gemspec",
|
25
|
+
"lib/google_search.rb",
|
26
|
+
"lib/google_search/google_search.rb",
|
27
|
+
"lib/google_search/google_search_error.rb",
|
28
|
+
"spec/google_search_paginated_spec.rb",
|
29
|
+
"spec/google_search_spec.rb",
|
30
|
+
"spec/spec_helper.rb"
|
33
31
|
]
|
34
32
|
s.homepage = %q{http://github.com/fox/google_search}
|
35
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
36
33
|
s.require_paths = ["lib"]
|
37
|
-
s.rubygems_version = %q{1.3.
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
38
35
|
s.summary = %q{Tiny wrapper for Google Search API}
|
39
36
|
s.test_files = [
|
37
|
+
"spec/google_search_paginated_spec.rb",
|
40
38
|
"spec/google_search_spec.rb",
|
41
|
-
|
42
|
-
"spec/spec_helper.rb"
|
39
|
+
"spec/spec_helper.rb"
|
43
40
|
]
|
44
41
|
|
45
42
|
if s.respond_to? :specification_version then
|
46
43
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
44
|
s.specification_version = 3
|
48
45
|
|
49
|
-
if Gem::Version.new(Gem::
|
50
|
-
s.add_development_dependency(%q<rspec>, [">= 1.
|
51
|
-
s.add_runtime_dependency(%q<
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>, [">= 2.1.0"])
|
48
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
52
49
|
else
|
53
|
-
s.add_dependency(%q<rspec>, [">= 1.
|
54
|
-
s.add_dependency(%q<
|
50
|
+
s.add_dependency(%q<rspec>, [">= 2.1.0"])
|
51
|
+
s.add_dependency(%q<json>, [">= 0"])
|
55
52
|
end
|
56
53
|
else
|
57
|
-
s.add_dependency(%q<rspec>, [">= 1.
|
58
|
-
s.add_dependency(%q<
|
54
|
+
s.add_dependency(%q<rspec>, [">= 2.1.0"])
|
55
|
+
s.add_dependency(%q<json>, [">= 0"])
|
59
56
|
end
|
60
57
|
end
|
61
58
|
|
data/lib/google_search.rb
CHANGED
@@ -1,9 +1,4 @@
|
|
1
1
|
class GoogleSearch
|
2
|
-
##
|
3
|
-
# :singleton-method:
|
4
|
-
# Sets up default options that should be present in every request
|
5
|
-
cattr_accessor :default_options
|
6
|
-
|
7
2
|
##
|
8
3
|
# :singleton-method: web(options)
|
9
4
|
|
@@ -24,36 +19,53 @@ class GoogleSearch
|
|
24
19
|
|
25
20
|
##
|
26
21
|
# :singleton-method: patent(options)
|
22
|
+
class << self
|
23
|
+
##
|
24
|
+
# :singleton-method:
|
25
|
+
# Default options that should be present in every request
|
26
|
+
def default_options
|
27
|
+
@default_options ||= {}
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# Yields the search object for number +pages+ specified.
|
34
|
-
# Each page will contain 8 results, +pages+ must be something enumerable
|
35
|
-
def self.with_pages(pages)
|
36
|
-
pages.each do |page|
|
37
|
-
GoogleSearch.with_options :rsz => "large", :start => (page - 1) * 8 do |search|
|
38
|
-
yield search
|
39
|
-
end
|
30
|
+
##
|
31
|
+
# :singleton-method:
|
32
|
+
# Sets up default options that should be present in every request
|
33
|
+
def default_options=(options)
|
34
|
+
@default_options = options
|
40
35
|
end
|
41
|
-
end
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
def method_missing(method, args) # :nodoc:
|
38
|
+
raise "Unknown search type '#{method}'" unless supported_search_types.include?(method)
|
39
|
+
query(method, args)
|
46
40
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
result
|
41
|
+
|
42
|
+
# Yields the search method for number +pages+ specified.
|
43
|
+
# Each page will contain 8 results, +pages+ must be something enumerable
|
44
|
+
def with_pages(pages)
|
45
|
+
orig_options = default_options.clone
|
46
|
+
pages.each do |page|
|
47
|
+
default_options.merge!(:rsz => "large", :start => (page - 1) * 8)
|
48
|
+
yield
|
49
|
+
end
|
50
|
+
default_options = orig_options
|
58
51
|
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def supported_search_types
|
55
|
+
[:web, :local, :video, :blogs, :news, :books, :images, :patent]
|
56
|
+
end
|
57
|
+
|
58
|
+
def query(type, options)
|
59
|
+
options = default_options.merge(options)
|
60
|
+
options[:v] = "1.0"
|
61
|
+
|
62
|
+
query_string = options.collect { |key, value| "#{key}=#{CGI::escape(value.to_s)}" }.join("&")
|
63
|
+
uri = "http://ajax.googleapis.com/ajax/services/search/#{type}?#{query_string}"
|
64
|
+
|
65
|
+
result = JSON.parse(open(uri).read)
|
66
|
+
raise GoogleSearchError, "#{result['responseStatus']} - #{result['responseDetails']}" unless result["responseStatus"] == 200
|
67
|
+
|
68
|
+
result
|
69
|
+
end
|
70
|
+
end
|
59
71
|
end
|
@@ -4,8 +4,8 @@ describe "Doing a paginated search" do
|
|
4
4
|
context "Searching 3 pages" do
|
5
5
|
before do
|
6
6
|
@results = []
|
7
|
-
GoogleSearch.with_pages(1..3) do
|
8
|
-
@results <<
|
7
|
+
GoogleSearch.with_pages(1..3) do
|
8
|
+
@results << GoogleSearch.images(:q => "moon")
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -16,7 +16,7 @@ describe "Doing a paginated search" do
|
|
16
16
|
it "each page results should originate from its page" do
|
17
17
|
page = 0
|
18
18
|
|
19
|
-
|
19
|
+
@results.each do
|
20
20
|
|result|
|
21
21
|
page.should == result["responseData"]["cursor"]["currentPageIndex"].to_i
|
22
22
|
page += 1
|
data/spec/spec_helper.rb
CHANGED
@@ -1,32 +1 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
-
|
4
1
|
require 'google_search'
|
5
|
-
require 'spec'
|
6
|
-
require 'spec/autorun'
|
7
|
-
|
8
|
-
Spec::Runner.configure do |config|
|
9
|
-
config.before :all do
|
10
|
-
# The open method, used by GoogleSearch, will get cached so we
|
11
|
-
# don't bombard Google each time we do testing. Cache is stored
|
12
|
-
# in tmp/cached_open.
|
13
|
-
GoogleSearch.should_receive(:open).any_number_of_times.and_return { |url| cached_open(url) }
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
require 'digest/md5'
|
18
|
-
require 'fileutils'
|
19
|
-
|
20
|
-
def cached_open(url)
|
21
|
-
dir = "tmp/cached_open"
|
22
|
-
FileUtils::mkdir_p(dir) unless File.exists?(dir)
|
23
|
-
|
24
|
-
path = File.join(dir, Digest::MD5.hexdigest(url))
|
25
|
-
|
26
|
-
unless File.exists?(path)
|
27
|
-
content = open(url).read
|
28
|
-
File.open(path, "w") { |f| f.puts(content) }
|
29
|
-
end
|
30
|
-
|
31
|
-
open(path)
|
32
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 1.2.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Sasa Brankovic
|
@@ -9,29 +15,39 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-11-21 00:00:00 +01:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: rspec
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
29
|
+
hash: 11
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
version: 2.1.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
25
37
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
|
28
|
-
|
29
|
-
|
38
|
+
name: json
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
30
42
|
requirements:
|
31
43
|
- - ">="
|
32
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
33
48
|
version: "0"
|
34
|
-
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
35
51
|
description:
|
36
52
|
email: sasa@hakeraj.com
|
37
53
|
executables: []
|
@@ -43,7 +59,6 @@ extra_rdoc_files:
|
|
43
59
|
- README.rdoc
|
44
60
|
files:
|
45
61
|
- .document
|
46
|
-
- .gitignore
|
47
62
|
- LICENSE
|
48
63
|
- README.rdoc
|
49
64
|
- Rakefile
|
@@ -54,37 +69,42 @@ files:
|
|
54
69
|
- lib/google_search/google_search_error.rb
|
55
70
|
- spec/google_search_paginated_spec.rb
|
56
71
|
- spec/google_search_spec.rb
|
57
|
-
- spec/spec.opts
|
58
72
|
- spec/spec_helper.rb
|
59
73
|
has_rdoc: true
|
60
74
|
homepage: http://github.com/fox/google_search
|
61
75
|
licenses: []
|
62
76
|
|
63
77
|
post_install_message:
|
64
|
-
rdoc_options:
|
65
|
-
|
78
|
+
rdoc_options: []
|
79
|
+
|
66
80
|
require_paths:
|
67
81
|
- lib
|
68
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
69
84
|
requirements:
|
70
85
|
- - ">="
|
71
86
|
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
72
90
|
version: "0"
|
73
|
-
version:
|
74
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
75
93
|
requirements:
|
76
94
|
- - ">="
|
77
95
|
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
97
|
+
segments:
|
98
|
+
- 0
|
78
99
|
version: "0"
|
79
|
-
version:
|
80
100
|
requirements: []
|
81
101
|
|
82
102
|
rubyforge_project:
|
83
|
-
rubygems_version: 1.3.
|
103
|
+
rubygems_version: 1.3.7
|
84
104
|
signing_key:
|
85
105
|
specification_version: 3
|
86
106
|
summary: Tiny wrapper for Google Search API
|
87
107
|
test_files:
|
88
|
-
- spec/google_search_spec.rb
|
89
108
|
- spec/google_search_paginated_spec.rb
|
109
|
+
- spec/google_search_spec.rb
|
90
110
|
- spec/spec_helper.rb
|
data/.gitignore
DELETED
data/spec/spec.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|