trusty_google_custom_search 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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 ADDED
@@ -0,0 +1,51 @@
1
+ GoogleCustomSearch
2
+ ==================
3
+
4
+ Rewritten version of the GoogleCustomSearch plugin, replacing the now deprecated AJAX API with
5
+ the Google Site Search XML API.
6
+
7
+ How to Use
8
+ ==========
9
+
10
+ After adding the Google Custom Search plugin to your vendor/plugins directory, add the following to your environment.rb file:
11
+
12
+ GoogleCustomSearch::Search.google_search_api_key = "Your Google API Key"
13
+
14
+ From there, you can run a search like so:
15
+
16
+ @results = GoogleCustomSearch::Search.new.with_page_index(1).for url_encode("query")
17
+
18
+ Result items can be accessed through the items parameter (i.e. result.items). An example implementation for a view:
19
+
20
+ <% @results.items.each do |result_item| %>
21
+ <div class="result">
22
+ <div class="title">
23
+ <h4>
24
+ <%= link_to sanitize(result_item.title), result_item.url %>
25
+ </h4>
26
+ </div>
27
+ <div class="description">
28
+ <%= sanitize(result_item.content) %>
29
+ </div>
30
+ <div class="url">
31
+ <%= result_item.url %>
32
+ </div>
33
+ </div>
34
+ <% end %>
35
+
36
+ And, finally, a sample implementation to create next and previous page links:
37
+
38
+ <div class="pagination-links">
39
+ <% if @result.items.count > 0 %>
40
+ <% unless @result.first_page? %>
41
+ <%= render :partial => "page", :locals => {:page_number => @result.previous_page_number, :label => "&lt; Previous"} %>
42
+ <% end %>
43
+ <% unless @result.last_page? %>
44
+ <%= render :partial => "page", :locals => {:page_number => @result.next_page_number, :label => "Next &gt;"} %>
45
+ <% end %>
46
+ <% end %>
47
+ </div>
48
+
49
+
50
+ Modifications (c) 2011 The Pittsburgh Cultural Trust, released under the MIT license
51
+ Copyright (c) 2010 Anand Agarwal / Anay Kamat, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require "spec"
5
+ require "spec/rake/spectask"
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Test the google_custom_search plugin.'
11
+ Spec::Rake::SpecTask.new(:test) do |t|
12
+ plugin_root = File.dirname(__FILE__)
13
+ # t.rcov = true
14
+ # t.rcov_opts = lambda do
15
+ # IO.readlines("#{plugin_root}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
16
+ # end
17
+ t.spec_opts = ['--options', "\"#{plugin_root}/spec/spec.opts\""]
18
+ t.spec_files = FileList[].include(File.join(plugin_root, 'spec', '**/*_spec.rb'))
19
+ end
20
+
21
+ desc 'Generate documentation for the google_custom_search plugin.'
22
+ Rake::RDocTask.new(:rdoc) do |rdoc|
23
+ rdoc.rdoc_dir = 'rdoc'
24
+ rdoc.title = 'GoogleCustomSearch'
25
+ rdoc.options << '--line-numbers' << '--inline-source'
26
+ rdoc.rdoc_files.include('README')
27
+ rdoc.rdoc_files.include('lib/**/*.rb')
28
+ end
Binary file
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require File.join(File.dirname(__FILE__), "lib", "google_custom_search")
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,43 @@
1
+ require 'uri'
2
+ require 'open-uri'
3
+ require 'hpricot'
4
+
5
+ module GoogleCustomSearch
6
+
7
+ class Search
8
+
9
+ class << self
10
+ attr_accessor :google_search_api_key
11
+ end
12
+ def initialize
13
+ @page_index = 0
14
+ @query_parameters = {
15
+ :num => 10,
16
+ :start => 1
17
+ }
18
+ end
19
+
20
+ def for keyword
21
+ uri = build_query_url(keyword)
22
+ search_data = uri.read
23
+ xml_data = Hpricot(search_data)
24
+ GoogleCustomSearch::SearchResult.new xml_data
25
+ end
26
+
27
+ def with_page_index index
28
+ @page_index = index - 1
29
+ self
30
+ end
31
+
32
+ private
33
+ def build_query_url(keyword)
34
+ @query_parameters[:start] = @query_parameters[:num] * @page_index
35
+ google_search_url = "http://www.google.com/cse?client=google-csbe&output=xml_no_dtd"
36
+ arguments = @query_parameters.collect { |key, value| "#{key}=#{value}" }
37
+ arguments.sort!
38
+ URI.parse("#{google_search_url}&cx=#{self.class.google_search_api_key}&q=#{keyword}&"+arguments.join("&"))
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,45 @@
1
+ require 'hpricot'
2
+ module GoogleCustomSearch
3
+ class SearchResult
4
+ def initialize result
5
+ @result = result
6
+ end
7
+
8
+ def estimated_count
9
+ (@result/"res"/"m").innerHTML.to_i
10
+ end
11
+
12
+ def start_index
13
+ @result.at('res').attributes['sn'].to_i
14
+ end
15
+
16
+ def end_index
17
+ @result.at('res').attributes['en'].to_i
18
+ end
19
+
20
+ def items
21
+ (@result/"r").collect { |result| SearchResultItem.new result }
22
+ end
23
+
24
+ def range
25
+ start_index..end_index
26
+ end
27
+
28
+ def first_page?
29
+ start_index == 1
30
+ end
31
+
32
+ def previous_page_number
33
+ (start_index - 1) / 10 unless first_page?
34
+ end
35
+
36
+ def last_page?
37
+ end_index >= estimated_count
38
+ end
39
+
40
+ def next_page_number
41
+ (end_index / 10) + 1 unless last_page?
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ require 'hpricot'
2
+ require 'cgi'
3
+ module GoogleCustomSearch
4
+
5
+ class SearchResultItem
6
+
7
+ attr_reader :title, :content, :url
8
+
9
+ def initialize(search_item)
10
+ @title = CGI.unescapeHTML((search_item/"t").innerHTML)
11
+ @content = CGI.unescapeHTML((search_item/"s").innerHTML)
12
+ @url = (search_item/"u").innerHTML
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ Dir["#{File.dirname(__FILE__)}/google_custom_search/**/*.rb"].each { |file| require file }
@@ -0,0 +1,43 @@
1
+ require 'hpricot'
2
+ require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
3
+
4
+ describe GoogleCustomSearch::SearchResultItem do
5
+
6
+ MOCK_XML =<<-xml
7
+ <GSP VER="3.2">
8
+ <TM>0.129014</TM><Q>Wicked</Q>
9
+ <PARAM name="cx" value="abcd" original_value="abcd" url_escaped_value="abcd" js_escaped_value="abcd"/>
10
+ <PARAM name="client" value="google-csbe" original_value="google-csbe" url_escaped_value="google-csbe" js_escaped_value="google-csbe"/>
11
+ <PARAM name="output" value="xml_no_dtd" original_value="xml_no_dtd" url_escaped_value="xml_no_dtd" js_escaped_value="xml_no_dtd"/>
12
+ <PARAM name="q" value="Wicked" original_value="Wicked" url_escaped_value="Wicked" js_escaped_value="Wicked"/>
13
+ <Context><title>Cultural District Search Engine</title></Context><ARES/><RES SN="1" EN="1">
14
+ <M>33</M>
15
+ <FI/><NB><NU>/custom?q=Wicked&amp;hl=en&amp;safe=off&amp;client=google-csbe&amp;cx=004905679161489350096:rymp5afccji&amp;boostcse=0&amp;site=culturaldistrict.org&amp;output=xml_no_dtd&amp;ie=UTF-8&amp;oe=UTF-8&amp;ei=wFH_TdLGI8fKgQfGpMXwCg&amp;start=10&amp;sa=N</NU>
16
+ </NB><RG START="1" SIZE="1"/><RG START="1" SIZE="1"/><R N="1"><U>http://pgharts.culturaldistrict.org/production/26485</U><UE>http://pgharts.culturaldistrict.org/production/26485</UE><T>&lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets Entertainment Weekly calls &lt;b&gt;WICKED&lt;/b&gt; &amp;quot;the &lt;br&gt; best musical of the decade,” and when it first played Pittsburgh in 2006, &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>_cse_rymp5afccji</Label><HAS><L/><C SZ="25k" CID="zZcvqAH799sJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
17
+ </RES>
18
+ </GSP>
19
+ xml
20
+
21
+ def item_xml
22
+ results = Hpricot(MOCK_XML)
23
+ (results/"r").first
24
+ end
25
+
26
+
27
+ it "should return the title of search item" do
28
+ item = GoogleCustomSearch::SearchResultItem.new item_xml
29
+ item.title.should == "<b>Wicked</b> Pittsburgh Benedum Center Tickets"
30
+ end
31
+
32
+ it "should return the url of search item" do
33
+ item = GoogleCustomSearch::SearchResultItem.new item_xml
34
+ item.url.should == "http://pgharts.culturaldistrict.org/production/26485"
35
+ end
36
+
37
+ it "should return the content of search item" do
38
+ item = GoogleCustomSearch::SearchResultItem.new item_xml
39
+ item.content.should_not be_nil
40
+ end
41
+
42
+
43
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec'
2
+ require 'hpricot'
3
+ require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
4
+
5
+ describe GoogleCustomSearch::SearchResult do
6
+
7
+ RESULTS_XML = <<xml
8
+ <GSP VER="3.2">
9
+ <TM>0.129014</TM><Q>Wicked</Q>
10
+ <PARAM name="cx" value="abcd" original_value="abcd" url_escaped_value="abcd" js_escaped_value="abcd"/>
11
+ <PARAM name="client" value="google-csbe" original_value="google-csbe" url_escaped_value="google-csbe" js_escaped_value="google-csbe"/>
12
+ <PARAM name="output" value="xml_no_dtd" original_value="xml_no_dtd" url_escaped_value="xml_no_dtd" js_escaped_value="xml_no_dtd"/>
13
+ <PARAM name="q" value="Wicked" original_value="Wicked" url_escaped_value="Wicked" js_escaped_value="Wicked"/>
14
+ <PARAM name="site" value="culturaldistrict.org" original_value="culturaldistrict.org" url_escaped_value="culturaldistrict.org" js_escaped_value="culturaldistrict.org"/>
15
+ <PARAM name="adkw" value="AELymgX673ECcjZDQxtHuHEb5eHdobDdCLAvA552m-THsHQIu0ADF7SYAAoooi6mXsZjdqeBc-erFEdiD5Ir7Jml7ViklMSxqaFvPG05sJJoTebK88Rys-Q" original_value="AELymgX673ECcjZDQxtHuHEb5eHdobDdCLAvA552m-THsHQIu0ADF7SYAAoooi6mXsZjdqeBc-erFEdiD5Ir7Jml7ViklMSxqaFvPG05sJJoTebK88Rys-Q" url_escaped_value="AELymgX673ECcjZDQxtHuHEb5eHdobDdCLAvA552m-THsHQIu0ADF7SYAAoooi6mXsZjdqeBc-erFEdiD5Ir7Jml7ViklMSxqaFvPG05sJJoTebK88Rys-Q" js_escaped_value="AELymgX673ECcjZDQxtHuHEb5eHdobDdCLAvA552m-THsHQIu0ADF7SYAAoooi6mXsZjdqeBc-erFEdiD5Ir7Jml7ViklMSxqaFvPG05sJJoTebK88Rys-Q"/>
16
+ <PARAM name="hl" value="en" original_value="en" url_escaped_value="en" js_escaped_value="en"/>
17
+ <PARAM name="oe" value="UTF-8" original_value="UTF-8" url_escaped_value="UTF-8" js_escaped_value="UTF-8"/>
18
+ <PARAM name="ie" value="UTF-8" original_value="UTF-8" url_escaped_value="UTF-8" js_escaped_value="UTF-8"/>
19
+ <PARAM name="boostcse" value="0" original_value="0" url_escaped_value="0" js_escaped_value="0"/>
20
+ <Context><title>Cultural District Search Engine</title></Context><ARES/><RES SN="1" EN="10">
21
+ <M>33</M>
22
+ <FI/><NB><NU>/custom?q=Wicked&amp;hl=en&amp;safe=off&amp;client=google-csbe&amp;cx=004905679161489350096:rymp5afccji&amp;boostcse=0&amp;site=culturaldistrict.org&amp;output=xml_no_dtd&amp;ie=UTF-8&amp;oe=UTF-8&amp;ei=wFH_TdLGI8fKgQfGpMXwCg&amp;start=10&amp;sa=N</NU>
23
+ </NB>
24
+
25
+ <RG START="1" SIZE="10"/><RG START="1" SIZE="1"/><R N="1"><U>http://pgharts.culturaldistrict.org/production/26485</U><UE>http://pgharts.culturaldistrict.org/production/26485</UE><T>&lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets Entertainment Weekly calls &lt;b&gt;WICKED&lt;/b&gt; &amp;quot;the &lt;br&gt; best musical of the decade,” and when it first played Pittsburgh in 2006, &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>_cse_rymp5afccji</Label><HAS><L/><C SZ="25k" CID="zZcvqAH799sJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
26
+ <RG START="2" SIZE="1"/><R N="2"><U>http://pgharts.culturaldistrict.org/production/26485/wicked?cid=CT_05122011_FB_Wicked_WickedPresale</U><UE>http://pgharts.culturaldistrict.org/production/26485/wicked%3Fcid%3DCT_05122011_FB_Wicked_WickedPresale</UE><T>&lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets</T><RK>0</RK><BYLINEDATE>1305231821</BYLINEDATE><S>May 12, 2011 &lt;b&gt;...&lt;/b&gt; &lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets Entertainment Weekly calls &lt;b&gt;WICKED&lt;/b&gt; &amp;quot;the &lt;br&gt; best musical of the decade,” and when it first played &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>3</Label><Label>4</Label><HAS><L/><C SZ="26k" CID="unFTTJ48B_oJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
27
+ <RG START="3" SIZE="1"/><R N="3"><U>http://www.culturaldistrict.org/tickets/tickets/reserve.aspx?performanceNumber=24471</U><UE>http://www.culturaldistrict.org/tickets/tickets/reserve.aspx%3FperformanceNumber%3D24471</UE><T>Idina Menzel Pittsburgh Heinz Hall Tickets</T><RK>0</RK><S>Broadway powerhouse Idina Menzel – the Tony award-winning &amp;quot;Elphaba&amp;quot; from &lt;br&gt; international blockbuster &lt;b&gt;Wicked&lt;/b&gt; – performs for one-night-only at Heinz Hall &lt;br&gt; with &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>3</Label><Label>4</Label><HAS><L/><C SZ="25k" CID="LixRI1ZIT5oJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
28
+ <RG START="4" SIZE="1"/><R N="4"><U>http://pgharts.culturaldistrict.org/calendar/daily/2011/10/1</U><UE>http://pgharts.culturaldistrict.org/calendar/daily/2011/10/1</UE><T>The Pittsburgh Cultural Trust: Event Calendar: Saturday October 01 &lt;b&gt;...&lt;/b&gt;</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt;. 2:00 PM. Presented By: PNC Broadway Across America - Pittsburgh | Venue: &lt;br&gt; Benedum Center. The untold story of the witches of Oz &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>3</Label><Label>4</Label><HAS><L/><C SZ="31k" CID="-OI1Yk1Uu00J"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
29
+ <RG START="5" SIZE="1"/><R N="5"><U>http://pgharts.culturaldistrict.org/pct_home/subscriptions/pnc-broadway-subscriber-benefits/</U><UE>http://pgharts.culturaldistrict.org/pct_home/subscriptions/pnc-broadway-subscriber-benefits/</UE><T>PNC Broadway Subscriber Benefits</T><RK>0</RK><S>You&amp;#39;ll also be able to swap out of a subscription series show into a special as &lt;br&gt; they are announced (excludes &lt;b&gt;Wicked&lt;/b&gt;.) You can swap your tickets out of only &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>_cse_rymp5afccji</Label><HAS><L/><C SZ="17k" CID="wlaQgeznJUsJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
30
+ <RG START="6" SIZE="1"/><R N="6" MIME="application/pdf"><U>http://pgharts.culturaldistrict.org/uploads/File/PCT%20Group%20Sales/Broadway/bwy_WICKED_groupsales.pdf</U><UE>http://pgharts.culturaldistrict.org/uploads/File/PCT%2520Group%2520Sales/Broadway/bwy_WICKED_groupsales.pdf</UE><T>September 7-October 2, 2011 • Benedum Center</T><RK>0</RK><S>Visit &lt;b&gt;wicked&lt;/b&gt;.pgharts.org for information. $4 per order handling fee is added to &lt;br&gt; final purchase. prices, dates and times are subject to change without notice &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>_cse_rymp5afccji</Label><PageMap><DataObject type="metatags"><Attribute name="creationdate" value="D:20110506145045-04'00'"/><Attribute name="creator" value="Adobe InDesign CS3 (5.0.4)"/><Attribute name="producer" value="Adobe PDF Library 8.0"/><Attribute name="moddate" value="D:20110506145046-04'00'"/></DataObject></PageMap><HAS><L/><C SZ="" CID="D57QXYGUDqAJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET BLOB_REF="ADGEESicCNeh5IbblmeWGTjD7GhHhT-x68j4uxcR1ABteSioixexJKoYjJy8N-QGkEid67mu1eQMILVLRJlcms6fB7PrpRy_Jbq_4DXkV0G-1mEXtPrlG5c5L6_iX055ApzMWVGgP6nC"/></R>
31
+ <RG START="7" SIZE="1"/><R N="7"><U>http://www.culturaldistrict.org/calendar/daily/2011/9/11</U><UE>http://www.culturaldistrict.org/calendar/daily/2011/9/11</UE><T>CulturalDistrict.org: Event Calendar: Sunday September 11, 2011</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt;. 1:00 PM. Presented By: PNC Broadway Across America - Pittsburgh | Venue: &lt;br&gt; Benedum Center. The untold story of the witches of Oz &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>3</Label><Label>4</Label><HAS><L/><C SZ="31k" CID="kLpVCLvWg9MJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
32
+ <RG START="8" SIZE="1"/><R N="8"><U>http://www.culturaldistrict.org/calendar/daily/2011/9/8</U><UE>http://www.culturaldistrict.org/calendar/daily/2011/9/8</UE><T>CulturalDistrict.org: Event Calendar: Thursday September 08, 2011</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt;. 2:00 PM. Presented By: PNC Broadway Across America - Pittsburgh | Venue: &lt;br&gt; Benedum Center. The untold story of the witches of Oz &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>3</Label><Label>4</Label><HAS><L/><C SZ="31k" CID="401rjaqny8oJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
33
+ <RG START="9" SIZE="1"/><R N="9"><U>http://www.culturaldistrict.org/calendar/daily/2011/9/24</U><UE>http://www.culturaldistrict.org/calendar/daily/2011/9/24</UE><T>CulturalDistrict.org: Event Calendar: Saturday September 24, 2011</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt;. 2:00 PM. Presented By: PNC Broadway Across America - Pittsburgh | Venue: &lt;br&gt; Benedum Center. The untold story of the witches of Oz &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>3</Label><Label>4</Label><HAS><L/><C SZ="34k" CID="vvjjOCKmgKIJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
34
+ <RG START="10" SIZE="1"/><R N="10"><U>http://www.culturaldistrict.org/calendar/daily/2011/9/30</U><UE>http://www.culturaldistrict.org/calendar/daily/2011/9/30</UE><T>CulturalDistrict.org: Event Calendar: Friday September 30, 2011</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt;. 8:00 PM. Presented By: PNC Broadway Across America - Pittsburgh | Venue: &lt;br&gt; Benedum Center. The untold story of the witches of Oz &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>3</Label><Label>4</Label><HAS><L/><C SZ="31k" CID="BqNFJisYpOkJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
35
+ </RES>
36
+ </GSP>
37
+ xml
38
+
39
+ RESULTS_XML_NO_RESULTS = <<xml
40
+ <GSP VER="3.2">
41
+ <TM>0.166453</TM>
42
+ <Q>ffffggggg</Q>
43
+ <PARAM name="cx" value="abcd" original_value="abcd" url_escaped_value="abcd" js_escaped_value="abcd"/>
44
+ <PARAM name="client" value="google-csbe" original_value="google-csbe" url_escaped_value="google-csbe" js_escaped_value="google-csbe"/>
45
+ <PARAM name="output" value="xml_no_dtd" original_value="xml_no_dtd" url_escaped_value="xml_no_dtd" js_escaped_value="xml_no_dtd"/>
46
+ <PARAM name="q" value="ffffggggg" original_value="ffffggggg" url_escaped_value="ffffggggg" js_escaped_value="ffffggggg"/>
47
+ <PARAM name="site" value="culturaldistrict.org" original_value="culturaldistrict.org" url_escaped_value="culturaldistrict.org" js_escaped_value="culturaldistrict.org"/>
48
+ <PARAM name="adkw" value="AELymgXMiR0AyItzzgcJ88snCQ2ZQzi95BHYCpbuWqtNT-FkkxqTr3DTojcP0wKL9BMQD_VtA2nqsPZBKc_VkpC2x_UStZxn28VD4JyE6g1V53hVHpaf2js" original_value="AELymgXMiR0AyItzzgcJ88snCQ2ZQzi95BHYCpbuWqtNT-FkkxqTr3DTojcP0wKL9BMQD_VtA2nqsPZBKc_VkpC2x_UStZxn28VD4JyE6g1V53hVHpaf2js" url_escaped_value="AELymgXMiR0AyItzzgcJ88snCQ2ZQzi95BHYCpbuWqtNT-FkkxqTr3DTojcP0wKL9BMQD_VtA2nqsPZBKc_VkpC2x_UStZxn28VD4JyE6g1V53hVHpaf2js" js_escaped_value="AELymgXMiR0AyItzzgcJ88snCQ2ZQzi95BHYCpbuWqtNT-FkkxqTr3DTojcP0wKL9BMQD_VtA2nqsPZBKc_VkpC2x_UStZxn28VD4JyE6g1V53hVHpaf2js"/>
49
+ <PARAM name="hl" value="en" original_value="en" url_escaped_value="en" js_escaped_value="en"/>
50
+ <PARAM name="oe" value="UTF-8" original_value="UTF-8" url_escaped_value="UTF-8" js_escaped_value="UTF-8"/>
51
+ <PARAM name="ie" value="UTF-8" original_value="UTF-8" url_escaped_value="UTF-8" js_escaped_value="UTF-8"/>
52
+ <PARAM name="boostcse" value="0" original_value="0" url_escaped_value="0" js_escaped_value="0"/>
53
+ <Spelling>
54
+ <Suggestion q="ffggggg"><b><i>ffggggg</i></b></Suggestion>
55
+ </Spelling>
56
+ <ARES/>
57
+ </GSP>
58
+ xml
59
+
60
+ it "should find the estimated count from the given search result json data" do
61
+ result = GoogleCustomSearch::SearchResult.new Hpricot(RESULTS_XML)
62
+ result.estimated_count.should == 33
63
+ end
64
+
65
+
66
+ it "should return the range of results" do
67
+ result = GoogleCustomSearch::SearchResult.new Hpricot(RESULTS_XML)
68
+ result.range.should == (1..10)
69
+ end
70
+
71
+ describe "items" do
72
+ it "should return the search result items" do
73
+ result = GoogleCustomSearch::SearchResult.new Hpricot(RESULTS_XML)
74
+ result.items.size.should == 10
75
+ end
76
+
77
+ it "should return the title of search item" do
78
+ result = GoogleCustomSearch::SearchResult.new Hpricot(RESULTS_XML)
79
+ result.items.first.title.should == "<b>Wicked</b> Pittsburgh Benedum Center Tickets"
80
+ end
81
+ end
82
+
83
+
84
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec'
2
+ require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
3
+
4
+ describe GoogleCustomSearch::Search do
5
+
6
+ MOCK_JSON = {"responseData"=>{"results"=>[{"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page", "url"=>"http://www.localhost.site/tickets/tickets/production.aspx%3FperformanceNumber%3D17303", "cacheUrl"=>"http://www.google.com/search?q=cache:pGjUcr1qUkgJ:www.localhost.site", "content"=>"Search matching content", "visibleUrl"=>"www.localhost.site", "unescapedUrl"=>"http://www.localhost.site/tickets/tickets/production.aspx?performanceNumber=17303", "titleNoFormatting"=>"Tile of the page"}, {"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page", "url"=>"http://www.localhost.site/tickets/tickets/reserve.aspx%3FperformanceNumber%3D21140", "cacheUrl"=>"http://www.google.com/search?q=cache:E5_VPQiQo3kJ:www.localhost.site", "content"=>"1234", "visibleUrl"=>"www.localhost.site", "unescapedUrl"=>"http://www.localhost.site/tickets/tickets/reserve.aspx?performanceNumber=21140", "titleNoFormatting"=>"Tile of the page"}, {"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page", "url"=>"http://www.localhost.site/tickets/tickets/production.aspx%3FperformanceNumber%3D23379", "cacheUrl"=>"http://www.google.com/search?q=cache:V7cn6Foe2rMJ:www.localhost.site", "content"=>"abcd", "visibleUrl"=>"www.localhost.site", "unescapedUrl"=>"http://www.localhost.site/tickets/tickets/production.aspx?performanceNumber=23379", "titleNoFormatting"=>"Tile of the page"}, {"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page", "url"=>"http://www.localhost.site/tickets/tickets/production.aspx%3FperformanceNumber%3D17350", "cacheUrl"=>"http://www.google.com/search?q=cache:fJurfpC17DYJ:www.localhost.site", "content"=>"uiuio", "visibleUrl"=>"www.localhost.site", "unescapedUrl"=>"http://www.localhost.site/tickets/tickets/production.aspx?performanceNumber=17350", "titleNoFormatting"=>"Tile of the page"}, {"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page", "url"=>"http://www.localhost.site/tickets/tickets/production.aspx%3FperformanceNumber%3D23374", "cacheUrl"=>"http://www.google.com/search?q=cache:jExxaBgeDD0J:www.localhost.site", "content"=>"this looks great", "visibleUrl"=>"www.localhost.site", "unescapedUrl"=>"http://www.localhost.site/tickets/tickets/production.aspx?performanceNumber=23374", "titleNoFormatting"=>"Tile of the page"}, {"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page", "url"=>"http://www.localhost.site/tickets/tickets/production.aspx%3FperformanceNumber%3D17353", "cacheUrl"=>"http://www.google.com/search?q=cache:h43Ggs_qthYJ:www.localhost.site", "content"=>"super duper content", "visibleUrl"=>"www.localhost.site", "unescapedUrl"=>"http://www.localhost.site/tickets/tickets/production.aspx?performanceNumber=17353", "titleNoFormatting"=>"Tile of the page"}, {"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page", "url"=>"http://www.localhost.site/tickets/calendar/", "cacheUrl"=>"http://www.google.com/search?q=cache:cI47PY7ITc4J:www.localhost.site", "content"=>"this should be useful", "visibleUrl"=>"www.localhost.site", "unescapedUrl"=>"http://www.localhost.site/tickets/calendar/", "titleNoFormatting"=>"Tile of the page"}, {"GsearchResultClass"=>"GwebSearch", "title"=>"Tile of the page - Your Show Place!", "url"=>"http://localhost.site/%3Fop%3Dcontact", "cacheUrl"=>"http://www.google.com/search?q=cache:UUq_Mw_-UnIJ:localhost.site", "content"=>"cards game", "visibleUrl"=>"localhost.site", "unescapedUrl"=>"http://localhost.site/?op=contact", "titleNoFormatting"=>"Tile of the page - Your Show Place!"}], "cursor"=>{"estimatedResultCount"=>"409", "currentPageIndex"=>3, "moreResultsUrl"=>"http://www.google.com/cse?oe=utf8&ie=utf8&source=uds&cx=004905679161489350096%3Arymp5afccji&start=28&hl=en&q=org", "pages"=>[{"label"=>1, "start"=>"0"}, {"label"=>2, "start"=>"8"}, {"label"=>3, "start"=>"16"}, {"label"=>4, "start"=>"24"}, {"label"=>5, "start"=>"32"}, {"label"=>6, "start"=>"40"}, {"label"=>7, "start"=>"48"}, {"label"=>8, "start"=>"56"}]}, "context"=>{"title"=>"Cultural District Search Engine", "facets"=>[]}}, "responseDetails"=>nil, "responseStatus"=>200}
7
+
8
+ MOCK_XML =<<-xml
9
+ <GSP VER="3.2">
10
+ <TM>0.129014</TM><Q>Wicked</Q>
11
+ <PARAM name="cx" value="abcd" original_value="abcd" url_escaped_value="abcd" js_escaped_value="abcd"/>
12
+ <PARAM name="client" value="google-csbe" original_value="google-csbe" url_escaped_value="google-csbe" js_escaped_value="google-csbe"/>
13
+ <PARAM name="output" value="xml_no_dtd" original_value="xml_no_dtd" url_escaped_value="xml_no_dtd" js_escaped_value="xml_no_dtd"/>
14
+ <PARAM name="q" value="Wicked" original_value="Wicked" url_escaped_value="Wicked" js_escaped_value="Wicked"/>
15
+ <Context><title>Cultural District Search Engine</title></Context><ARES/><RES SN="1" EN="1">
16
+ <M>33</M>
17
+ <FI/><NB><NU>/custom?q=Wicked&amp;hl=en&amp;safe=off&amp;client=google-csbe&amp;cx=004905679161489350096:rymp5afccji&amp;boostcse=0&amp;site=culturaldistrict.org&amp;output=xml_no_dtd&amp;ie=UTF-8&amp;oe=UTF-8&amp;ei=wFH_TdLGI8fKgQfGpMXwCg&amp;start=10&amp;sa=N</NU>
18
+ </NB><RG START="1" SIZE="1"/><RG START="1" SIZE="1"/><R N="1"><U>http://pgharts.culturaldistrict.org/production/26485</U><UE>http://pgharts.culturaldistrict.org/production/26485</UE><T>&lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets</T><RK>0</RK><S>&lt;b&gt;Wicked&lt;/b&gt; Pittsburgh Benedum Center Tickets Entertainment Weekly calls &lt;b&gt;WICKED&lt;/b&gt; &amp;quot;the &lt;br&gt; best musical of the decade,” and when it first played Pittsburgh in 2006, &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>_cse_rymp5afccji</Label><HAS><L/><C SZ="25k" CID="zZcvqAH799sJ"/><RT/></HAS><ELIGIBLE_FOR_VISUAL_SNIPPET/></R>
19
+ </RES>
20
+ </GSP>
21
+ xml
22
+
23
+ ERROR_RESPONSE = {"responseData" => nil, "responseDetails" => "out of range start", "responseStatus" => 400}
24
+
25
+ GoogleCustomSearch::Search.google_search_api_key = "abcd"
26
+
27
+ describe "for" do
28
+
29
+ it "should return the estimated results count for the given search" do
30
+ mock_uri "http://www.google.com/cse?client=google-csbe&output=xml_no_dtd&cx=abcd&q=org&num=10&start=0", MOCK_XML
31
+ search = GoogleCustomSearch::Search.new
32
+ results = search.for("org")
33
+ results.estimated_count.should == 33
34
+ end
35
+
36
+ end
37
+
38
+ it "should return correct start and end index for results received" do
39
+ result = MOCK_XML
40
+ mock_uri "http://www.google.com/cse?client=google-csbe&output=xml_no_dtd&cx=abcd&q=org&num=10&start=10", result
41
+ search = GoogleCustomSearch::Search.new
42
+ results = search.with_page_index(2).for("org")
43
+ results.start_index.should == 1
44
+ results.end_index.should == 1
45
+ end
46
+
47
+ private
48
+ def mock_uri url, result
49
+ mock_uri = mock(URI)
50
+ mock_uri.should_receive(:read).and_return(result)
51
+ URI.should_receive(:parse).with(url).and_return(mock_uri)
52
+ end
53
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1,5 @@
1
+ --exclude "spec,tasks"
2
+ --rails
3
+ --include-file "lib/google_custom_search"
4
+ --text-report
5
+ --sort coverage
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.expand_path( File.dirname(__FILE__)+'/../lib/google_custom_search')
4
+ include Spec::Matchers
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :google_custom_search do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "google_custom_search"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "trusty_google_custom_search"
7
+ s.version = '1.0.0'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Eric Sipple"]
10
+ s.email = ["sipple@trustarts.org"]
11
+ s.homepage = "https://github.com/pgharts/google_custom_search/"
12
+ s.summary = %q{Interface for google custom search}
13
+ s.description = %q{Provides access to google custom search that can be integrated with a website.}
14
+
15
+
16
+ s.add_dependency "hpricot", '~> 0.8.6'
17
+
18
+ ignores = if File.exist?('.gitignore')
19
+ File.read('.gitignore').split("\n").inject([]) {|a,p| a + Dir[p] }
20
+ else
21
+ []
22
+ end
23
+ s.files = Dir['**/*'] - ignores
24
+ s.test_files = Dir['test/**/*','spec/**/*','features/**/*'] - ignores
25
+ # s.executables = Dir['bin/*'] - ignores
26
+ s.require_paths = ["lib"]
27
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trusty_google_custom_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Sipple
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hpricot
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.6
30
+ description: Provides access to google custom search that can be integrated with a
31
+ website.
32
+ email:
33
+ - sipple@trustarts.org
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - google_custom_search-1.0.0.gem
39
+ - init.rb
40
+ - install.rb
41
+ - lib/google_custom_search/search.rb
42
+ - lib/google_custom_search/search_result.rb
43
+ - lib/google_custom_search/search_result_item.rb
44
+ - lib/google_custom_search.rb
45
+ - MIT-LICENSE
46
+ - Rakefile
47
+ - README
48
+ - spec/google_custom_search/search_result_item_spec.rb
49
+ - spec/google_custom_search/search_result_spec.rb
50
+ - spec/google_custom_search/search_spec.rb
51
+ - spec/rcov.opts
52
+ - spec/spec.opts
53
+ - spec/spec_helper.rb
54
+ - tasks/google_custom_search_tasks.rake
55
+ - trusty-google-custom-search.gemspec
56
+ - uninstall.rb
57
+ homepage: https://github.com/pgharts/google_custom_search/
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.29
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Interface for google custom search
81
+ test_files:
82
+ - spec/google_custom_search/search_result_item_spec.rb
83
+ - spec/google_custom_search/search_result_spec.rb
84
+ - spec/google_custom_search/search_spec.rb
85
+ - spec/rcov.opts
86
+ - spec/spec.opts
87
+ - spec/spec_helper.rb