mlangenberg-googlesearch 0.0.3 → 0.0.4

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/googlesearch.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'googlesearch'
3
- s.version = '0.0.3'
3
+ s.version = '0.0.4'
4
4
  s.date = '2009-01-14'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.has_rdoc = false
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  "spec/search_response_spec.rb",
17
17
  "spec/spec_helper.rb",
18
18
  "spec/result_example.xml"]
19
- s.add_dependency("nokogiri", ["> 0.0.0"])
19
+ s.add_dependency("nokogiri", [">= 1.1.1"])
20
20
  end
21
21
 
22
22
 
@@ -1,23 +1,11 @@
1
1
  class SearchResponse
2
+ attr_reader :total_server_time, :total_number_of_results, :results, :index_of_first_result, :index_of_last_result
2
3
  def initialize(xml)
3
4
  doc = Nokogiri::XML(xml)
4
- @tm = doc.root.xpath('TM').text.to_f
5
- @m = doc.root.xpath('RES/M').text.to_i
6
- @results = []
7
- doc.root.xpath('RES//R').each do |res_doc|
8
- @results << SearchResult.new(res_doc)
9
- end
10
- end
11
-
12
- def total_server_time
13
- @tm
14
- end
15
-
16
- def total_number_of_results
17
- @m
18
- end
19
-
20
- def results
21
- @results
5
+ @total_server_time = doc.root.xpath('TM').text.to_f
6
+ @index_of_first_result = doc.root.xpath('RES/@SN').text.to_i
7
+ @index_of_last_result = doc.root.xpath('RES/@EN').text.to_i
8
+ @total_number_of_results = doc.root.xpath('RES/M').text.to_i
9
+ @results = doc.root.xpath('RES//R').map { |res_doc| SearchResult.new(res_doc) }
22
10
  end
23
11
  end
data/lib/googlesearch.rb CHANGED
@@ -9,8 +9,8 @@ class GoogleSearch
9
9
 
10
10
  attr_reader :response
11
11
 
12
- def initialize(options, search_request = SearchRequest.new)
13
- @response = SearchResponse.new search_request.get("http://www.google.com/search?&q=#{options[:q]}&client=google-csbe&output=xml&cx=#{options[:cx]}&cr=lang_#{options[:cr]}&lr=lang_#{options[:lr]}")
12
+ def initialize(o = {}, search_request = SearchRequest.new)
13
+ @response = SearchResponse.new search_request.get("http://www.google.com/search?&q=#{o[:q]}&client=google-csbe&output=xml_no_dtd&cx=#{o[:cx]}&cr=lang_#{o[:cr]}&lr=lang_#{o[:lr]}&start=#{o[:start]}&num=#{o[:num]}")
14
14
  end
15
15
  end
16
16
 
@@ -4,11 +4,11 @@ describe GoogleSearch do
4
4
  it "should be able to create a search query" do
5
5
  request_mock = mock('request')
6
6
  request_mock.should_receive(:get).with(
7
- 'http://www.google.com/search?&q=adres&client=google-csbe&output=xml&cx=unique-cse-id&cr=lang_nl&lr=lang_nl'
7
+ 'http://www.google.com/search?&q=adres&client=google-csbe&output=xml_no_dtd&cx=unique-cse-id&cr=lang_nl&lr=lang_nl&start=0&num=10'
8
8
  ).and_return('wat_xml')
9
9
 
10
10
  SearchResponse.should_receive(:new).with('wat_xml').and_return('search-response-object')
11
- search = GoogleSearch.new({:cx => 'unique-cse-id', :q => 'adres', :cr => 'nl', :lr => 'nl'}, request_mock)
11
+ search = GoogleSearch.new({:cx => 'unique-cse-id', :q => 'adres', :cr => 'nl', :lr => 'nl', :start => 0, :num => 10 }, request_mock)
12
12
  search.response.should == 'search-response-object'
13
13
  end
14
14
  end
@@ -7,7 +7,7 @@
7
7
  <PARAM name="output" value="xml" original_value="xml"/>
8
8
  <PARAM name="cx" value="unique-cse-id" original_value="unique-cse-id"/>
9
9
  <Context><title>Shopr</title></Context><RES SN="1" EN="5">
10
- <M>5</M>
10
+ <M>520</M>
11
11
  <FI/><XT/>
12
12
  <R N="1"><U>http://i3.shop-r.nl/language/nl/pages/39</U><UE>http://i3.shop-r.nl/language/nl/pages/39</UE><T>&lt;b&gt;Intercodam&lt;/b&gt; Tegels B.V.</T><RK>0</RK><S>&lt;b&gt;Intercodam&lt;/b&gt; B.V.. Amstel 135 (vlak naast theater Carre) 1018 EN Amsterdam &lt;b&gt;...&lt;/b&gt; &lt;br&gt; &lt;b&gt;Intercodam&lt;/b&gt; Tegels B.V. 2008. Aenean eget mi. Fusce mattis est id diam. &lt;b&gt;...&lt;/b&gt;</S><LANG>nl</LANG><Label>_cse_4a4weggqfu8</Label><HAS><L/><C SZ="7k" CID="sleD4FLqURwJ"/><RT/></HAS></R>
13
13
 
@@ -12,7 +12,15 @@ describe SearchResponse do
12
12
  end
13
13
 
14
14
  it "should be able to return total number of results" do
15
- @response.total_number_of_results.should == 5
15
+ @response.total_number_of_results.should == 520
16
+ end
17
+
18
+ it "should be able to return the index of the first search result returned in the result set" do
19
+ @response.index_of_first_result.should == 1
20
+ end
21
+
22
+ it "should be able to return the index of the last search result returned in the result set" do
23
+ @response.index_of_last_result.should == 5
16
24
  end
17
25
 
18
26
  it "should be able to return an array of SearchResult objects" do
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.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rene Heino
@@ -18,9 +18,9 @@ dependencies:
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
- - - ">"
21
+ - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.0.0
23
+ version: 1.1.1
24
24
  version:
25
25
  description: Abstraction of the Google CSE XML API
26
26
  email: rails@newminds.nl