Class: ProjectEulerCli::ArchiveSearcher

Inherits:
Object
  • Object
show all
Includes:
Scraper
Defined in:
lib/project_euler_cli/archive_searcher.rb

Overview

Handles searching the problems

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scraper

#load_page, #load_problem_details, #load_recent, #lookup_totals

Constructor Details

#initializeArchiveSearcher

Returns a new instance of ArchiveSearcher.



12
13
14
15
16
# File 'lib/project_euler_cli/archive_searcher.rb', line 12

def initialize
  @results = []
  @searching = false
  @initial_search = true
end

Instance Attribute Details

#resultsObject (readonly)

Array of IDs corresponding to the problems found in last search



8
9
10
# File 'lib/project_euler_cli/archive_searcher.rb', line 8

def results
  @results
end

#searchingObject

Tracks whether there is an active search



10
11
12
# File 'lib/project_euler_cli/archive_searcher.rb', line 10

def searching
  @searching
end

Instance Method Details

#load_keywordsObject

Loads the problem numbers and titles for every page that is not loaded.



19
20
21
22
23
# File 'lib/project_euler_cli/archive_searcher.rb', line 19

def load_keywords
  puts "updating keywords..."

  0.upto(Page.total) { |page| load_page(page) }
end

#search(search_string) ⇒ Object

Performs a simple search of the problems. It accepts multiple terms and recognizes quoted phrases. Results will contain all of the search terms.

  • terms - String of search terms



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/project_euler_cli/archive_searcher.rb', line 29

def search(search_string)
  load_keywords if Page.visited != (0..Page.total).to_a

  puts "searching..."
  @searching = true

  search_string.downcase!
  terms = search_string.scan(/"[^"]*"/)
  terms.each { |term| search_string.slice!(term) }
  terms.collect! { |term| term.gsub("\"", '') }
  terms += search_string.split(' ')

  @results = (1..Problem.total).select do |i|
    terms.all? { |term| Problem[i].title.downcase.include?(term) }
  end
end