class Whatsa::Scraper

Constants

WIKISEARCH

Attributes

page[R]
query[R]

Public Class Methods

new(term) click to toggle source
# File lib/whatsa/scraper.rb, line 12
def initialize(term)
  # only keep word chars and parens, turn everything between each 'word'
  # to a single '+' and remove '+'s at the beginning and end if they're there
  @query = url_friendly(term)

  # store the page in an instance variable so we don't keep polling the site
  @page = Nokogiri::HTML(open(WIKISEARCH + self.query))
end

Public Instance Methods

article?() click to toggle source
# File lib/whatsa/scraper.rb, line 29
def article?
  !self.page.css('#ca-nstab-main').empty? && !disambig?
end
disambig?() click to toggle source
# File lib/whatsa/scraper.rb, line 33
def disambig?
  !self.page.css('#disambigbox').empty?
end
make_article() click to toggle source
# File lib/whatsa/scraper.rb, line 37
def make_article
  if article?
    Whatsa::Article.new(self.page)
  elsif results_page? && !not_found?
    first_title = self.page.css('.mw-search-results li a').first.text
    self.class.new(first_title).make_article
  elsif disambig?
    self.class.new(make_disambig.choices.first).make_article
  else
    nil
  end
end
make_disambig() click to toggle source
# File lib/whatsa/scraper.rb, line 50
def make_disambig
  disambig? ? Whatsa::Disambig.new(self.page) : nil
end
not_found?() click to toggle source
# File lib/whatsa/scraper.rb, line 25
def not_found?
  !self.page.css('.mw-search-nonefound').empty?
end
results_page?() click to toggle source
# File lib/whatsa/scraper.rb, line 21
def results_page?
  !self.page.css('.searchresults').empty?
end