project_euler_cli 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0016fc054fcdc5ebc58a58ae77500b6a59776e38a8c2e55c4a6af7b69848007
4
- data.tar.gz: dd93295b9e645b7038f8eed4de1ff68f6fce96151530e791d911e9a6cb1c5e86
3
+ metadata.gz: 6779626e07ad722584aa34beb3a2f3f2d0db6be50e19c77d8d1cbad050befb23
4
+ data.tar.gz: 5247763643de597db2903e2973332e921d5dcb954d0af74d0f9fe104934439eb
5
5
  SHA512:
6
- metadata.gz: 775225f6b9fc001b3f5779ed6d1521b01914512d04494bfd2ed9feceafadb17555df1514f27b71039acf08c314f35782d396c5d70bf4485415fc2c8456caff85
7
- data.tar.gz: 839bb873d2d478773011d8627ea3dbb57ee18e4e61bef37e6983cc8a17938de7db660e055ca10bbbe841f9b766a7a0a9a1b1db77908e570121a68e9c4c5f8cf1
6
+ metadata.gz: 4d3dffe05c81249a33cf6a1eda98cd1df94d35da6280ae46e25068bcd22b0750705d4461486c0cc3df4b7fd37b405ef57185d902c72a3c0c1c6223d4b4e02e40
7
+ data.tar.gz: e70681145d9f1dfefa4528404b3e8ddedbd66332963f13a5c2f81818af94651da308311b170504815fdca67aac88199781e9b13ee6aa8c652827caa33f4d1abd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- project_euler_cli (1.1.1)
4
+ project_euler_cli (1.1.2)
5
5
  nokogiri (~> 1.8)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  module ProjectEulerCli
2
2
 
3
- # Controller class that manages the archive system. It holds the archive data used
4
- # by ArchiveViewer and ArchiveSearcher.
3
+ # Controller class that manages the archive system. It holds the archive data
4
+ # used by ArchiveViewer and ArchiveSearcher.
5
5
  class ArchiveController
6
6
  include Scraper
7
7
 
@@ -50,15 +50,9 @@ class ArchiveController
50
50
  # get_page(id) => page
51
51
  #
52
52
  # Returns page number based on the ID of the problem. The recent page is
53
- # considered page 0, invalid pages return -1.
53
+ # considered page 0.
54
54
  def get_page(id)
55
- if id.between?(Problem.total - 9, Problem.total)
56
- 0
57
- elsif id.between?(1, Problem.total - 10)
58
- (id - 1) / Page::PROBLEMS_PER_PAGE + 1
59
- else
60
- -1
61
- end
55
+ id.between?(1, Problem.total - 10) ? (id - 1) / Page::LENGTH + 1 : 0
62
56
  end
63
57
 
64
58
  end
@@ -14,18 +14,21 @@ class ArchiveViewer
14
14
 
15
15
  puts
16
16
 
17
- (Problem.total).downto(Problem.total - 9) { |i| puts "#{i} - #{@problems[i].title}" }
17
+ (Problem.total).downto(Problem.total - 9) do |i|
18
+ puts "#{i} - #{@problems[i].title}"
19
+ end
18
20
  end
19
21
 
20
- # Displays the problem numbers and titles for an individual page of the archive.
22
+ # Displays the problem numbers and titles for an individual page of the
23
+ # archive.
21
24
  def display_page(page)
22
25
  load_page(page, @problems)
23
26
 
24
27
  puts
25
28
 
26
- start = (page - 1) * Page::PROBLEMS_PER_PAGE + 1
27
- for i in start...start + Page::PROBLEMS_PER_PAGE
28
- puts "#{i} - #{@problems[i].title}" unless i >= Problem.total - 9
29
+ i = (page - 1) * Page::LENGTH
30
+ Page::LENGTH.times do
31
+ puts "#{i += 1} - #{@problems[i].title}" unless i >= Problem.total - 9
29
32
  end
30
33
  end
31
34
 
@@ -1,7 +1,7 @@
1
1
  module ProjectEulerCli #:nodoc:
2
2
 
3
- # Manages the command line interface for the program. It uses the ArchiveController
4
- # to access the site data.
3
+ # Manages the command line interface for the program. It uses the
4
+ # ArchiveController to access the site data.
5
5
  class CLI
