Sha256: da1f41cd0bc31b4f5badda4e4c7c9f4a6a596c7730eccfc1aabb949010acb3a7

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require "gsearch-parser/version"
require 'open-uri'
require 'nokogiri'

module GSearchParser

  def GSearchParser.webSearch(query)
    GoogleSearch.new(query)
  end

end

###################################################
#                                                 #
#              GoogleSearch Class                 #
#                                                 #
###################################################
class GoogleSearch
  attr_accessor :results

  # Class initializer
  def initialize(query)
    # Initialize array
    @results = Array.new

    # TODO: Format query

    # Fetch page
    searchPage = Nokogiri::HTML(open("http://google.com/search?sourceid=chrome&q=#{query}", 
      'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.152 Safari/535.19'))

    # Iterate over each Google result list element and extract data
    searchPage.css('li.g').each do |result|
      title = result.css('h3').first.content
      content = result.css('span.st').first.inner_html
      uri = result.css('cite').inner_html
      @results << Result.new(title, content, uri)
    end
  end

  # Iterator over results
  def each(&blk)
    @results.each(&blk)
  end

  ###################################################
  #                                                 #
  #                Result Class                     #
  #                                                 #
  ###################################################
  class Result
    attr_accessor :title, :content, :uri

    # Class initializer
    def initialize(title, content, uri)
      @title = title
      @content = content
      @uri = uri
    end

  end # Result

end # GoogleSearch

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gsearch-parser-0.1.0 lib/gsearch-parser.rb