Class: Descartes::Google

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/descartes/modules/google.rb

Instance Method Summary collapse

Instance Method Details

#execute(m, query) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/descartes/modules/google.rb', line 24

def execute(m, query)
  res = search query

  if res.empty?
    # m.reply 'No results found.'
    m.reply 'Nessun risultato.'
  else
    res[0..3].each { |r| m.reply "#{r[:title]} - #{r[:url]}" }
  end
end

#search(query) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/descartes/modules/google.rb', line 35

def search(query)
  page = Nokogiri::HTML open("http://www.google.com/search?q=#{CGI.escape(query)}")

  [].tap { |res|
    page.search('cite').each { |r|
      res << { url: r.inner_text }
    }

    page.xpath('//h3[@class="r"]').each_with_index { |r, i|
      res[i][:title] = r.child.inner_text
    }
  }
end