Class: Roogle

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/roogle.rb

Constant Summary collapse

THE_GOOGLE =
"http://google.com"

Instance Method Summary collapse

Constructor Details

#initializeRoogle

Returns a new instance of Roogle.



10
11
12
13
# File 'lib/roogle.rb', line 10

def initialize
  @results = []
  @mech = ::Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' }
end

Instance Method Details

#do_search(query) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/roogle.rb', line 40

def do_search(query)
  search_result = nil
  @mech.get(THE_GOOGLE) do |page|
    search_result = page.form_with(:name => 'f') do |search|
      search.q = query
    end.submit
  end

  scan_page(search_result)
end

#scan_page(page) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/roogle.rb', line 15

def scan_page(page)
  page.parser.css("h3.r a").each do |link|
    @results << link
    print red, "%2s " % [@results.size], reset
    print yellow, link.text[0..60].ljust(62), reset
    print " ", link.attr("href").split("/")[2]

    puts
  end

  print ":"

  n = STDIN.gets.chomp

  if n == 'n'
    page = page.links.find {|l| l.text == "Next" }.click
    scan_page(page)
  elsif n == 'p'
    page = page.links.find {|l| l.text == "Previous" }.click
    scan_page(page)
  end

  ::Launchy.open(@results[n.to_i - 1].attr('href')) unless n.to_i < 1
end