robhurring-gspot 0.2

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.
Files changed (5) hide show
  1. data/CHANGELOG +2 -0
  2. data/README +22 -0
  3. data/examples/example.rb +55 -0
  4. data/lib/gspot.rb +125 -0
  5. metadata +57 -0
@@ -0,0 +1,2 @@
1
+ 0.1
2
+ * went to a gemspec rather than rake task
data/README ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008 Rob Hurring
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby -rrubygems
2
+ require '../lib/google_search'
3
+ require 'pp'
4
+
5
+ # Some example uses, this lib is still _very_ basic and will
6
+ # have some rough edges, but i works for basic queries.
7
+
8
+ # Searching the web
9
+
10
+ cool_sites = GSpot::Search.web('cool', :max => 1)
11
+ puts "I found #{cool_sites.count} cooool sites"
12
+ puts '-'*30
13
+ cool_sites.each do |cool_site|
14
+ puts "A cool site:"
15
+ puts "Title: " << cool_site.title
16
+ puts "URL: " << cool_site.url
17
+ puts "About: " << cool_site.content
18
+ puts
19
+ end
20
+
21
+ # Goin' Local
22
+
23
+ local_resturaunts = GSpot::Search.local('resturaunts in new jersey', :max => 2)
24
+ puts "I found #{local_resturaunts.count} local resturaunt(s)!"
25
+ puts '-'*30
26
+ local_resturaunts.each do |local_resturaunt|
27
+ puts "Resturaunt:"
28
+ puts local_resturaunt.title
29
+ puts 'Located At: ' << local_resturaunt.addressLines.join("\n")
30
+ puts 'Phone: ' << local_resturaunt.phoneNumbers.map{ |pn| pn['number'] }.join(", ")
31
+ puts
32
+ end
33
+
34
+ # Images Rawk!
35
+
36
+ lolcats = GSpot::Search.images('site:icanhascheezburger.com filetype:jpg', :max => 10)
37
+ puts "I CAN HAZ #{lolcats.count} LOLZ?"
38
+ puts '-'*30
39
+ lolcats.each do |lolcat|
40
+ puts "At " << lolcat.content << ' some kitteh did this'
41
+ puts 'Image: ' << lolcat.url
42
+ puts
43
+ end
44
+
45
+ # Videos!
46
+
47
+ cool_explosion = GSpot::Search.videos('cool explosion', :max => 1).results.first
48
+ puts "I have a cool explosion"
49
+ puts '-'*30
50
+ puts "Title: " << cool_explosion.title
51
+ puts "Runtime: " << cool_explosion.duration << ' seconds.'
52
+ puts "Publisher: " << cool_explosion.publisher
53
+ puts "Published: " << cool_explosion.published
54
+ puts "View!: " << cool_explosion.url
55
+ puts
@@ -0,0 +1,125 @@
1
+ # == About
2
+ #
3
+ # Author:: Rob Hurring <rob@ubrio.us>
4
+ # Website:: http://robhurring.com
5
+ #
6
+ # This is s a stupid simple way to make managing crontabs easier
7
+ # It allows you to write basic cron tasks in ruby and output the
8
+ # complete crontab. I find it easier with setup tasks and repetitive
9
+ # schedules and such. Really only meant as a simple cron mgt. script
10
+ #
11
+ # == License
12
+ #
13
+ # Copyright (c) 2008 Rob Hurring
14
+ #
15
+ # Permission is hereby granted, free of charge, to any person
16
+ # obtaining a copy of this software and associated documentation
17
+ # files (the "Software"), to deal in the Software without
18
+ # restriction, including without limitation the rights to use,
19
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ # copies of the Software, and to permit persons to whom the
21
+ # Software is furnished to do so, subject to the following
22
+ # conditions:
23
+ #
24
+ # The above copyright notice and this permission notice shall be
25
+ # included in all copies or substantial portions of the Software.
26
+ #
27
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34
+ # OTHER DEALINGS IN THE SOFTWARE.
35
+ require 'json'
36
+ require 'open-uri'
37
+ # GSpot is a simple ruby wrapper for Google's AJAX search API
38
+ # It supports:
39
+ # * web searches
40
+ # * image searches
41
+ # * local searches
42
+ # * video searches
43
+ # This is a very crude class and was made for a project of mine (which is why it isn't very refined
44
+ # at the moment.) I do plan on polishing it up a bit when I get time, but this should do for now :)
45
+ module GSpot
46
+ class BadEndpointError < StandardError; end
47
+ class InvalidVersionError < StandardError; end
48
+ # This guy does the actual searching... srsly.
49
+ # check out the examples.rb file for example usage
50
+ class Search
51
+ attr_reader :results, :estimatedResultCount, :moreResultsUrl, :pages, :viewport, :responseStatus
52
+ ENDPOINTS = {
53
+ :base => %{http://ajax.googleapis.com/ajax/services/search},
54
+ :web => %{/web},
55
+ :local => %{/local},
56
+ :images => %{/images},
57
+ :videos => %{/video},
58
+ :news => %{/news}
59
+ }
60
+ # options can be
61
+ # * :version => API version you wish to use
62
+ # * :max => max amount of results you wish to retrieve
63
+ def initialize(query, service, options = {})
64
+ @version = options[:version] || 1.0
65
+ @results = []
66
+ @query = URI.escape(query)
67
+ @service = service || :web
68
+ @max = options[:max] || 10
69
+ @responseStatus = nil
70
+ @viewport, @pages, @moreResultsUrl, @estimatedResultCount = nil
71
+ end
72
+ # calles Googles REST interface and gets back the JSON data
73
+ # Google by default sends +4+ results at a time, so if we request a :max > 4
74
+ # we simply loop and build a result set until we hit our :max, Google should
75
+ # also put a cap on the total results at around 32
76
+ def search
77
+ (@max.to_f / 4).ceil.times do |page|
78
+ start = page * 4
79
+ endpoint = '%s%s?v=%s&q=%s&start=%d' % [ENDPOINTS[:base], ENDPOINTS[@service], @version, @query, start]
80
+ json = JSON.parse(open(endpoint).read)
81
+ if json['responseData'] and not json['responseData']['results'].empty?
82
+ json['responseData']['results'].each do |data|
83
+ @results << SearchResult.new(data) unless @results.size >= @max
84
+ end
85
+ @estimatedResultCount ||= json['responseData']['cursor']['estimatedResultCount'].to_i
86
+ @moreResultsUrl ||= json['responseData']['cursor']['moreResultsUrl']
87
+ @pages ||= json['responseData']['cursor']['pages']
88
+ @viewport ||= json['responseData']['cursor']['viewport']
89
+ else
90
+ raise InvalidVersionError('Invalid Version') if json['responseDetails'] == 'invalid version'
91
+ end
92
+ @responseStatus = json['responseStatus']
93
+ end
94
+ self
95
+ end
96
+ def count
97
+ @results.size
98
+ end
99
+ def each
100
+ @results.each { |result| yield(result) }
101
+ end
102
+ class << self
103
+ # overloading for endpoints, calling
104
+ # GSpot::Search.web('what') will do a web search, apply the same to images, video & local
105
+ def method_missing(sym, *args)
106
+ valid_endpoints = ENDPOINTS.keys.select{ |e| e != :base }
107
+ raise(BadEndpointError, %{The endpoint you requested is invalid. "#{sym}" Available endpoints are: #{valid_endpoints}}) unless valid_endpoints.include?(sym)
108
+ Search.new(args[0], sym, args[1] || {}).search
109
+ end
110
+ end
111
+ end
112
+ # A generic search result object used to transform JSON into a ruby object
113
+ # all methods correspond to those of the JSON's ['responseData']['results'] data
114
+ # for more on what is available just +pp+ them out, or check Google for the JSON
115
+ # response variables, they should all be tucked in there
116
+ class SearchResult
117
+ def initialize(data = {})
118
+ data.each do |key, value|
119
+ value.gsub!(/<\/?[^>]*>/, "") if value.respond_to?(:gsub)
120
+ instance_variable_set "@#{key}", value
121
+ instance_eval %{ def #{key}; @#{key}; end }
122
+ end
123
+ end
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robhurring-gspot
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Rob Hurring
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: rob@zerobased.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - CHANGELOG
27
+ - lib/gspot.rb
28
+ - examples/example.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/robhurring/gspot
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --main
34
+ - README
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: gspot
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Google AJAX search implemented in ruby
56
+ test_files: []
57
+