class WikiScraper::WikiDisplay

Public Class Methods

new(page) click to toggle source
# File lib/wiki_scraper/wiki_display.rb, line 10
def initialize(page)
  blank
  @page = page
  @title = @page.css("#firstHeading").text
  get_summary
  create_headings_and_paragraphs
end

Public Instance Methods

blank() click to toggle source
# File lib/wiki_scraper/wiki_display.rb, line 6
def blank
  puts "\e[2J\e[f"
end
clean_headings_and_paragraphs() click to toggle source
# File lib/wiki_scraper/wiki_display.rb, line 45
def clean_headings_and_paragraphs
  bad_headings = ["Contents", "See also", "References", "Bibliography", "External links", "Navigation menu", "Notes", "Further reading", "Cited sources"]
  @heading_array.each_with_index do |heading|
    if bad_headings.any? { |bad| bad == heading } || @h[heading].nil?
      @heading_array = @heading_array - [heading]
      @h.delete(heading.to_s)
    end
  end
end
create_headings_and_paragraphs() click to toggle source
# File lib/wiki_scraper/wiki_display.rb, line 27
def create_headings_and_paragraphs
  @h = Hash.new
  @heading_array = []

  @page.css(".mw-parser-output").children.map do |thing|
    if thing.name == "h2"
      @heading_array << thing.text.gsub("[edit]", "").strip
    else
      if !@heading_array.empty? && thing.name == "p"
        @h[@heading_array.last] ||= []
        @h[@heading_array.last] << thing.text.gsub(/\[\d\d?\d?\]/, "")
        @h[@heading_array.last] << "\n"
      end
    end
  end
  clean_headings_and_paragraphs
end
get_summary() click to toggle source
# File lib/wiki_scraper/wiki_display.rb, line 18
def get_summary
  paragraphs = @page.css("p")
  if paragraphs.text.strip == "Other reasons this message may be displayed:"
    @summary = "Wikipedia does not have an article with this exact name."
  else
    @summary = paragraphs.find { |para| para.text.length > 50 }.text.gsub(/\[\d\d?\d?\]/, "")
  end
end
line() click to toggle source
# File lib/wiki_scraper/wiki_display.rb, line 2
def line
  puts "----------------------------------------------"
end
print_subheadings() click to toggle source
print_summary() click to toggle source
print_title() click to toggle source
print_topic(number) click to toggle source
print_trio() click to toggle source
subheading_count() click to toggle source
# File lib/wiki_scraper/wiki_display.rb, line 102
def subheading_count
  @heading_array.count
end