Sha256: fc1580102d4fd5fc364f9ea23a6cda4a63c87261d7b270b08783a21a6354b395

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

require "gss/version"
require "rubygems"
require "nokogiri"
require "open-uri"
require "certified"

module Gss
    attr_accessor :site_name, :search_for, :gss_url, :noko

    # Initializes Gss with a site to search and 
    # things to search for. Returns top result, and top results.
    #
    # @param site_name [String] site to be searched.
    # @param search_for [String] string to search for.
    # @return [Array] contains Hash objects for the top result, and top results.
    def self.return_results(site_name, search_for)
        @site_name = site_name
        @search_for = search_for.gsub(" ", "+")
        @gss_url = "https://www.google.com/search?q=site%3A#{@site_name}+#{@search_for}"
        @noko = Nokogiri::HTML(open(@gss_url))

        return [top_result(), top_results()]
    end

    # This will return a Hash object containing information about
    # the top link.
    # 
    # Access hash:
    # name - link title.
    # url - link to the site.
    # description - short snippet about link.
    #
    # @return result [Hash] contains info about the top link.
    def self.top_result()
        result = Hash.new
        result[:name] = @noko.css("li.g a")[0].text
        result[:url] = "http://google.com" + @noko.css("li.g h3 a")[0]["href"]
        result[:description] = @noko.css("li.g span.st")[0].text

        return result
    end

    # This will return as array of Hash objects containing information about
    # the top links.
    #
    # @return results [Array] array of hash objects that contain info about top links
    def self.top_results()
        results = []

        @noko.css("li.g").each do |link|
            # puts link
            result = Hash.new
            result[:name] = link.css("h3")[0].text
            result[:url] = "http://google.com" + link.css("h3 a")[0]["href"]
            result[:description] = link.css("span.st")[0].text

            results << result
        end

        return results
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gss-0.0.6 lib/gss.rb
gss-0.0.5 lib/gss.rb