mlangenberg-googlesearch 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/googlesearch.gemspec +2 -2
- data/lib/googlesearch.rb +12 -12
- data/spec/google_search_spec.rb +3 -5
- metadata +2 -2
data/googlesearch.gemspec
CHANGED
data/lib/googlesearch.rb
CHANGED
@@ -10,6 +10,7 @@ require 'nokogiri'
|
|
10
10
|
class GoogleSearch
|
11
11
|
|
12
12
|
attr_reader :response
|
13
|
+
attr_reader :url
|
13
14
|
|
14
15
|
def initialize(o = {}, search_request = SearchRequest.new)
|
15
16
|
|
@@ -90,15 +91,15 @@ class GoogleSearch
|
|
90
91
|
# Optional. The c2coff parameter enables or disables the Simplified and Traditional Chinese Search feature.
|
91
92
|
|
92
93
|
|
93
|
-
o[:client] ||= "google-csbe"
|
94
|
+
#o[:client] ||= "google-csbe"
|
94
95
|
# Required!!! The client parameter must be set to google-csbe if you use Google Custom Search Service.
|
95
96
|
|
96
97
|
|
97
|
-
o[:cr] = "
|
98
|
+
#o[:cr] = "country#{o[:cr].upcase}" if o[:cr]
|
98
99
|
# Optional. The cr parameter restricts search results to documents originating in a particular country.
|
99
100
|
|
100
101
|
|
101
|
-
o[:cx] ||= ""
|
102
|
+
#o[:cx] ||= ""
|
102
103
|
# Required!!! The cx parameter specifies a unique code that identifies a custom search engine. You must specify a Custom Search Engine using the cx parameter to retrieve search results from that CSE.
|
103
104
|
|
104
105
|
|
@@ -117,11 +118,11 @@ class GoogleSearch
|
|
117
118
|
#o[:ie] ||= "utf8"
|
118
119
|
# Optional. The ie parameter sets the character encoding scheme that should be used to interpret the query string. The default ie value is latin1.
|
119
120
|
|
120
|
-
o[:lr] = "lang_#{o[:lr]}" if o[:lr]
|
121
|
+
#o[:lr] = "lang_#{o[:lr].downcase}" if o[:lr]
|
121
122
|
# Optional. The lr (language restrict) parameter restricts search results to documents written in a particular language.
|
122
123
|
|
123
124
|
|
124
|
-
o[:num] ||= 10
|
125
|
+
#o[:num] ||= 10
|
125
126
|
# Optional. The num parameter identifies the number of search results to return. The default num value is 10, and the maximum value is 20. If you request more than 20 results, only 20 results will be returned.
|
126
127
|
|
127
128
|
|
@@ -132,11 +133,11 @@ class GoogleSearch
|
|
132
133
|
#o[:oe] ||= "utf8"
|
133
134
|
# Optional. The oe parameter sets the character encoding scheme that should be used to decode the XML result. The default oe value is latin1.
|
134
135
|
|
135
|
-
o[:output] ||= "xml_no_dtd"
|
136
|
+
#o[:output] ||= "xml_no_dtd"
|
136
137
|
# Required. The output parameter specifies the format of the XML results. The only valid values for this parameter are xml and xml_no_dtd. The chart below explains how these parameter values differ.
|
137
138
|
|
138
139
|
|
139
|
-
o[:q] ||= ""
|
140
|
+
#o[:q] ||= ""
|
140
141
|
# Optional. The q parameter specifies the search query entered by the user. Even though this parameter is optional, you must specify a value for at least one of the query parameters (as_epq, as_lq, as_oq, as_q, as_rq) to get search results.
|
141
142
|
|
142
143
|
|
@@ -144,7 +145,7 @@ class GoogleSearch
|
|
144
145
|
# Optional. The safe parameter indicates how search results should be filtered for adult and pornographic content. The default value for the safe parameter is off. Valid parameter values are: off, medium or high
|
145
146
|
|
146
147
|
|
147
|
-
o[:start] ||= 10
|
148
|
+
#o[:start] ||= 10
|
148
149
|
# Optional. The start parameter indicates the first matching result that should be included in the search results. The start parameter uses a zero-based index, meaning the first result is 0, the second result is 1 and so forth.
|
149
150
|
|
150
151
|
|
@@ -162,9 +163,8 @@ class GoogleSearch
|
|
162
163
|
|
163
164
|
|
164
165
|
# And finally:
|
165
|
-
@
|
166
|
-
|
167
|
-
)
|
166
|
+
@url = "http://www.google.com/search?" + o.map {|key, value| "#{key}=#{value}"}.join('&')
|
167
|
+
@response = SearchResponse.new(search_request.get(url))
|
168
168
|
|
169
169
|
end
|
170
170
|
end
|
@@ -173,4 +173,4 @@ class SearchRequest
|
|
173
173
|
def get(uri)
|
174
174
|
open(uri)
|
175
175
|
end
|
176
|
-
end
|
176
|
+
end
|
data/spec/google_search_spec.rb
CHANGED
@@ -3,12 +3,10 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
3
3
|
describe GoogleSearch do
|
4
4
|
it "should be able to create a search query" do
|
5
5
|
request_mock = mock('request')
|
6
|
-
request_mock.should_receive(:get).with(
|
7
|
-
'http://www.google.com/search?cx=unique-cse-id&cr=lang_nl&output=xml_no_dtd&q=adres&lr=lang_nl&client=google-csbe&start=0&num=10'
|
8
|
-
).and_return('wat_xml')
|
6
|
+
request_mock.should_receive(:get).with('http://www.google.com/search?cx=unique-cse-id&q=adres&cr=countryNL&lr=lang_nl').and_return('wat_xml')
|
9
7
|
|
10
|
-
SearchResponse.should_receive(:new).with('wat_xml'
|
11
|
-
search = GoogleSearch.new({:cx => 'unique-cse-id', :q => 'adres', :cr => '
|
8
|
+
SearchResponse.should_receive(:new).with('wat_xml').and_return('search-response-object')
|
9
|
+
search = GoogleSearch.new({:cx => 'unique-cse-id', :q => 'adres', :cr => 'countryNL', :lr => 'lang_nl'}, request_mock)
|
12
10
|
search.response.should == 'search-response-object'
|
13
11
|
end
|
14
12
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mlangenberg-googlesearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rene Heino
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2009-
|
14
|
+
date: 2009-02-04 00:00:00 -08:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|