google-search 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/History.rdoc +8 -0
  2. data/Manifest +70 -0
  3. data/README.rdoc +95 -0
  4. data/Rakefile +16 -0
  5. data/examples/image.rb +21 -0
  6. data/examples/images.html +1 -0
  7. data/examples/web.rb +28 -0
  8. data/google-search.gemspec +33 -0
  9. data/lib/google-search.rb +29 -0
  10. data/lib/google-search/item.rb +10 -0
  11. data/lib/google-search/item/base.rb +78 -0
  12. data/lib/google-search/item/blog.rb +34 -0
  13. data/lib/google-search/item/book.rb +40 -0
  14. data/lib/google-search/item/image.rb +40 -0
  15. data/lib/google-search/item/local.rb +107 -0
  16. data/lib/google-search/item/news.rb +46 -0
  17. data/lib/google-search/item/patent.rb +40 -0
  18. data/lib/google-search/item/video.rb +46 -0
  19. data/lib/google-search/item/web.rb +22 -0
  20. data/lib/google-search/response.rb +92 -0
  21. data/lib/google-search/search.rb +11 -0
  22. data/lib/google-search/search/base.rb +212 -0
  23. data/lib/google-search/search/blog.rb +14 -0
  24. data/lib/google-search/search/book.rb +8 -0
  25. data/lib/google-search/search/image.rb +93 -0
  26. data/lib/google-search/search/local.rb +8 -0
  27. data/lib/google-search/search/mixins.rb +4 -0
  28. data/lib/google-search/search/mixins/filter.rb +26 -0
  29. data/lib/google-search/search/mixins/order_by.rb +36 -0
  30. data/lib/google-search/search/mixins/safety_level.rb +40 -0
  31. data/lib/google-search/search/news.rb +66 -0
  32. data/lib/google-search/search/patent.rb +37 -0
  33. data/lib/google-search/search/video.rb +15 -0
  34. data/lib/google-search/search/web.rb +15 -0
  35. data/lib/google-search/version.rb +6 -0
  36. data/spec/fixtures/400-response.json +1 -0
  37. data/spec/fixtures/blog-response.json +1 -0
  38. data/spec/fixtures/book-response.json +1 -0
  39. data/spec/fixtures/image-response.json +1 -0
  40. data/spec/fixtures/invalid-response.json +1 -0
  41. data/spec/fixtures/local-response.json +1 -0
  42. data/spec/fixtures/news-response.json +1 -0
  43. data/spec/fixtures/patent-response.json +1 -0
  44. data/spec/fixtures/video-response.json +1 -0
  45. data/spec/fixtures/web-2-response.json +1 -0
  46. data/spec/fixtures/web-response.json +1 -0
  47. data/spec/item_blog_spec.rb +26 -0
  48. data/spec/item_book_spec.rb +18 -0
  49. data/spec/item_image_spec.rb +21 -0
  50. data/spec/item_local_spec.rb +30 -0
  51. data/spec/item_news_spec.rb +19 -0
  52. data/spec/item_patent_spec.rb +19 -0
  53. data/spec/item_spec.rb +10 -0
  54. data/spec/item_video_spec.rb +20 -0
  55. data/spec/item_web_spec.rb +18 -0
  56. data/spec/response_spec.rb +63 -0
  57. data/spec/search_blog_spec.rb +0 -0
  58. data/spec/search_book_spec.rb +0 -0
  59. data/spec/search_image_spec.rb +71 -0
  60. data/spec/search_local_spec.rb +0 -0
  61. data/spec/search_news_spec.rb +33 -0
  62. data/spec/search_patent_spec.rb +27 -0
  63. data/spec/search_spec.rb +89 -0
  64. data/spec/search_video_spec.rb +42 -0
  65. data/spec/search_web_spec.rb +42 -0
  66. data/spec/spec.opts +2 -0
  67. data/spec/spec_helper.rb +13 -0
  68. data/tasks/docs.rake +18 -0
  69. data/tasks/gemspec.rake +3 -0
  70. data/tasks/spec.rake +25 -0
  71. metadata +168 -0
