summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrian m. carlson <sandals@crustytoothpaste.net>2017-04-05 02:58:58 +0000
committerbrian m. carlson <sandals@crustytoothpaste.net>2017-04-05 02:58:58 +0000
commitaa847c6bbff9410cecdf16f61a9a2a993ed809f7 (patch)
tree68d87b883f230388e2a8ca1d3739d7e7c644c42f
parent25210a1d0a730cdfca1d0279007173250c1a0269 (diff)
comics/data: simplify date stamp parsing
Simplify the date stamp parsing. Add a spec to ensure that the behavior before and after the change is the same. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
-rw-r--r--lib/comics/data.rb15
-rw-r--r--spec/comic_spec.rb51
2 files changed, 58 insertions, 8 deletions
diff --git a/lib/comics/data.rb b/lib/comics/data.rb
index cad3b42..61bb91b 100644
--- a/lib/comics/data.rb
+++ b/lib/comics/data.rb
@@ -115,14 +115,13 @@ module Comics
def latest
return @latest if @latest
- now = Time.new.gmtime
- if @updatetime
- @latest = Time.parse(updatetime, now)
- @latest -= 86_400 if @latest > now
- else
- @latest = Time.utc(now.year, now.month, now.day)
- end
- @latest
+ now = Time.now.gmtime
+ @latest = if @updatetime
+ parsed = Time.parse(updatetime, now)
+ parsed > now ? parsed - 86_400 : parsed
+ else
+ Time.utc(now.year, now.month, now.day)
+ end
end
end
diff --git a/spec/comic_spec.rb b/spec/comic_spec.rb
new file mode 100644
index 0000000..6e354c0
--- /dev/null
+++ b/spec/comic_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+require 'comics/data'
+require 'stringio'
+
+describe Comics::Comic do
+ let(:config) do
+ Comics::Configuration.new(StringIO.new(<<-EOM, 'r'))
+ {
+ "comics":{
+ "dilbert":{
+ "name":"Dilbert",
+ "comics":{
+ "daily":{
+ "website": "http://www.dilbert.com/strips/comic/",
+ "link": "http://www.dilbert.com/strips/comic/%F/",
+ "type":"link",
+ "frequency":"daily",
+ "time":"01:00"
+ }
+ }
+ }
+ },
+ "config": {
+ "id":"https://example.org/",
+ "css":"",
+ "default":{
+ }
+ }
+ }
+ EOM
+ end
+
+ it 'handles last update times properly' do
+ early = Time.at(1_491_264_000).gmtime
+ late = Time.at(1_491_271_200).gmtime
+ parse = Time.method(:parse)
+ time = class_double('Time').as_stubbed_const
+ [
+ [early, '2017-04-03T01:00:00'],
+ # If the time hasn't happened yet, then use the previous date.
+ [late, '2017-04-04T01:00:00'],
+ ].each do |(stamp, expected)|
+ allow(time).to receive(:now) { stamp }
+ allow(time).to receive(:parse) { |*args| parse.call(*args) }
+ c = config.comic('dilbert')
+ entry = c.first
+ expect(entry.date('%FT%T')).to eq expected
+ end
+ end
+end