class Googling::Search
Attributes
list[R]
response[R]
Public Class Methods
new(keywords)
click to toggle source
# File lib/googling/search.rb, line 9 def initialize(keywords) @keywords = keywords.to_s.to_uri end
Public Instance Methods
execute()
click to toggle source
# File lib/googling/search.rb, line 13 def execute make_request parse_results end
Private Instance Methods
make_request()
click to toggle source
# File lib/googling/search.rb, line 20 def make_request uri = URI.parse("https://www.google.com/search?q=#{@keywords}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) @response = http.request(request) end
parse_results()
click to toggle source
# File lib/googling/search.rb, line 28 def parse_results page = Nokogiri::HTML(response.body) page.encoding = 'utf-8' nodes = page.css('li.g') nodes = nodes.select { |r| r.css('table').empty? } nodes.map do |r| title = r.css('h3 a').text link = r.css('h3 a').first['href'].sub("/url?q=","").sub(/\&.*/, "") description = r.css('div.s span.st').text Result.new(title: title, link: link, description: description) end end