Class: BusinessHours

Inherits:
Object
  • Object
show all
Defined in:
lib/business-hours.rb,
lib/business-hours/version.rb

Constant Summary collapse

DAYS =
%w(sunday monday tuesday wednesday thursday friday saturday)
VERSION =
"0.0.9"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BusinessHours

Valid options: :times, :time_zone, :business_date, :business_day_start

:times => must be a Hash with an Array of time (example: { :sunday => [Time.now, Time.now + 10.hours] }). Defaults to an empty hash.
:time_zone => must be a time zone string. Defaults to Central Time.
:business_date => must be a Date object. Defaults to the business date determined by the lib.
:business_day_start => the hour the business day flips over. Defaults to app config.


18
19
20
21
22
23
24
25
# File 'lib/business-hours.rb', line 18

def initialize(options = {})
  @times = options[:times] || {}
  @time_zone = options[:time_zone] || "Central Time (US & Canada)"
  @business_date = options[:business_date] || BusinessHours.business_date(options)
  @business_day_start = options[:business_day_start] || 6
  
  Time.zone = @time_zone
end

Instance Attribute Details

#business_dateObject

Returns the value of attribute business_date.



10
11
12
# File 'lib/business-hours.rb', line 10

def business_date
  @business_date
end

#business_day_startObject

Returns the value of attribute business_day_start.



10
11
12
# File 'lib/business-hours.rb', line 10

def business_day_start
  @business_day_start
end

#time_zoneObject

Returns the value of attribute time_zone.



10
11
12
# File 'lib/business-hours.rb', line 10

def time_zone
  @time_zone
end

#timesObject

Returns the value of attribute times.



10
11
12
# File 'lib/business-hours.rb', line 10

def times
  @times
end

Class Method Details

.business_date(options = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/business-hours.rb', line 78

def self.business_date(options = {})
  business_day_start = options[:business_day_start] || 6
  Time.zone = options[:time_zone] || 'Central Time (US & Canada)'
  current_time = options[:current_time] || Time.zone.now
  business_date = current_time.hour < business_day_start ? Time.zone.today - 1 : Time.zone.today
end

Instance Method Details

#business_day(date = business_date) ⇒ Object



71
72
73
74
75
76
# File 'lib/business-hours.rb', line 71

def business_day(date = business_date)
  Time.zone = time_zone
  @start_time = Time.zone.parse("#{date.to_s} #{business_day_start}:00")
  @end_time = Time.zone.parse("#{(date+1).to_s} #{business_day_start}:00")
  [@start_time, @end_time]
end

#for_day(date) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/business-hours.rb', line 46

def for_day(date)
  raise ArgumentError.new("Not a valid Date object") if date.class != Date
  
  times = times_for_date(date)
  
  if open_past_midnight(date.wday, times)
    close_day = date.tomorrow
  else
    close_day = date
  end
  
  open_day = date
  open_time = nil
  close_time = nil
  if times
    open_time = parse("#{open_day} #{times[0]}").utc if times[0].present?
    close_time = parse("#{close_day} #{times[1]}").utc if times[1].present?
  end
  [open_time, close_time]
end

#for_todayObject



42
43
44
# File 'lib/business-hours.rb', line 42

def for_today
  for_day(@business_date)
end

#open?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/business-hours.rb', line 27

def open?(options = {})
  current_time = options[:current_time] || Time.zone.now
  times = for_today
  !!(times[0] && times[1] && times[0] <= current_time and current_time <= times[1])
end

#open_and_close_times(time) ⇒ Object



67
68
69
# File 'lib/business-hours.rb', line 67

def open_and_close_times(time)
  for_day((time.in_time_zone(time_zone) - business_day_start * 60 * 60).to_date)
end

#open_on_day?(day) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/business-hours.rb', line 37

def open_on_day?(day)
  times = times_for_date(day)
  !!(times && times[0] && times[1])
end

#open_today?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/business-hours.rb', line 33

def open_today?
  open_on_day?((Time.zone.now - @business_day_start * 60 * 60).to_date)
end