class Whatsa::Disambig

Attributes

descriptions[RW]
items[R]
title[R]

Public Class Methods

new(noko_doc) click to toggle source
# File lib/whatsa/disambig.rb, line 5
def initialize(noko_doc)
  @title = noko_doc.css('h1').text
  @items = noko_doc.css('#mw-content-text li')
  @descriptions = make_descriptions
end

Public Instance Methods

choices() click to toggle source
# File lib/whatsa/disambig.rb, line 11
def choices
  self.descriptions.keys
end
choose_article(choice) click to toggle source
# File lib/whatsa/disambig.rb, line 15
def choose_article(choice)
  if choice.to_i > 0
    Whatsa::Scraper.new(choices[choice.to_i - 1]).make_article
  else
    Whatsa::Scraper.new(choice).make_article
  end
end

Private Instance Methods

make_descriptions() click to toggle source

make a hash with links and their descriptions

# File lib/whatsa/disambig.rb, line 26
def make_descriptions
  self.items.inject({}) do |memo, item|
    unless item.css('a').empty?
      key = item.css('a').first.text
      toc_link = item["class"] && item["class"].match(/toc/)
      dmb_link = key.match("(disambiguation)")
      all_link = key.match("All pages with titles")
      unless toc_link || dmb_link || all_link
        desc = item.text.split("\n").first.strip
        memo[key] = desc.gsub(/^#{Regexp.escape(key)}[^\w"]/, "").strip
      end
    end
    memo
  end
end