google-site-search 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :test do
7
+ gem "minitest"
7
8
  gem "guard-minitest"
8
9
  gem "rb-inotify"
9
10
  end
@@ -47,18 +47,8 @@ module GoogleSiteSearch
47
47
 
48
48
  # Makes a request to the google search api and returns the xml response as a string.
49
49
  def request_xml url
50
- response = nil
51
- begin
52
- ::Timeout::timeout(2) do
53
- response = Net::HTTP.get_response(URI.parse(url.to_s))
54
- end
55
- rescue Errno::ECONNREFUSED => err
56
- rescue URI::InvalidURIError => err
57
- puts "URI Error [#{url}]"
58
- rescue => err
59
- puts "ERROR #{err.class}"
60
- end
61
- response.body if response && response.code == "200"
50
+ response = Net::HTTP.get_response(URI.parse(url.to_s))
51
+ response.body if response.is_a?(Net::HTTPSuccess)
62
52
  end
63
53
 
64
54
  # Google returns a result link as an absolute but you may
@@ -72,7 +62,7 @@ module GoogleSiteSearch
72
62
  def separate_search_term_from_filters(string)
73
63
  match = /\smore:p.*/.match(string)
74
64
  return [string, nil] if match.nil?
75
- return [match.pre_match, match[0]]
65
+ return [match.pre_match.strip, match[0].strip]
76
66
  end
77
67
 
78
68
  end
@@ -53,28 +53,21 @@ module GoogleSiteSearch
53
53
  @results || []
54
54
  end
55
55
 
56
-
57
56
  private
58
57
 
59
58
  def parse_xml
60
- begin
61
- doc = ::XML::Parser.string(@xml).parse
62
- doc.find("//GSP/RES/R").each do |result_node|
63
- @results << result_class.new(result_node)
64
- #puts "*** attribute = #{result_node.find_first("PageMap/DataObject[@type='metatags']/Attribute[@name='title']").attributes[:value] }"
65
- end
66
-
67
- spelling_node = doc.find_first("Spelling/Suggestion")
68
- @spelling = spelling_node.try(:content)
69
- @spelling_q = spelling_node.try(:attributes).try(:[],:q)
70
- @estimated_results_total = doc.find_first("RES/M").try(:content)
71
- @next_results_url = doc.find_first("RES/NB/NU").try(:content)
72
- @previous_results_url = doc.find_first("RES/NB/PU").try(:content)
73
- @search_query = doc.find_first("Q").try(:content)
74
-
75
- rescue => err
76
- puts "parse_xml error #{err.message}"
59
+ doc = ::XML::Parser.string(@xml).parse
60
+ doc.find("//GSP/RES/R").each do |result_node|
61
+ @results << result_class.new(result_node)
77
62
  end
63
+
64
+ spelling_node = doc.find_first("Spelling/Suggestion")
65
+ @spelling = spelling_node.try(:content)
66
+ @spelling_q = spelling_node.try(:attributes).try(:[],:q)
67
+ @estimated_results_total = doc.find_first("RES/M").try(:content)
68
+ @next_results_url = doc.find_first("RES/NB/NU").try(:content)
69
+ @previous_results_url = doc.find_first("RES/NB/PU").try(:content)
70
+ @search_query = doc.find_first("Q").try(:content)
78
71
  end
79
72
  end
80
73
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleSiteSearch
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,14 +2,18 @@ require_relative 'test_helper'
2
2
 
3
3
  describe GoogleSiteSearch do
4
4
 
5
- describe '#.relative_path' do
5
+ describe '.paginate' do
6
+ it 'completes a valid url for the relative path supplied' do
7
+ GoogleSiteSearch.paginate("/some/path").must_equal "http://www.google.com/some/path"
8
+ end
9
+ end
6
10
 
11
+ describe '.relative_path' do
7
12
  it 'raises InvalidURLError if a nil is given' do
8
13
  -> {GoogleSiteSearch.relative_path(nil)}.must_raise URI::InvalidURIError
9
14
  end
10
15
 
11
16
  describe 'given a relative path' do
12
-
13
17
  it 'returns the path given' do
14
18
  GoogleSiteSearch.relative_path("/somepath").must_equal "/somepath"
15
19
  end
@@ -17,21 +21,67 @@ describe GoogleSiteSearch do
17
21
  end
18
22
 
19
23
  describe 'given an absolute url' do
20
-
21
24
  it 'with just the domain a root path will be returned' do
22
25
  GoogleSiteSearch.relative_path("http://www.somesite.com/").must_equal "/"
23
26
  end
24
27
 
25
28
  it 'with a domain and path given the path will be returned' do
