Class: EightBall::Providers::RefreshPolicies::Interval
- Inherits:
-
Object
- Object
- EightBall::Providers::RefreshPolicies::Interval
- Defined in:
- lib/eight_ball/providers/refresh_policies/interval.rb
Overview
An Interval RefreshPolicy states that data is considered fresh for a certain amount of time, after which it is considered stale and should be refreshed.
Constant Summary collapse
- SECONDS_IN_A_DAY =
86_400
Instance Method Summary collapse
-
#initialize(seconds = 60) ⇒ Interval
constructor
Creates a new instance of an Interval RefreshPolicy.
-
#refresh ⇒ Object
Yields if the current data is stale, in order to refresh it.
Constructor Details
#initialize(seconds = 60) ⇒ Interval
Creates a new instance of an Interval RefreshPolicy.
17 18 19 20 |
# File 'lib/eight_ball/providers/refresh_policies/interval.rb', line 17 def initialize(seconds = 60) @interval = seconds @fresh_until = nil end |
Instance Method Details
#refresh ⇒ Object
Yields if the current data is stale, in order to refresh it. Resets the interval once the data is refreshed.
27 28 29 30 31 32 |
# File 'lib/eight_ball/providers/refresh_policies/interval.rb', line 27 def refresh return unless block_given? && stale? yield @fresh_until = DateTime.now + Rational(@interval, SECONDS_IN_A_DAY) end |