Class: Google

Inherits:
Object
  • Object
show all
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

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, options = {}
  if options[:pages] and options[:pages] > 1
    results = []
    options[: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, options = {}
  url =  BASE_SEARCH_URL + URI.encode(query)
  url += "&start=#{ (options[:page] - 1) * 10 }" if options[:page] and options[:page] != 1
  url
end