@@ -0,0 +1,18 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item::Book do
5
+ describe "#initialize" do
6
+ it "should populate attributes" do
7
+ hash = json_fixture('book-response')['responseData']['results'].first
8
+ item = Google::Search::Item::Book.new hash
9
+ item.title.should include('Foo Fighters')
10
+ item.author.should == '<b>Foo</b> Fighters (CRT), Hal Leonard Publishing Corporation'
11
+ item.id.should == 'ISBN1423404580'
12
+ item.uri.should == 'http://books.google.com/books?id=vUoCAgAACAAJ&dq=foo&client=internal-uds&source=uds'
13
+ item.thumbnail_uri.should == 'http://bks6.books.google.com/books?id=vUoCAgAACAAJ&printsec=frontcover&img=1&zoom=5&sig=ACfU3U1NHHhXuERH30Xfn0GC3A0BW5nMPg'
14
+ item.published_year.should == 2006
15
+ item.pages.should == 69
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item::Image do
5
+ describe "#initialize" do
6
+ it "should populate attributes" do
7
+ hash = json_fixture('image-response')['responseData']['results'].first
8
+ item = Google::Search::Item::Image.new hash
9
+ item.id.should == 'IYlLzX-w4vX2AM'
10
+ item.title.should == 'foo_fighters.jpg'
11
+ item.uri.should == 'http://tomdiaz.files.wordpress.com/2009/06/foo_fighters.jpg'
12
+ item.content.should include('Not FBI Agents')
13
+ item.context_uri.should == 'http://tomdiaz.wordpress.com/2009/06/'
14
+ item.width.should == 883
15
+ item.height.should == 891
16
+ item.thumbnail_uri.should == 'http://images.google.com/images?q=tbn:IYlLzX-w4vX2AM:tomdiaz.files.wordpress.com/2009/06/foo_fighters.jpg'
17
+ item.thumbnail_width.should == 145
18
+ item.thumbnail_height.should == 146
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item::Local do
5
+ describe "#initialize" do
6
+ it "should populate attributes" do
7
+ hash = json_fixture('local-response')['responseData']['results'].first
8
+ item = Google::Search::Item::Local.new hash
9
+ item.city.should == 'San Francisco'
10
+ item.type.should == 'local'
11
+ item.region.should == 'CA'
12
+ item.uri.should == 'http://www.google.com/local?source=uds&q=foo&sll=37.795279%2C-122.407451&latlng=37795279%2C-122407451%2C1909516875098392575&near=37.795279%2C-122.407451'
13
+ item.directions_from_here_uri.should == 'http://www.google.com/maps?source=uds&saddr=852+Washington+St%2C+San+Francisco%2C+CA+%28Foo+Wah+Cheung%29+%4037.795279%2C-122.407451&iwstate1=dir%3Afrom'
14
+ item.directions_to_here_uri.should == 'http://www.google.com/maps?source=uds&daddr=852+Washington+St%2C+San+Francisco%2C+CA+%28Foo+Wah+Cheung%29+%4037.795279%2C-122.407451&iwstate1=dir%3Ato'
15
+ item.directions_uri.should == 'http://www.google.com/maps?source=uds&daddr=852+Washington+St%2C+San+Francisco%2C+CA+%28Foo+Wah+Cheung%29+%4037.795279%2C-122.407451&saddr'
16
+ item.title.should == 'Foo Wah Cheung'
17
+ item.accuracy.should == 8
18
+ item.country.should == 'United States'
19
+ item.max_age.should == 604800
20
+ item.thumbnail_uri.should == 'http://mt.google.com/mapdata?cc=us&tstyp=5&Point=b&Point.latitude_e6=37795279&Point.longitude_e6=-122407451&Point.iconid=15&Point=e&w=150&h=100&zl=4'
21
+ item.long.should == -122.407451
22
+ item.street_address.should == '852 Washington St'
23
+ item.content.should == ''
24
+ item.lat.should == 37.795279
25
+ item.viewport_mode.should == 'computed'
26
+ item.phone_numbers.should == ['(415) 391-4067']
27
+ item.address_lines.should == ['852 Washington St', 'San Francisco, CA']
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item::News do
5
+ describe "#initialize" do
6
+ it "should populate attributes" do
7
+ hash = json_fixture('news-response')['responseData']['results'].first
8
+ item = Google::Search::Item::News.new hash
9
+ item.title.should include('TICKET TO DINING')
10
+ item.language.should == 'en'
11
+ item.uri.should == 'http://www.montgomerynews.com/articles/2009/07/22/entertainment/doc4a672746b0941650009917.txt'
12
+ item.published.should be_a(DateTime)
13
+ item.publisher.should == 'Montgomery Newspapers'
14
+ item.content.should include('After all, it was')
15
+ item.redirect_uri.should == 'http://news.google.com/news/url?sa=T&ct=us/0-0-0&fd=S&url=http://www.montgomerynews.com/articles/2009/07/22/entertainment/doc4a672746b0941650009917.txt&cid=0&ei=4aFnSpnxK474rQPutNHRAQ&usg=AFQjCNGsuTniL5DY24lNJ8qy204ZWjQoKA'
16
+ item.location.should == 'Fort Washington,PA,USA'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item::Patent do
5
+ describe "#initialize" do
6
+ it "should populate attributes" do
7
+ hash = json_fixture('patent-response')['responseData']['results'].first
8
+ item = Google::Search::Item::Patent.new hash
9
+ item.title.should == 'SZE-FOO CHIEN'
10
+ item.id.should == 3468158
11
+ item.content.should include('METHOD OF AND APPARATUS FOR DETERMINING RH')
12
+ item.assignee.should == ''
13
+ item.application_date.should be_a(DateTime)
14
+ item.uri.should == 'http://www.google.com/patents/about?id=GDMdAAAAEBAJ&dq=foo&client=internal-uds&source=uds'
15
+ item.thumbnail_uri.should == 'http://bks9.books.google.com/patents?id=GDMdAAAAEBAJ&printsec=drawing&img=1&zoom=1&sig=ACfU3U10b3w-4hMfKTEykPmtqnoObaLhaA'
16
+ item.status.should == 'issued'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item do
5
+ describe ".class_for" do
6
+ it "should return a constant" do
7
+ Google::Search::Item.class_for('GwebSearch').should == Google::Search::Item::Web
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item::Video do
5
+ describe "#initialize" do
6
+ it "should populate attributes" do
7
+ hash = json_fixture('video-response')['responseData']['results'].first
8
+ item = Google::Search::Item::Video.new hash
9
+ item.title.should include('Foo Fighters')
10
+ item.content.should include('Foo Fighters')
11
+ item.uri.should == 'http://www.google.com/url?q=http://www.youtube.com/watch%3Fv%3DTVboOdX9icA&source=video&vgc=rss&usg=AFQjCNFvZftyyTO-IswoCPWEmmQbskBMRA'
12
+ item.rating.should == 4.9076071
13
+ item.type.should == 'YouTube'
14
+ item.published.should be_a(DateTime)
15
+ item.thumbnail_uri.should == 'http://0.gvt0.com/vi/TVboOdX9icA/default.jpg'
16
+ item.publisher.should == 'www.youtube.com'
17
+ item.duration.should == 269
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Item::Web do
5
+ describe "#initialize" do
6
+ it "should populate attributes" do
7
+ hash = json_fixture('web-response')['responseData']['results'].first
8
+ hash['index'] = 0
9
+ item = Google::Search::Item::Web.new hash
10
+ item.index.should == 0
11
+ item.title.should include('foobar - Wikipedia')
12
+ item.content.should include('Foobar is often used')
13
+ item.uri.should == 'http://en.wikipedia.org/wiki/Foobar'
14
+ item.cache_uri.should == 'http://www.google.com/search?q=cache:4styY9qq7tYJ:en.wikipedia.org'
15
+ item.visible_uri.should == 'en.wikipedia.org'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,63 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Response do
5
+ before :each do
6
+ @response = Google::Search::Response.new json_fixture('web-response')
7
+ end
8
+
9
+ describe "#initialize" do
10
+ it "should not throw an error when invalid" do
11
+ lambda { Google::Search::Response.new json_fixture('invalid-response') }.should_not raise_error
12
+ end
13
+
14
+ it "should set #items" do
15
+ @response.items.first.should be_a(Google::Search::Item)
16
+ @response.items.length.should == 4
17
+ end
18
+
19
+ it "should set item indices" do
20
+ @response.items[0].index.should == 0
21
+ @response.items[1].index.should == 1
22
+ @response.items[2].index.should == 2
23
+ end
24
+
25
+ it "should set item indices when page is present" do
26
+ @response = Google::Search::Response.new json_fixture('web-2-response')
27
+ @response.size.should == :small
28
+ @response.page.should == 1
29
+ @response.items[0].index.should == 4
30
+ @response.items[1].index.should == 5
31
+ @response.items[2].index.should == 6
32
+ end
33
+
34
+ it "should set #estimated_count" do
35
+ @response.estimated_count.should == 33400000
36
+ end
37
+
38
+ it "should set #page" do
39
+ @response.page.should == 0
40
+ end
41
+
42
+ it "should set #status" do
43
+ @response.status.should == 200
44
+ end
45
+
46
+ it "should set #details" do
47
+ @response.details.should be_nil
48
+ end
49
+ end
50
+
51
+ describe "#valid?" do
52
+ it "should return false when response status is not 200" do
53
+ response = Google::Search::Response.new 'responseStatus' => 400
54
+ response.should_not be_valid
55
+ end
56
+
57
+ it "should return true when status is 200" do
58
+ response = Google::Search::Response.new 'responseStatus' => 200, 'responseData' => { 'results' => [] }
59
+ response.should be_valid
60
+ end
61
+ end
62
+
63
+ end
File without changes
File without changes
@@ -0,0 +1,71 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Image do
5
+ before :each do
6
+ @search = Google::Search::Image.new :query => 'foo'
7
+ end
8
+
9
+ describe "#get_uri" do
10
+ describe "safety_level" do
11
+ it "should validate" do
12
+ @search.safety_level = :active
13
+ @search.get_uri.should include('safe=active')
14
+ @search.safety_level = :foo
15
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /safety_level/)
16
+ end
17
+
18
+ it "should provide alternative naming :none, :medium, :high" do
19
+ @search.safety_level = :none
20
+ @search.get_uri.should include('safe=off')
21
+ @search.safety_level = :medium
22
+ @search.get_uri.should include('safe=moderate')
23
+ @search.safety_level = :high
24
+ @search.get_uri.should include('safe=active')
25
+ end
26
+ end
27
+
28
+ describe "image_size" do
29
+ it "should validate" do
30
+ @search.image_size = :icon
31
+ @search.get_uri.should include('imgsz=icon')
32
+ @search.image_size = :small, :medium, :large
33
+ @search.get_uri.should include('imgsz=small%7Cmedium%7Clarge')
34
+ @search.image_size = :foo
35
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /image_size/)
36
+ end
37
+ end
38
+
39
+ describe "color" do
40
+ it "should validate" do
41
+ @search.color = 'blue'
42
+ @search.get_uri.should include('imgcolor=blue')
43
+ end
44
+ end
45
+
46
+ describe "image_type" do
47
+ it "should validate" do
48
+ @search.image_type = :lineart
49
+ @search.get_uri.should include('imgtype=lineart')
50
+ @search.image_type = :foo
51
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /image_type/)
52
+ end
53
+ end
54
+
55
+ describe "file_type" do
56
+ it "should validate" do
57
+ @search.file_type = :jpg
58
+ @search.get_uri.should include('as_filetype=jpg')
59
+ @search.file_type = :foo
60
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /file_type/)
61
+ end
62
+ end
63
+
64
+ describe "uri" do
65
+ it "should validate" do
66
+ @search.uri = 'http://foo.com'
67
+ @search.get_uri.should include('as_sitesearch=http%3A%2F%2Ffoo.com')
68
+ end
69
+ end
70
+ end
71
+ end
File without changes
@@ -0,0 +1,33 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::News do
5
+ before :each do
6
+ @search = Google::Search::News.new :query => 'foo'
7
+ end
8
+
9
+ describe "#get_uri" do
10
+ describe "relative_to" do
11
+ it "should set relative to geo" do
12
+ @search.relative_to = 'Edmonton Alberta'
13
+ @search.get_uri.should_not include('geo=Edmonton%20Alberta')
14
+ end
15
+ end
16
+
17
+ describe "topic" do
18
+ it "should validate" do
19
+ @search.topic = :world
20
+ @search.get_uri.should include('topic=world')
21
+ @search.topic = :foo
22
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /topic/)
23
+ end
24
+ end
25
+
26
+ describe "edition" do
27
+ it "should set edition" do
28
+ @search.edition = :en
29
+ @search.get_uri.should include('ned=en')
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search::Patent do
5
+ before :each do
6
+ @search = Google::Search::Patent.new :query => 'foo'
7
+ end
8
+
9
+ describe "#get_uri" do
10
+ describe "issued_only" do
11
+ it "should return issued and filed by default" do
12
+ @search.get_uri.should_not include('as_psrg')
13
+ @search.get_uri.should_not include('as_psra')
14
+ end
15
+
16
+ it "should return issued when true" do
17
+ @search.issued_only = true
18
+ @search.get_uri.should include('as_psrg=1')
19
+ end
20
+
21
+ it "should return filed when false" do
22
+ @search.issued_only = false
23
+ @search.get_uri.should include('as_psra=1')
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,89 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Google::Search do
5
+ before :each do
6
+ @search = Google::Search::Web.new :query => 'foo'
7
+ end
8
+
9
+ it "should throw an error when trying to initialize the base class" do
10
+ lambda { Google::Search.new :query => 'foo' }.should raise_error(Google::Search::Error)
11
+ end
12
+
13
+ it "should be enumerable" do
14
+ @search.to_a.first.should be_a(Google::Search::Item)
15
+ end
16
+
17
+ describe "#get_uri" do
18
+ it "should return a uri" do
19
+ @search.get_uri.should == 'http://www.google.com/uds/GwebSearch?start=0&rsz=large&hl=en&key=notsupplied&v=1.0&q=foo&filter=1'
20
+ end
21
+
22
+ it "should allow arbitrary key/value pairs" do
23
+ search = Google::Search::Web.new :query => 'foo', :foo => 'bar'
24
+ search.get_uri.should == 'http://www.google.com/uds/GwebSearch?start=0&rsz=large&hl=en&key=notsupplied&v=1.0&q=foo&filter=1&foo=bar'
25
+ end
26
+
27
+ describe "query" do
28
+ it "should raise an error when no query string is present" do
29
+ @search.query = nil
30
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /query/)
31
+ @search.query = ''
32
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /query/)
33
+ end
34
+ end
35
+
36
+ describe "version" do
37
+ it "should raise an error when it is not present" do
38
+ @search.version = nil
39
+ lambda { @search.get_uri }.should raise_error(Google::Search::Error, /version/)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "#get_raw" do
45
+ it "should return JSON string" do
46
+ @search.get_raw.should be_a(String)
47
+ end
48
+ end
49
+
50
+ describe "#get_hash" do
51
+ it "should return JSON converted to a hash" do
52
+ @search.stub!(:get_raw).and_return fixture('web-response.json')
53
+ @search.get_hash.should be_a(Hash)
54
+ end
55
+ end
56
+
57
+ describe "#get_response" do
58
+ it "should return a Response object" do
59
+ @search.stub!(:get_raw).and_return fixture('web-response.json')
60
+ @search.get_response.should be_a(Google::Search::Response)
61
+ end
62
+
63
+ it "should populate #raw" do
64
+ @search.stub!(:get_raw).and_return fixture('web-response.json')
65
+ @search.get_response.raw.should be_a(String)
66
+ end
67
+ end
68
+
69
+ describe "#next" do
70
+ it "should prepare offset" do
71
+ @search.size = :small
72
+ @search.next.offset.should == 0; @search.get_raw
73
+ @search.next.offset.should == 4; @search.get_raw
74
+ @search.next.offset.should == 8
75
+ @search.size = :large
76
+ @search.sent = false
77
+ @search.offset = 0
78
+ @search.next.offset.should == 0; @search.get_raw
79
+ @search.next.offset.should == 8; @search.get_raw
80
+ @search.next.offset.should == 16
81
+ end
82
+ end
83
+
84
+ describe "#response" do
85
+ it "should alias #get_response" do
86
+ @search.next.response.should be_a(Google::Search::Response)
87
+ end
88
+ end
89
+ end