Skip to content

Instantly share code, notes, and snippets.

@henrik
Created February 19, 2015 21:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/7f817afcc3c53f665d5d to your computer and use it in GitHub Desktop.
Save henrik/7f817afcc3c53f665d5d to your computer and use it in GitHub Desktop.
class AutodeployPolicy
ALLOWED_PERIODS = [
"08:30:00".."11:59:59",
# No autodeploys during lunch!
"13:00:00".."18:00:00",
]
# On Fridays, people sometimes leave earlier.
# Not the funnest day to fix problems, so let's cut autodeploys earlier.
END_OF_WORKDAY_ON_FRIDAYS = "16:30:00"
def self.allow?(time)
new(time).allow?
end
def initialize(time)
@time = time
end
attr_reader :time
private :time
def allow?
return false if weekend?
return false if late_on_friday?
within_allowed_hours?
end
private
def weekend?
time.saturday? || time.sunday?
end
def late_on_friday?
time.friday? && formatted_time > END_OF_WORKDAY_ON_FRIDAYS
end
def within_allowed_hours?
ALLOWED_PERIODS.any? { |period| period.cover?(formatted_time) }
end
# E.g. "08:30:00" or "18:00:00".
def formatted_time
time.strftime("%H:%M:%S")
end
end
# When run directly (and not just required/loaded).
if __FILE__ == $0
puts AutodeployPolicy.allow?(Time.now)
end
require "spec_helper"
require "ci/support/is_autodeploy_allowed"
describe AutodeployPolicy, ".allow?" do
it "is true during office hours on weekdays" do
expect(AutodeployPolicy.allow?(weekday_at 8, 30, 0)).to be_true
expect(AutodeployPolicy.allow?(weekday_at 18, 0, 0)).to be_true
end
it "is false outside office hours on weekdays" do
expect(AutodeployPolicy.allow?(weekday_at 8, 29, 59)).to be_false
expect(AutodeployPolicy.allow?(weekday_at 18, 0, 1)).to be_false
end
it "is false during lunch on weekdays" do
expect(AutodeployPolicy.allow?(weekday_at 11, 59, 59)).to be_true
expect(AutodeployPolicy.allow?(weekday_at 12, 0, 0)).to be_false
expect(AutodeployPolicy.allow?(weekday_at 12, 59, 59)).to be_false
expect(AutodeployPolicy.allow?(weekday_at 13, 0, 0)).to be_true
end
it "false any time during weekends" do
expect(AutodeployPolicy.allow?(weekend_at 10, 0, 0)).to be_false
end
it "assumes the workday ends earlier on Fridays" do
expect(AutodeployPolicy.allow?(friday_at 16, 30, 0)).to be_true
expect(AutodeployPolicy.allow?(friday_at 16, 30, 1)).to be_false
end
private
def weekday_at(hour, min, sec)
# A Thursday.
Time.mktime(2014, 10, 2, hour, min, sec)
end
def friday_at(hour, min, sec)
Time.mktime(2014, 10, 3, hour, min, sec)
end
def weekend_at(hour, min, sec)
# A Saturday.
Time.mktime(2014, 10, 4, hour, min, sec)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment