lib/gsearch-parser.rb in gsearch-parser-0.0.2 vs lib/gsearch-parser.rb in gsearch-parser-0.0.3
- old
+ new
@@ -1,7 +1,63 @@
require "gsearch-parser/version"
+require 'open-uri'
+require 'nokogiri'
-module Gsearch
- module Parser
- # Your code goes here...
+module GSearchParser
+ def 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
+
+ # TODO: Fetch page
+ searchPage = Nokogiri::HTML(open("http://google.com/search?q=#{query}"))
+
+ @results << Result.new('title', 'content', "http://www.google.com")
+
+ # Iterate over all search result divs and parse content into Result objects,
+ # and finally store these in the results array
+ #searchPage.css('li.g > div.vsc').each do |result|
+ # title = result.css('h3.r > a.l').content
+ #content = result.css('div.s > span.st').content
+ #@results << Result.new('title', 'content', "http://www.google.com")
+ #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
+