Class: EightBall::Providers::RefreshPolicies::Interval

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(seconds = 60) ⇒ Interval

Creates a new instance of an Interval RefreshPolicy.

Examples:

New data stays fresh for 2 minutes

EightBall::Providers::RefreshPolicies::Interval.new 120

Parameters:

  • seconds (Integer) (defaults to: 60)

    The number of seconds the data is considered fresh.



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

#refreshObject

Yields if the current data is stale, in order to refresh it. Resets the interval once the data is refreshed.

Examples:

Load new data if current data is stale

policy.refresh { load_new_data }


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