Class: ProjectEulerCli::ArchiveViewer
- Inherits:
-
Object
- Object
- ProjectEulerCli::ArchiveViewer
- Defined in:
- lib/project_euler_cli/archive_viewer.rb
Overview
Handles the work of displaying information about the problems.
Instance Method Summary collapse
-
#display_custom_page(list) ⇒ Object
Displays a custom page of problems given by an array of IDs.
-
#display_page(page) ⇒ Object
Displays the problem numbers and titles for an individual page of the archive.
-
#display_problem(id) ⇒ Object
Displays the details of an individual problem.
-
#display_recent ⇒ Object
Displays the 10 most recently added problems.
-
#initialize(archive_data) ⇒ ArchiveViewer
constructor
A new instance of ArchiveViewer.
-
#load_page(page) ⇒ Object
Loads the problem numbers and titles for an individual page of the archive.
-
#load_problem_details(id) ⇒ Object
Loads the details of an individual problem.
-
#load_recent ⇒ Object
Loads in all of the problem numbers and titles from the recent page.
Constructor Details
#initialize(archive_data) ⇒ ArchiveViewer
Returns a new instance of ArchiveViewer.
6 7 8 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 6 def initialize(archive_data) @archive_data = archive_data end |
Instance Method Details
#display_custom_page(list) ⇒ Object
Displays a custom page of problems given by an array of IDs.
-
list- Array of problem IDs
108 109 110 111 112 113 114 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 108 def display_custom_page(list) puts list.each do |id| puts "#{id} - #{@archive_data[:problems][id]}" end end |
#display_page(page) ⇒ Object
Displays the problem numbers and titles for an individual page of the archive.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 54 def display_page(page) load_page(page) unless @archive_data[:visited_pages].include?(page) puts start = (page - 1) * 50 + 1 for i in start...start + 50 unless i >= @archive_data[:num_problems] - 9 puts "#{i} - #{@archive_data[:problems][i]}" end end end |
#display_problem(id) ⇒ Object
Displays the details of an individual problem.
-
id- ID of the problem to be displayed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 87 def display_problem(id) load_problem_details(id) if @archive_data[:problem_details][id].empty? puts puts "#{@archive_data[:problems][id]}".upcase puts "Problem #{id}" puts puts @archive_data[:problem_details][id][:published] puts @archive_data[:problem_details][id][:solved_by] if id < @archive_data[:num_problems] - 9 puts @archive_data[:problem_details][id][:difficulty] end puts puts "https://projecteuler.net/problem=#{id}" end |
#display_recent ⇒ Object
Displays the 10 most recently added problems.
27 28 29 30 31 32 33 34 35 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 27 def display_recent load_recent unless @archive_data[:visited_pages].include?(0) puts (@archive_data[:num_problems]).downto(@archive_data[:num_problems] - 9) do |i| puts "#{i} - #{@archive_data[:problems][i]}" end end |
#load_page(page) ⇒ Object
Loads the problem numbers and titles for an individual page of the archive.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 38 def load_page(page) html = open("https://projecteuler.net/archives;page=#{page}") fragment = Nokogiri::HTML(html) problem_links = fragment.css('#problems_table td a') i = (page - 1) * 50 + 1 problem_links.each do |link| @archive_data[:problems][i] = link.text i += 1 end @archive_data[:visited_pages] << page end |
#load_problem_details(id) ⇒ Object
Loads the details of an individual problem.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 68 def load_problem_details(id) html = open("https://projecteuler.net/problem=#{id}") fragment = Nokogiri::HTML(html) problem_info = fragment.css('div#problem_info span span') details = problem_info.text.split(';') @archive_data[:problem_details][id][:published] = details[0].strip @archive_data[:problem_details][id][:solved_by] = details[1].strip # recent problems do not have a difficult rating if id < @archive_data[:num_problems] - 9 @archive_data[:problem_details][id][:difficulty] = details[2].strip end end |
#load_recent ⇒ Object
Loads in all of the problem numbers and titles from the recent page.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/project_euler_cli/archive_viewer.rb', line 11 def load_recent html = open("https://projecteuler.net/recent") fragment = Nokogiri::HTML(html) problem_links = fragment.css('#problems_table td a') i = @archive_data[:num_problems] problem_links.each do |link| @archive_data[:problems][i] = link.text i -= 1 end @archive_data[:visited_pages] << 0 end |