26
29
  GoogleSiteSearch.relative_path("http://www.somesite.com/my-test").must_equal "/my-test"
27
-
28
30
  end
29
31
 
30
32
  it 'with a query string the path and query will be returned' do
31
33
  GoogleSiteSearch.relative_path("http://www.somesite.com/my-test?something=value").must_equal "/my-test?something=value"
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+ describe '.separate_search_term_from_filters' do
40
+
41
+ it 'works on a valid search and filter' do
42
+ GoogleSiteSearch.separate_search_term_from_filters("microsoft more:pagemap:mytype").must_equal ["microsoft", "more:pagemap:mytype"]
43
+ end
44
+
45
+ it 'not fooled by an improper filter' do
46
+ GoogleSiteSearch.separate_search_term_from_filters("microsoft more:x:wrong").must_equal ["microsoft more:x:wrong", nil]
47
+ end
32
48
 
49
+ it 'strips whitespace' do
50
+ GoogleSiteSearch.separate_search_term_from_filters(" microsoft more:p:my-value ").must_equal ["microsoft", "more:p:my-value"]
51
+ end
52
+
53
+ it 'handles nil' do
54
+ GoogleSiteSearch.separate_search_term_from_filters(nil).must_equal [nil, nil]
55
+ end
56
+
57
+ it 'handles ""' do
58
+ GoogleSiteSearch.separate_search_term_from_filters("").must_equal ["", nil]
59
+ end
60
+ end
61
+
62
+ describe ".request_xml" do
63
+
64
+ it "passes back the body of a successfull HTTP request" do
65
+ mock = MiniTest::Mock.new.expect(:is_a?, true, [Net::HTTPSuccess])
66
+ .expect(:body, "my response")
67
+ Net::HTTP.stub(:get_response, mock) do
68
+ GoogleSiteSearch.request_xml("/doesnt_matter").must_equal "my response"
33
69
  end
70
+ end
34
71
 
72
+ it "returns nil if not a Net::HTTPSuccess" do
73
+ mock = MiniTest::Mock.new.expect( :is_a?, false, [Net::HTTPSuccess])
74
+ Net::HTTP.stub(:get_response, mock) do
75
+ GoogleSiteSearch.request_xml("/doesnt_matter").must_be_nil
76
+ end
77
+ end
78
+
79
+ it "doesn't catch exceptions if they happen" do
80
+ mock = -> a {raise StandardError}
81
+ Net::HTTP.stub(:get_response, mock) do
82
+ -> {GoogleSiteSearch.request_xml("/doesnt_matter")}.must_raise StandardError
83
+ end
35
84
  end
36
85
  end
86
+
37
87
  end
data/test/test_helper.rb CHANGED
@@ -2,4 +2,5 @@ ENV['RACK_ENV'] = 'test'
2
2
 
3
3
  require "google-site-search"
4
4
  require "minitest/spec"
5
+ require "minitest/mock"
5
6
  include GoogleSiteSearch #include module so I don't have to namespace everything
data/test/test_search.rb CHANGED
@@ -2,6 +2,71 @@ require_relative 'test_helper'
2
2
 
3
3
  describe Search do
4
4
 
