Class: WikiScraper::WikiDisplay
- Inherits:
-
Object
- Object
- WikiScraper::WikiDisplay
- Defined in:
- lib/wiki_scraper/wiki_display.rb
Instance Method Summary collapse
- #blank ⇒ Object
- #clean_headings_and_paragraphs ⇒ Object
- #create_headings_and_paragraphs ⇒ Object
- #get_summary ⇒ Object
-
#initialize(page) ⇒ WikiDisplay
constructor
A new instance of WikiDisplay.
- #line ⇒ Object
- #print_subheadings ⇒ Object
- #print_summary ⇒ Object
- #print_title ⇒ Object
- #print_topic(number) ⇒ Object
-
#print_trio ⇒ Object
Prints title, summary, subheadings.
- #subheading_count ⇒ Object
Constructor Details
#initialize(page) ⇒ WikiDisplay
Returns a new instance of WikiDisplay.
10 11 12 13 14 15 16 |
# 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 |
Instance Method Details
#blank ⇒ Object
6 7 8 |
# File 'lib/wiki_scraper/wiki_display.rb', line 6 def blank puts "\e[2J\e[f" end |
#clean_headings_and_paragraphs ⇒ Object
45 46 47 48 49 50 51 52 53 |
# 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 ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
18 19 20 21 22 23 24 25 |
# 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 ⇒ Object
2 3 4 |
# File 'lib/wiki_scraper/wiki_display.rb', line 2 def line puts "----------------------------------------------" end |
#print_subheadings ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/wiki_scraper/wiki_display.rb', line 71 def print_subheadings if @heading_array.length > 0 index = 0 puts "Topics:" line @heading_array.each do |heading| puts "#{index + 1}. #{heading}" index += 1 end puts "\n" puts "#{index + 1}. To make another search" puts "#{index + 2}. To exit" line else puts "There are no topics to display." puts "Type '1' to search again or '2' to exit." line end end |
#print_summary ⇒ Object
66 67 68 69 |
# File 'lib/wiki_scraper/wiki_display.rb', line 66 def print_summary puts @summary line end |
#print_title ⇒ Object
61 62 63 64 |
# File 'lib/wiki_scraper/wiki_display.rb', line 61 def print_title puts "#{@title}" line end |
#print_topic(number) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/wiki_scraper/wiki_display.rb', line 91 def print_topic(number) blank line line puts "Topic #{number + 1}/#{subheading_count}: #{@heading_array[number]}" line line puts @h["#{@heading_array[number]}"] line end |
#print_trio ⇒ Object
Prints title, summary, subheadings
55 56 57 58 59 |
# File 'lib/wiki_scraper/wiki_display.rb', line 55 def print_trio #Prints title, summary, subheadings print_title print_summary print_subheadings end |
#subheading_count ⇒ Object
102 103 104 |
# File 'lib/wiki_scraper/wiki_display.rb', line 102 def subheading_count @heading_array.count end |