mlangenberg-googlesearch 0.0.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.
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'googlesearch'
3
+ s.version = '0.0.1'
4
+ s.date = '2009-01-07'
5
+ s.platform = Gem::Platform::RUBY
6
+ s.has_rdoc = false
7
+ s.summary = 'Google CSE implementation'
8
+ s.description = 'Abstraction of the Google CSE XML API'
9
+ s.authors = ["Rene Heino", "Matthijs Langenberg"]
10
+ s.email = 'rails@newminds.nl'
11
+ s.files = [ "googlesearch.gemspec",
12
+ "lib/googlesearch.rb",
13
+ "lib/googlesearch/search_response.rb",
14
+ "lib/googlesearch/search_result.rb"]
15
+ s.test_files = ["spec/google_search_spec.rb",
16
+ "spec/search_response_spec.rb",
17
+ "spec/spec_helper.rb",
18
+ "spec/result_example.xml"]
19
+ s.add_dependency("nokogiri", ["> 0.0.0"])
20
+ end
21
+
22
+
23
+
@@ -0,0 +1,18 @@
1
+ require 'googlesearch/search_response'
2
+ require 'googlesearch/search_result'
3
+ require 'open-uri'
4
+
5
+ class GoogleSearch
6
+
7
+ attr_reader :response
8
+
9
+ def initialize(options, search_request = SearchRequest.new)
10
+ @response = SearchResponse.new search_request.get("http://www.google.com/search?&q=#{options[:q]}&client=google-csbe&output=xml&cx=#{options[:cx]}")
11
+ end
12
+ end
13
+
14
+ class SearchRequest
15
+ def get(uri)
16
+ open(uri)
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ require 'nokogiri'
2
+
3
+ class SearchResponse
4
+ def initialize(xml)
5
+ doc = Nokogiri::XML(xml)
6
+ @tm = doc.root.xpath('TM').text.to_f
7
+ @m = doc.root.xpath('RES/M').text.to_i
8
+ @results = []
9
+ doc.root.xpath('RES//R').each do |res_doc|
10
+ @results << SearchResult.new(res_doc)
11
+ end
12
+ end
13
+
14
+ def total_server_time
15
+ @tm
16
+ end
17
+
18
+ def total_number_of_results
19
+ @m
20
+ end
21
+
22
+ def results
23
+ @results
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ class SearchResult
2
+ attr_reader :url, :index, :title, :excerpt
3
+ def initialize(doc)
4
+ @url = doc.xpath('U').text
5
+ @index = doc.attributes["N"].to_i
6
+ @title = doc.xpath('T').text
7
+ @excerpt = doc.xpath('S').text
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe GoogleSearch do
4
+ it do
5
+ request_mock = mock('request')
6
+ request_mock.should_receive(:get).with(
7
+ 'http://www.google.com/search?&q=adres&client=google-csbe&output=xml&cx=unique-cse-id'
8
+ ).and_return('wat_xml')
9
+
10
+ SearchResponse.should_receive(:new).with('wat_xml').and_return('search-response-object')
11
+ search = GoogleSearch.new({:cx => 'unique-cse-id', :q => 'adres'}, request_mock)
12
+ search.response.should == 'search-response-object'
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
2
+ <!DOCTYPE GSP SYSTEM "google.dtd">
3
+ <GSP VER="3.2">
4
+ <TM>0.458025</TM><Q>intercodam</Q>
5
+ <PARAM name="q" value="intercodam" original_value="intercodam"/>
6
+ <PARAM name="client" value="google-csbe" original_value="google-csbe"/>
7
+ <PARAM name="output" value="xml" original_value="xml"/>
8
+ <PARAM name="cx" value="unique-cse-id" original_value="unique-cse-id"/>
9
+ <Context><title>Shopr</title></Context><RES SN="1" EN="5">
10
+ <M>5</M>
11
+ <FI/><XT/>
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
+
14
+ <R N="2"><U>http://i3.shop-r.nl/language/nl/</U><UE>http://i3.shop-r.nl/language/nl/</UE><T>&lt;b&gt;Intercodam&lt;/b&gt; Tegels B.V.</T><RK>0</RK><S>&lt;b&gt;Intercodam&lt;/b&gt; Tegels B.V. 2008. Aenean eget mi. Fusce mattis est id diam. Phasellus &lt;br&gt; faucibus interdum sapien. Duis quis nunc. Sed enim.</S><LANG>nl</LANG><Label>_cse_4a4weggqfu8</Label><HAS><L/><C SZ="6k" CID="zvDK6Ic-qNMJ"/><RT/></HAS></R>
15
+
16
+ <R N="3"><U>http://i3.shop-r.nl/language/nl/pages/41</U><UE>http://i3.shop-r.nl/language/nl/pages/41</UE><T>&lt;b&gt;Intercodam&lt;/b&gt; Tegels B.V.</T><RK>0</RK><S>De &amp;#39;NV &lt;b&gt;Intercodam&lt;/b&gt;&amp;#39; is opgericht op 16 september 1919 en handelde in de meest &lt;br&gt; uiteenlopende producten. In 1972 werd deze NV omgezet in een Beheer B.V. &lt;b&gt;...&lt;/b&gt;</S><LANG>nl</LANG><Label>_cse_4a4weggqfu8</Label><HAS><L/><C SZ="6k" CID="7FB2NBtfU-UJ"/><RT/></HAS></R>
17
+
18
+ <R N="4" MIME="application/pdf"><U>http://i3.shop-r.nl/shops/i3.shop-r.nl/assets/REFIN%20ARDENNES.pdf</U><UE>http://i3.shop-r.nl/shops/i3.shop-r.nl/assets/REFIN%2520ARDENNES.pdf</UE><T>refin - ardennes</T><RK>0</RK><S>&lt;b&gt;INTERCODAM&lt;/b&gt;. refin - ardennes. maart 2008. &lt;b&gt;Intercodam&lt;/b&gt; Tegels BV. Amstel 135 (&lt;br&gt; naast theater Carré). 1018 EN Amsterdam. tel 020-6225115. fax 020-6243354 &lt;b&gt;...&lt;/b&gt;</S><LANG>nl</LANG><Label>_cse_4a4weggqfu8</Label><HAS><L/><C SZ="" CID="3gRs0rPYzpYJ"/><RT/></HAS></R>
19
+
20
+ <R N="5" MIME="application/pdf"><U>http://i3.shop-r.nl/shops/i3.shop-r.nl/assets/CAESAR%20MORE.pdf</U><UE>http://i3.shop-r.nl/shops/i3.shop-r.nl/assets/CAESAR%2520MORE.pdf</UE><T>caesar - more</T><RK>0</RK><S>&lt;b&gt;INTERCODAM&lt;/b&gt;. caesar - more. maart 2008. &lt;b&gt;Intercodam&lt;/b&gt; Tegels BV. Amstel 135 (naast &lt;br&gt; theater Carré). 1018 EN Amsterdam. tel 020-6225115. fax 020-6243354 &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>_cse_4a4weggqfu8</Label><HAS><L/><C SZ="" CID="jzBwnKAFGckJ"/><RT/></HAS></R>
21
+
22
+ </RES>
23
+ </GSP>
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe SearchResponse do
4
+
5
+ setup do
6
+ example_xml = File.read(File.join(File.dirname(__FILE__), 'result_example.xml'))
7
+ @response = SearchResponse.new(example_xml)
8
+ end
9
+
10
+ it "shoud be able to return total server time in seconds" do
11
+ @response.total_server_time.should == 0.458025
12
+ end
13
+
14
+ it "should be able to return total number of results" do
15
+ @response.total_number_of_results.should == 5
16
+ end
17
+
18
+ it "should be able to return the first URL of the results" do
19
+ @response.results.first.url.should == 'http://i3.shop-r.nl/language/nl/pages/39'
20
+ end
21
+
22
+ it "should be able to return the second index of the results" do
23
+ @response.results[1].index.should == 2
24
+ end
25
+
26
+ it "should be able to return the last title of the results" do
27
+ @response.results.last.title.should == 'caesar - more'
28
+ end
29
+
30
+ it "should be able to return the third text of the results" do
31
+ @response.results[2].excerpt.should == %[De &#39;NV <b>Intercodam</b>&#39; is opgericht op 16 september 1919 en handelde in de meest <br> uiteenlopende producten. In 1972 werd deze NV omgezet in een Beheer B.V. <b>...</b>]
32
+ end
33
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'googlesearch'
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mlangenberg-googlesearch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rene Heino
8
+ - Matthijs Langenberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-01-07 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: nokogiri
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ description: Abstraction of the Google CSE XML API
26
+ email: rails@newminds.nl
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - googlesearch.gemspec
35
+ - lib/googlesearch.rb
36
+ - lib/googlesearch/search_response.rb
37
+ - lib/googlesearch/search_result.rb
38
+ has_rdoc: false
39
+ homepage:
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Google CSE implementation
64
+ test_files:
65
+ - spec/google_search_spec.rb
66
+ - spec/search_response_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/result_example.xml