6
6
 
7
7
  def initialize
@@ -76,7 +76,10 @@ class CLI
76
76
 
77
77
  input = prompt
78
78
 
79
- if input.to_i.between?(Page::PROBLEMS_PER_PAGE * (page - 1) + 1, Page::PROBLEMS_PER_PAGE * page)
79
+ first_problem = Page::LENGTH * (page - 1) + 1
80
+ last_problem = Page::LENGTH * page
81
+
82
+ if input.to_i.between?(first_problem, last_problem)
80
83
  problem_menu(input.to_i)
81
84
  elsif input == 'n'
82
85
  page_menu(page + 1)
@@ -113,6 +116,14 @@ class CLI
113
116
  end
114
117
  end
115
118
 
119
+ def search_menu
120
+ print "search: "
121
+
122
+ search_terms = gets.strip
123
+ @ac.search(search_terms)
124
+ search_results_menu
125
+ end
126
+
116
127
  def search_results_menu
117
128
  @ac.display_results
118
129
 
@@ -133,14 +144,6 @@ class CLI
133
144
  end
134
145
  end
135
146
 
136
- def search_menu
137
- print "search: "
138
-
139
- search_terms = gets.strip
140
- @ac.search(search_terms)
141
- search_results_menu
142
- end
143
-
144
147
  end
145
148
 
146
149
  end
@@ -2,22 +2,30 @@ module ProjectEulerCli
2
2
 
3
3
  module Scraper
4
4
 
5
- # Pulls information from the recent page to determine the total number of problems
6
- # and pages.
5
+ # Pulls information from the recent page to determine the total number of
6
+ # problems and pages.
7
7
  def lookup_totals
8
- html = open("https://projecteuler.net/recent")
9
- fragment = Nokogiri::HTML(html)
10
-
11
- id_col = fragment.css('#problems_table td.id_column')
12
-
13
- # The newest problem is the first one listed on the recent page. The ID of this
14
- # problem will always equal the total number of problems.
15
- Problem.total = id_col.first.text.to_i
16
- # There are ten problems on the recent page, so the last archive problem can be
17
- # found by subtracting 10 from the total number of problems. This is used to
18
- # calculate the total number of pages.
19
- last_archive_id = Problem.total - 10
20
- Page.total = (last_archive_id - 1) / Page::PROBLEMS_PER_PAGE + 1
8
+ begin
9
+ Timeout.timeout(4) do
10
+ html = open("https://projecteuler.net/recent")
11
+ fragment = Nokogiri::HTML(html)
12
+
13
+ id_col = fragment.css('#problems_table td.id_column')
14
+
15
+ # The newest problem is the first one listed on the recent page. The ID
16
+ # of this problem will always equal the total number of problems.
17
+ Problem.total = id_col.first.text.to_i
18
+ # There are ten problems on the recent page, so the last archive problem
19
+ # can be found by subtracting 10 from the total number of problems. This
20
+ # is used to calculate the total number of pages.
21
+ last_archive_id = Problem.total - 10
22
+ Page.total = (last_archive_id - 1) / Page::LENGTH + 1
23
+ end
24
+ rescue Timeout::Error
25
+ puts "Project Euler is not responding."
26
+
27
+ exit(true)
28
+ end
21
29
  end
22
30
 
23
31
  # Loads in all of the problem numbers and titles from the recent page.
@@ -47,7 +55,7 @@ module Scraper
47
55
 
48
56
  problem_links = fragment.css('#problems_table td a')
49
57
 
50
- i = (page - 1) * Page::PROBLEMS_PER_PAGE + 1
58
+ i = (page - 1) * Page::LENGTH + 1
51
59
  problem_links.each do |link|
52
60
  problems[i].title = link.text
53
61
  i += 1
@@ -2,7 +2,7 @@ module ProjectEulerCli
2
2
 
3
3
  class Page
4
4
 
5
- PROBLEMS_PER_PAGE = 50
5
+ LENGTH = 50
6
6
 
7
7
  @@total = 0
8
8
  @@visited = []
@@ -1,3 +1,3 @@
1
1
  module ProjectEulerCli
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: project_euler_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ecssiah
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-20 00:00:00.000000000 Z
11
+ date: 2018-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler