Skip to content

Commit

Permalink
[modify] able to specify "expires_in" at config/rss.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny4381 committed Aug 7, 2020
1 parent fdacba8 commit 2ee984e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/jobs/rss/import_weather_xml_job.rb
Expand Up @@ -79,7 +79,9 @@ def after_import
def gc_rss_tempfile
return if rand(100) >= 20

threshold = 2.weeks.ago
expires_in = SS.config.rss.weather_xml["expires_in"].try { |threshold| SS::Duration.parse(threshold) rescue nil }
expires_in ||= 2.weeks
threshold = Time.zone.now - expires_in
Rss::TempFile.with_repl_master.lt(updated: threshold).destroy_all
remove_old_cache(threshold)
end
Expand Down
2 changes: 2 additions & 0 deletions config/defaults/rss.yml
Expand Up @@ -11,6 +11,7 @@ production: &production
# 高頻度フィード その他
- http://www.data.jma.go.jp/developer/xml/feed/other.xml
data_cache_dir: tmp/cache/weather_xml
expires_in: 2.weeks

development:
<<: *production
Expand All @@ -21,3 +22,4 @@ test:
urls:
- http://weather.example.jp/developer/xml/feed/other.xml
data_cache_dir: tmp/cache/test_weather_xml
expires_in: 1.day
26 changes: 26 additions & 0 deletions lib/ss/duration.rb
@@ -0,0 +1,26 @@
class SS::Duration
def self.parse(name)
num, unit = name.to_s.split('.', 2)
raise "malformed duration: #{name}" if !num.numeric?

num = num.to_i
case unit.singularize
when "year"
num.years
when "month"
num.months
when "week"
num.weeks
when "day"
num.days
when "hour"
num.hours
when "minute"
num.minutes
when "second"
num.seconds
else
raise "malformed duration: #{name}"
end
end
end
34 changes: 34 additions & 0 deletions spec/lib/ss/duration_spec.rb
@@ -0,0 +1,34 @@
require 'spec_helper'

describe SS::Duration do
describe ".parse" do
context "with valid durations" do
it do
expect(SS::Duration.parse("1.year")).to eq 1.year
expect(SS::Duration.parse("2.years")).to eq 2.years
expect(SS::Duration.parse("1.month")).to eq 1.month
expect(SS::Duration.parse("2.months")).to eq 2.months
expect(SS::Duration.parse("1.day")).to eq 1.day
expect(SS::Duration.parse("2.days")).to eq 2.days
expect(SS::Duration.parse("1.hour")).to eq 1.hour
expect(SS::Duration.parse("2.hours")).to eq 2.hours
expect(SS::Duration.parse("1.minute")).to eq 1.minute
expect(SS::Duration.parse("2.minutes")).to eq 2.minutes
expect(SS::Duration.parse("1.second")).to eq 1.second
expect(SS::Duration.parse("2.seconds")).to eq 2.seconds
end
end

context "with invalid unit" do
it do
expect { SS::Duration.parse("1.decade") }.to raise_error RuntimeError, "malformed duration: 1.decade"
end
end

context "with non-number" do
it do
expect { SS::Duration.parse("hello.days") }.to raise_error RuntimeError, "malformed duration: hello.days"
end
end
end
end

0 comments on commit 2ee984e

Please sign in to comment.