5
+ let :xml do
6
+ <<-xml
7
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
8
+ <GSP VER="3.2">
9
+ <TM>0.075037</TM><Q>rails</Q>
10
+ <PARAM name="q" value="rails" original_value="rails" url_escaped_value="rails" js_escaped_value="rails"></PARAM><PARAM name="num" value="2" original_value="2" url_escaped_value="2" js_escaped_value="2"></PARAM><PARAM name="hl" value="en" original_value="en" url_escaped_value="en" js_escaped_value="en"></PARAM><PARAM name="client" value="google-csbe" original_value="google-csbe" url_escaped_value="google-csbe" js_escaped_value="google-csbe"></PARAM><PARAM name="cx" value="009797329687680929468:y_saholjpsk" original_value="009797329687680929468:y_saholjpsk" url_escaped_value="009797329687680929468%3Ay_saholjpsk" js_escaped_value="009797329687680929468:y_saholjpsk"></PARAM><PARAM name="boostcse" value="0" original_value="0" url_escaped_value="0" js_escaped_value="0"></PARAM><PARAM name="output" value="xml_no_dtd" original_value="xml_no_dtd" url_escaped_value="xml_no_dtd" js_escaped_value="xml_no_dtd"></PARAM><PARAM name="ie" value="UTF-8" original_value="UTF-8" url_escaped_value="UTF-8" js_escaped_value="UTF-8"></PARAM><PARAM name="oe" value="UTF-8" original_value="UTF-8" url_escaped_value="UTF-8" js_escaped_value="UTF-8"></PARAM><PARAM name="prmd" value="ivnslb" original_value="ivnslb" url_escaped_value="ivnslb" js_escaped_value="ivnslb"></PARAM><PARAM name="ei" value="PYSNUNWAKKea2AXH0YDYDA" original_value="PYSNUNWAKKea2AXH0YDYDA" url_escaped_value="PYSNUNWAKKea2AXH0YDYDA" js_escaped_value="PYSNUNWAKKea2AXH0YDYDA"></PARAM><PARAM name="start" value="2" original_value="2" url_escaped_value="2" js_escaped_value="2"></PARAM><PARAM name="sa" value="N" original_value="N" url_escaped_value="N" js_escaped_value="N"></PARAM><SEARCH_MODES><PROMOTED_MODES></PROMOTED_MODES><UNPROMOTED_MODES></UNPROMOTED_MODES><MORE_TEXT>More</MORE_TEXT><LESS_TEXT>Fewer</LESS_TEXT></SEARCH_MODES><Spelling><Suggestion q="fake suggestion escaped">fake suggestion not escaped</Suggestion></Spelling><Context><title>Info-Tech Research Group</title><Facet><FacetItem><label>test_refinement</label><anchor_text>Test Refinement</anchor_text></FacetItem></Facet></Context><RES SN="3" EN="4">
11
+ it "contains the spelling suggestions" do
5
12
 
