Class: GoogleCompanySearch

Inherits:
Object
  • Object
show all
Defined in:
lib/google_company_search.rb,
lib/google_company_search/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.search(name, location = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/google_company_search.rb', line 6

def self.search(name, location = nil)
  agent = Mechanize.new
  page = agent.get('http://www.google.com/')
  google_form = page.form('f')
  google_form.q = 
    if location.nil? 
      name
    else
      "#{name} #{location}"
    end
  page = agent.submit(google_form, google_form.buttons.first)
  valid_url = nil
  page.links.each do |link|
    if link.href.to_s =~/url.q/
      str=link.href.to_s
      strList=str.split(%r{=|&}) 
      url=strList[1] 
      if is_valid_link(url)
        valid_url = url
        break
      end
    end 
  end
  valid_url
end