Class: ProjectEulerCli::ArchiveSearcher
- Inherits:
-
Object
- Object
- ProjectEulerCli::ArchiveSearcher
- Defined in:
- lib/project_euler_cli/archive_searcher.rb
Overview
Handles searching the problems
Instance Attribute Summary collapse
-
#results ⇒ Object
readonly
Array of IDs corresponding to the problems found in last search.
-
#searching ⇒ Object
Tracks whether there is an active search.
Instance Method Summary collapse
-
#initialize(archive_data) ⇒ ArchiveSearcher
constructor
A new instance of ArchiveSearcher.
-
#load_terms ⇒ Object
Loads the problem numbers and titles for every page that is not loaded.
-
#search(terms) ⇒ Object
Performs a simple search of the problems.
Constructor Details
#initialize(archive_data) ⇒ ArchiveSearcher
Returns a new instance of ArchiveSearcher.
10 11 12 13 14 15 16 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 10 def initialize(archive_data) @archive_data = archive_data @results = [] @searching = false @initial_search = true end |
Instance Attribute Details
#results ⇒ Object (readonly)
Array of IDs corresponding to the problems found in last search
6 7 8 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 6 def results @results end |
#searching ⇒ Object
Tracks whether there is an active search
8 9 10 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 8 def searching @searching end |
Instance Method Details
#load_terms ⇒ Object
Loads the problem numbers and titles for every page that is not loaded.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 19 def load_terms puts "updating keywords..." # Loading each archive page 1.upto(@archive_data[:num_pages]) do |page| unless @archive_data[:visited_pages].include?(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 end # Loading the recent problems unless @archive_data[:visited_pages].include?(0) 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 end |
#search(terms) ⇒ Object
Performs a simple search of the problems. It accepts multiple terms. Results will contain any of the terms
-
terms- String of search terms
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 61 def search(terms) if @initial_search @initial_search = false load_terms end puts "searching..." @results.clear @searching = true terms.downcase.split(' ').each do |term| for i in 1..@archive_data[:num_problems] if @archive_data[:problems][i].downcase.include?(term.downcase) @results << i end end end end |