Skip to content

Commit a8573cd

Browse files
committed
Implement your own duration logic
This is to avoid Timerizer/AR conflicts between the classes
1 parent 966a222 commit a8573cd

File tree

3 files changed

+93
-7
lines changed

3 files changed

+93
-7
lines changed

html-proofer.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Gem::Specification.new do |gem|
2626
gem.add_dependency 'yell', '~> 2.0'
2727
gem.add_dependency 'parallel', '~> 1.3'
2828
gem.add_dependency 'addressable', '~> 2.3'
29-
gem.add_dependency 'timerizer', '~> 0.3'
3029

3130
gem.add_development_dependency 'redcarpet'
3231
gem.add_development_dependency 'rubocop'

lib/html-proofer/cache.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# frozen_string_literal: true
22

33
require_relative 'utils'
4-
4+
require 'date'
55
require 'json'
6-
require 'timerizer'
76

87
module HTMLProofer
98
class Cache
@@ -46,13 +45,13 @@ def parsed_timeframe(timeframe)
4645
time = time.to_i
4746
case date
4847
when 'M'
49-
time.months.ago
48+
time_ago(time, :months)
5049
when 'w'
51-
time.weeks.ago
50+
time_ago(time, :weeks)
5251
when 'd'
53-
time.days.ago
52+
time_ago(time, :days)
5453
when 'h'
55-
time.hours.ago
54+
time_ago(time, :hours)
5655
else
5756
raise ArgumentError, "#{date} is not a valid timeframe!"
5857
end
@@ -162,5 +161,21 @@ def setup_cache!(options)
162161
contents = File.read(cache_file)
163162
@cache_log = contents.empty? ? {} : JSON.parse(contents)
164163
end
164+
165+
private
166+
167+
def time_ago(measurement, unit)
168+
d = DateTime.now
169+
case unit
170+
when :months
171+
d >> -measurement
172+
when :weeks
173+
d - measurement * 7
174+
when :days
175+
d - measurement
176+
when :hours
177+
d - Rational(72/24.0)
178+
end.to_time
179+
end
165180
end
166181
end

spec/html-proofer/cache_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,82 @@
99

1010
let(:default_cache_options) { { storage_dir: storage_dir } }
1111

12+
let (:logger) { HTMLProofer::Log.new(:debug) }
13+
1214
def read_cache(cache_file)
1315
JSON.parse File.read(cache_file)
1416
end
1517

18+
context 'with time parser' do
19+
it 'understands months' do
20+
now_time = Time.local(2019, 9, 6, 12, 0, 0)
21+
Timecop.freeze(now_time)
22+
23+
cache = HTMLProofer::Cache.new(logger, { timeframe: '2M' })
24+
25+
check_time = Time.local(2019, 8, 6, 12, 0, 0).to_s
26+
27+
expect(cache.within_timeframe?(check_time)).to be true
28+
29+
check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s
30+
31+
expect(cache.within_timeframe?(check_time)).to be false
32+
33+
Timecop.return
34+
end
35+
36+
it 'understands days' do
37+
now_time = Time.local(2019, 9, 6, 12, 0, 0)
38+
Timecop.freeze(now_time)
39+
40+
cache = HTMLProofer::Cache.new(logger, { timeframe: '2d' })
41+
42+
check_time = Time.local(2019, 9, 5, 12, 0, 0).to_s
43+
44+
expect(cache.within_timeframe?(check_time)).to be true
45+
46+
check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s
47+
48+
expect(cache.within_timeframe?(check_time)).to be false
49+
50+
Timecop.return
51+
end
52+
53+
it 'understands weeks' do
54+
now_time = Time.local(2019, 9, 6, 12, 0, 0)
55+
Timecop.freeze(now_time)
56+
57+
cache = HTMLProofer::Cache.new(logger, { timeframe: '2w' })
58+
59+
check_time = Time.local(2019, 8, 30, 12, 0, 0).to_s
60+
61+
expect(cache.within_timeframe?(check_time)).to be true
62+
63+
check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s
64+
65+
expect(cache.within_timeframe?(check_time)).to be false
66+
67+
Timecop.return
68+
end
69+
70+
it 'understands hours' do
71+
now_time = Time.local(2019, 9, 6, 12, 0, 0)
72+
Timecop.freeze(now_time)
73+
74+
cache = HTMLProofer::Cache.new(logger, { timeframe: '3h' })
75+
76+
check_time = Time.local(2019, 9, 6, 9, 0, 0).to_s
77+
78+
expect(cache.within_timeframe?(check_time)).to be true
79+
80+
check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s
81+
82+
expect(cache.within_timeframe?(check_time)).to be false
83+
84+
Timecop.return
85+
end
86+
end
87+
1688
context 'with .htmlproofer.log' do
1789
let(:cache_file_name) { '.htmlproofer.log' }
1890

0 commit comments

Comments
 (0)