Class: LastResort::Scheduler
- Inherits:
-
Object
- Object
- LastResort::Scheduler
- 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
- #expand_if_possible(symbol_or_time_unit) ⇒ Object
- #get_matching_schedule ⇒ Object
-
#initialize(config = Config.new) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #match?(schedule, time_to_match = zone_adjusted_time) ⇒ Boolean
- #match_days?(days, time_to_match) ⇒ Boolean
- #match_hours?(hours, time_to_match) ⇒ Boolean
- #zone_adjusted_time ⇒ Object
Constructor Details
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 (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_schedule ⇒ Object
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
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
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 = [] days.each do |day| += (day) end .include? day_of_week end |
#match_hours?(hours, time_to_match) ⇒ 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) = [] hours.each do |hour| += (hour) end .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_time ⇒ Object
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 |