Class: LastResort::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/last-resort/scheduler.rb

Overview

The schedule as defined in the ‘schedule.rb’ file.

Constant Summary collapse

ALL_DAYS =
[:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
WEEKDAYS =
[:monday, :tuesday, :wednesday, :thursday, :friday]
WEEKENDS =
[:saturday, :sunday]

Instance Method Summary collapse

Constructor Details

#initialize(config = Config.new) ⇒ Scheduler

Returns a new instance of Scheduler.



9
10
11
# File 'lib/last-resort/scheduler.rb', line 9

def initialize config = Config.new
  @config = config
end

Instance Method Details

#expand_if_possible(symbol_or_time_unit) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/last-resort/scheduler.rb', line 58

def expand_if_possible(symbol_or_time_unit)
  return [symbol_or_time_unit] if
    symbol_or_time_unit.is_a? Fixnum or symbol_or_time_unit.is_a? Range

  case symbol_or_time_unit
  when :all_hours
    [0..23]
  when :off_hours
    [0..8, 17..23]
  when :everyday
    ALL_DAYS
  when :weekdays
    WEEKDAYS
  when :weekends
    WEEKENDS
  when :monday, :tuesday, :wednesday, :thursday, :friday
    [symbol_or_time_unit]
  else
    raise "#{symbol_or_time_unit} is not a recognized expandable symbol"
  end
end

#get_matching_scheduleObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/last-resort/scheduler.rb', line 13

def get_matching_schedule
  matched_schedule = @config.schedules.find { |schedule| match?(schedule) }

  if matched_schedule.nil?
    puts "No matched schedule"
    nil
  else
    puts "Schedule found -- calling #{matched_schedule[:contacts]}"
    matched_schedule
  end
end

#match?(schedule, time_to_match = zone_adjusted_time) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/last-resort/scheduler.rb', line 29

def match?(schedule, time_to_match = zone_adjusted_time)
  match_hours?(schedule[:hours], time_to_match) && match_days?(schedule[:days], time_to_match)
end

#match_days?(days, time_to_match) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/last-resort/scheduler.rb', line 48

def match_days?(days, time_to_match)
  day_of_week = time_to_match.strftime("%A").downcase.to_sym
  expanded_days = []
  days.each do |day|
    expanded_days += expand_if_possible(day)
  end

  expanded_days.include? day_of_week
end

#match_hours?(hours, time_to_match) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/last-resort/scheduler.rb', line 33

def match_hours?(hours, time_to_match)
  expanded_hours = []
  hours.each do |hour|
    expanded_hours += expand_if_possible(hour)
  end

  expanded_hours.any? do |hour|
    if hour.is_a? Range
      hour.include? time_to_match.hour
    elsif hour.is_a? Fixnum
      hour == time_to_match.hour
    end
  end
end

#zone_adjusted_timeObject



25
26
27
# File 'lib/last-resort/scheduler.rb', line 25

def zone_adjusted_time
  Time.now.utc + @config.local_utc_offset_in_seconds
end