13
+ end
14
+ <M>29</M>
15
+ <NB><PU>/previous</PU><NU>/next</NU></NB>
16
+ <RG START="1" SIZE="2"></RG> <RG START="1" SIZE="1"> </RG> <R N="3"><U>http://www.infotech.com/research/web-app-dev-trade-in-j2ee-for-ruby-on-rails</U><UE>http://www.infotech.com/research/web-app-dev-trade-in-j2ee-for-ruby-on-rails</UE><T>Web App Dev: Trade in J2EE for Ruby on &lt;b&gt;Rails&lt;/b&gt; | Info-Tech Research &lt;b&gt;...&lt;/b&gt;</T><RK>0</RK><BYLINEDATE>1144738800</BYLINEDATE><S>11 Apr 2006 &lt;b&gt;...&lt;/b&gt; Ruby is gaining a lot of attention as more Web developers report shorter learning &lt;br&gt; curves and development cycles using the language.</S><LANG>en</LANG><Label>_cse_y_saholjpsk</Label><PageMap><DataObject type="metatags"><Attribute name="viewport" value="width=device-width, initial-scale=1.0"/><Attribute name="csrf-param" value="authenticity_token"/><Attribute name="csrf-token" value="33G4AEWugnU0gp6lDmF/Ct527Yi16df7TR3qST8t52I="/><Attribute name="product" value="itap|"/><Attribute name="product_slug" value="web-app-dev-trade-in-j2ee-for-ruby-on-rails"/><Attribute name="publicationtype" value="note"/><Attribute name="lifecycle" value="MakeDecisions|Evaluate|"/><Attribute name="categories" value="|applications|application-web-development-maintenance|development|"/><Attribute name="publishdate" value="2006-04-11"/><Attribute name="authors" value="Curtis Gittens|"/><Attribute name="title" value="Web App Dev: Trade in J2EE for Ruby on Rails"/></DataObject></PageMap><HAS><L/><C SZ="40k" CID="6P9DT50RDYIJ"/><RT/></HAS></R>
17
+ <RG START="2" SIZE="1"> </RG> <R N="4"><U>http://hr.mcleanco.com/research/hr-craft-effective-performance-improvement-plans</U><UE>http://hr.mcleanco.com/research/hr-craft-effective-performance-improvement-plans</UE><T>Implement Performance Improvement Plans | McLean &amp;amp; Company</T><RK>0</RK><BYLINEDATE>1313996400</BYLINEDATE><S>22 Aug 2011 &lt;b&gt;...&lt;/b&gt; 1. Implement Performance Improvement Plans. Get employee performance on &lt;br&gt; track before it rides off the &lt;b&gt;rails&lt;/b&gt;. McLean &amp;amp; Company. / 12. 00:00 &lt;b&gt;...&lt;/b&gt;</S><LANG>en</LANG><Label>_cse_y_saholjpsk</Label><PageMap><DataObject type="publication"><Attribute name="type">solution-set</Attribute><Attribute name="type-title">Solution Set</Attribute><Attribute name="attachment-icons">http://static.infotech.com/images/css/icons/ppt_icon-14x14.png|http://static.infotech.com/images/css/icons/doc_icon-14x14.png</Attribute><Attribute name="product_type_bias">1</Attribute><Attribute name="product_families">hr</Attribute><Attribute name="slug">hr-craft-effective-performance-improvement-plans</Attribute><Attribute name="categories">talent-management|coaching-and-development</Attribute><Attribute name="publishdate">2011-05-26</Attribute><Attribute name="authors">Susanna Hunter</Attribute><Attribute name="title">Implement Performance Improvement Plans</Attribute><Attribute name="description">Managers dread implementing a Performance Improvement Plan (PIP) because they think it&#39;s a waste of time. However, in a recent McLean &amp; Company survey, 68% of employees who were placed on a PIP were still with the organization. When a manager...</Attribute><Attribute name="keywords">performance management|Development|difficult employee|performance review|discrimination|termination|Coaching|poor performance|employee performance|Performance Improvement Plan|PIP|performance improvement|Performance appraisal|corrective action|development plans|corrective discipline|progressive discipline|performance issue</Attribute></DataObject><DataObject type="metatags"><Attribute name="viewport" value="width=device-width, initial-scale=1.0"/><Attribute name="csrf-param" value="authenticity_token"/><Attribute name="csrf-token" value="9UbBckKIdrtN7Gn8ZFSuhOqvTcY/8JzUW34mXU4Cwiw="/><Attribute name="product" value="hr|"/><Attribute name="product_slug" value="hr-craft-effective-performance-improvement-plans"/><Attribute name="publicationtype" value="solution-set"/><Attribute name="categories" value="|talent-management|coaching-and-development|"/><Attribute name="publishdate" value="2011-05-26"/><Attribute name="authors" value="Susanna Hunter|"/><Attribute name="title" value="Implement Performance Improvement Plans"/></DataObject><DataObject type="cse_image"><Attribute name="src" value="http://static.infotech.com/images/MCO-Thumb-SB6.jpg"/></DataObject><DataObject type="cse_thumbnail"><Attribute name="width" value="280"/><Attribute name="height" value="160"/><Attribute name="src" value="http://t1.gstatic.com/images?q=tbn:ANd9GcSC2UdC2eeAKKC4nh_iWK8oiPjMI06QVKIKUgSwi3VbnlBEI4TmjiaG12IU"/></DataObject></PageMap><HAS><L/><C SZ="85k" CID="ffPBTuMTrTIJ"/><RT/></HAS></R>
18
+ <RHS_COLUMN><RG START="1" SIZE="0"></RG></RHS_COLUMN></RES>
19
+ </GSP>
20
+ xml
21
+
22
+ end
23
+
24
+ describe "#query" do
25
+
26
+ let :search do
27
+ GoogleSiteSearch.stub(:request_xml, xml) do
28
+ Search.new("/sample", Result).query
29
+ end
30
+ end
31
+
32
+ it "stores the original url given" do
33
+ search.url.must_equal "/sample"
34
+ end
35
+
36
+ it "has the two results from our xml example" do
37
+ search.results.count.must_equal 2
38
+ end
39
+
40
+ it "contains the estimated results total" do
41
+ search.estimated_results_total.must_equal "29"
42
+ end
43
+
44
+ it "contains the next results url" do
45
+ search.next_results_url.must_equal "/next"
46
+ end
47
+
48
+ it "contains the previous results url" do
49
+ search.previous_results_url.must_equal "/previous"
50
+ end
51
+
52
+ it "stores the original xml" do
53
+ search.xml.must_equal xml
54
+ end
55
+
56
+ it "defaults to the Result class" do
57
+ search.result_class.must_equal Result
58
+ end
59
+
60
+ it "stores the original search query" do
61
+ search.search_query.must_equal "rails"
62
+ end
63
+
64
+ it "contains the spelling suggestion" do
65
+ search.spelling.must_equal "fake suggestion not escaped"
66
+ end
6
67
 
68
+ it "contains the spelling suggestion attribute q" do
69
+ search.spelling_q.must_equal "fake suggestion escaped"
70
+ end
71
+ end
7
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-site-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-25 00:00:00.000000000 Z
12
+ date: 2012-10-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &7621180 !ruby/object:Gem::Requirement
16
+ requirement: &16445280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *7621180
24
+ version_requirements: *16445280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: libxml-ruby
27
- requirement: &7620760 !ruby/object:Gem::Requirement
27
+ requirement: &16444860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *7620760
35
+ version_requirements: *16444860
36
36
  description: A gem to aid in the consumption of the google site search service; querys
37
37
  the service, populates a result object and has some related helper methods.
38
38
  email: