Module: ScraperUtils::CycleUtils

Defined in:
lib/scraper_utils/cycle_utils.rb

Overview

Provides utilities for cycling through a range of options day by day

Class Method Summary collapse

Class Method Details

.pick(values, date: nil) ⇒ Object

Returns one value per day, cycling through all possible values in order Can override using CYCLE_POSITION ENV variable

Parameters:

  • values (Array)

    Values to cycle through

  • date (Date, nil) (defaults to: nil)

    Optional date to use instead of today to calculate position

Returns:

  • value from array



21
22
23
24
# File 'lib/scraper_utils/cycle_utils.rb', line 21

def self.pick(values, date: nil)
  values = values.to_a
  values[position(values.size, date: date)]
end

.position(cycle, date: nil) ⇒ Integer

Returns position in cycle from zero onwards Can override using CYCLE_POSITION ENV variable

Parameters:

  • cycle (Integer)

    Cycle size (2 onwards)

  • date (Date, nil) (defaults to: nil)

    Optional date to use instead of today

Returns:

  • (Integer)

    position in cycle progressing from zero to cycle-1 and then repeating day by day



11
12
13
14
# File 'lib/scraper_utils/cycle_utils.rb', line 11

def self.position(cycle, date: nil)
  day = ENV.fetch('CYCLE_POSITION', (date || Date.today).jd).to_i
  day % cycle
end