Class: Google
- Inherits:
-
Object
- Object
- Defined in:
- lib/domain-finder/google.rb
Overview
Simple class for searching google and getting results
Defined Under Namespace
Classes: Result
Constant Summary collapse
- BASE_SEARCH_URL =
"http://www.google.com/search?q="
Class Method Summary collapse
- .fetch_url(url) ⇒ Object
- .parse_results(html) ⇒ Object
- .search(query, options = {}) ⇒ Object
- .search_url(query, options = {}) ⇒ Object
Class Method Details
.fetch_url(url) ⇒ Object
12 13 14 |
# File 'lib/domain-finder/google.rb', line 12 def self.fetch_url url open(url).read end |
.parse_results(html) ⇒ Object
16 17 18 |
# File 'lib/domain-finder/google.rb', line 16 def self.parse_results html ( Hpricot(html) / 'div.g' ).map { |result| Result.new(result) } end |
.search(query, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/domain-finder/google.rb', line 20 def self.search query, = {} if [:pages] and [:pages] > 1 results = [] [:pages].times do |i| results += parse_results( fetch_url(search_url(query, :page => i+1)) ) end results else parse_results( fetch_url(search_url(query)) ) end end |
.search_url(query, options = {}) ⇒ Object
6 7 8 9 10 |
# File 'lib/domain-finder/google.rb', line 6 def self.search_url query, = {} url = BASE_SEARCH_URL + URI.encode(query) url += "&start=#{ ([:page] - 1) * 10 }" if [:page] and [:page] != 1 url end |