Created
May 12, 2011 20:22
-
-
Save chrishomer/969361 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListingWindow | |
SKIP_N_PERIODS = { | |
"exclusive" => 1, | |
"pickable" => 2 | |
} | |
WINDOW_TIMES = [9,18] | |
LISTING_TIME_ZONE = "Pacific Time (US & Canada)" | |
#LISTING_TIME_ZONE = "UTC" | |
def self.time_now | |
#Time.parse("April 5 2011 18:01 PDT") | |
Time.now.in_time_zone(LISTING_TIME_ZONE) | |
end | |
# takes hour as a 24 hour clock | |
def self.next_window(time = nil) | |
time ||= time_now | |
time = time.in_time_zone(LISTING_TIME_ZONE) | |
# Calculate next window based on time supplied | |
# => 1. Map Window Times array from hours to actual time values based on the day supplied | |
# => 2. Select the time that is greater than time supplied and take the first occurance (=next window) | |
windows = WINDOW_TIMES.map{|h| Time.new(time.year,time.month,time.day, h, 0, 0, time.utc_offset) } | |
window = windows.select{|t| t > time }.first | |
# => 3. If past the final window value (nil), default to the first window of the following day | |
time = time + 1.day | |
window ||= Time.new(time.year,time.month,time.day, WINDOW_TIMES[0], 0, 0, time.utc_offset) | |
window | |
end | |
def self.next_state_window(state,first_staged_at = nil) | |
n = SKIP_N_PERIODS[state.to_s] | |
window = next_window(first_staged_at) | |
n.times do |i| | |
window = next_window(window) | |
end | |
window | |
end | |
def self.next_exclusive_window(first_staged_at = nil) | |
next_state_window("exclusive",first_staged_at) | |
end | |
def self.next_pickable_window(first_staged_at = nil) | |
next_state_window("pickable",first_staged_at) | |
end | |
end | |
# | |
# | |
# def self.next_window(time = nil) | |
# if time | |
# time = time.in_time_zone(LISTING_TIME_ZONE) | |
# else | |
# time = time_now | |
# end | |
# | |
# | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment