class Ralert

Attributes

next_page[RW]
results[RW]

Public Class Methods

new(query, options = nil) click to toggle source
# File lib/ralert.rb, line 15
def initialize(query, options = nil)
  @results = Array.new

  # Take care of spaces and other special
  # characters.
  query = transform_query(query) if query.index(" ")
  
  # Instantiante a new search options object if
  # the user hasn't provided any when calling
  # the class
  options = SearchOptions.new unless !options.nil?

  uri = construct_uri(query, options)
  @results = perform_search(uri)
end

Public Instance Methods

construct_uri(query, options) click to toggle source

Takes the query string and a SearchOptions object and constructs a new search query.

# File lib/ralert.rb, line 35
def construct_uri(query, options)
  base_uri = "https://google.com/search?q="
  
  if options.literal
      query = "%22" + query + "%22" 
  end

  if !options.date_range.nil?
    query += "&tbs=qdr:#{options.date_range}"
  else
    query += "&tbs=qdr:w"
  end

  if options.sort_by == 'd'
    query += ",sbd:1"
  end
  
  if options.safe == 'on'
    query += "&safe=on"
  else
    query += "&safe=off"
  end

  query += "&tbm=#{options.mode}"

  return base_uri + query
end
each(&blk) click to toggle source
# File lib/ralert.rb, line 155
def each(&blk)
  @results.each(&blk)
end
next_page_missing() click to toggle source

Checks if this is the last of the search result pages available.

# File lib/ralert.rb, line 122
def next_page_missing
  return @page.at_css("table#nav tr td.b:last-child").at_css("a").nil?
end
next_results(page_number = 1) click to toggle source

Performs the search-parse-update routine on the next page of search results if available.

# File lib/ralert.rb, line 139
def next_results(page_number = 1)
  page_number.times.each do
    !next_page_missing ? perform_search(@next_page) : break
  end

  return @results
end
node_from_item(item) click to toggle source

Takes an HTML li block which represents a search result and extracts all the information from it like: a title, a link, a (relative) date and the articles source.

# File lib/ralert.rb, line 99
def node_from_item(item)
  result_node = Result.new
  title = '' 

  link = item.at('h3.r a')
  meta = item.search('div.slp span.f').inner_html
  
  link.children.each do |c|
    title += c
  end

  result_node.title  = title
  result_node.source = meta.split('-')[0]
  result_node.date = meta.split('-')[1]
  result_node.link = link['href'].gsub!(/\/url\?q\=/, '').gsub!(/\&sa\=.*/,'')

  return result_node
end
parse_results() click to toggle source

Parses the page resulting from the search query and returns the search items found in a result array.

# File lib/ralert.rb, line 80
def parse_results
  cur_results = Array.new

  @page.search('li.g').each do |item|
    cur_results << node_from_item(item)
  end
  
  update_next_page unless next_page_missing
  @results += cur_results
  
  return cur_results
end
transform_query(q) click to toggle source

Takes a text query and substitutes spaces for plus signs as the google search engine expects to be fed with.

# File lib/ralert.rb, line 151
def transform_query(q)
  return q.gsub!(/\s/, '+')
end
update_next_page() click to toggle source

Updates the @next_page instance variable to point to the next search result page.

# File lib/ralert.rb, line 130
def update_next_page
  next_uri = @page.at_css("table#nav tr td.b:last-child").at_css("a")['href']
  @next_page = "http://www.google.com" + next_uri
end