Class: WikiScraper::WikiDisplay

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki_scraper/wiki_display.rb

Instance Method Summary collapse

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

#blankObject



6
7
8
# File 'lib/wiki_scraper/wiki_display.rb', line 6

def blank
  puts "\e[2J\e[f"
end

#clean_headings_and_paragraphsObject



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_paragraphsObject



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_summaryObject



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

#lineObject



2
3
4
# File 'lib/wiki_scraper/wiki_display.rb', line 2

def line
  puts "----------------------------------------------"
end


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


66
67
68
69
# File 'lib/wiki_scraper/wiki_display.rb', line 66

def print_summary
  puts @summary
  line
end


61
62
63
64
# File 'lib/wiki_scraper/wiki_display.rb', line 61

def print_title
  puts "#{@title}"
  line
end


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

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_countObject



102
103
104
# File 'lib/wiki_scraper/wiki_display.rb', line 102

def subheading_count
  @heading_array.count
end