business-hours 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  class BusinessHours
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -31,6 +31,15 @@ class BusinessHours
31
31
  times[0] <= current_time and current_time <= times[1]
32
32
  end
33
33
  end
34
+
35
+ def open_today?
36
+ open_on_day?((Time.zone.now - @business_day_start * 60 * 60).to_date)
37
+ end
38
+
39
+ def open_on_day?(day)
40
+ times = times_for_date(day)
41
+ !!(times && times[0] && times[1])
42
+ end
34
43
 
35
44
  def for_today
36
45
  for_day(@business_date)
@@ -61,10 +70,10 @@ class BusinessHours
61
70
  for_day((time.in_time_zone(time_zone) - business_day_start * 60 * 60).to_date)
62
71
  end
63
72
 
64
- def business_day
73
+ def business_day(date = business_date)
65
74
  Time.zone = time_zone
66
- @start_time = Time.zone.parse("#{business_date.to_s} #{business_day_start}:00")
67
- @end_time = Time.zone.parse("#{(business_date+1).to_s} #{business_day_start}:00")
75
+ @start_time = Time.zone.parse("#{date.to_s} #{business_day_start}:00")
76
+ @end_time = Time.zone.parse("#{(date+1).to_s} #{business_day_start}:00")
68
77
  [@start_time, @end_time]
69
78
  end
70
79
 
@@ -72,7 +81,6 @@ class BusinessHours
72
81
  business_day_start = options[:business_day_start] || 6
73
82
  Time.zone = options[:time_zone] || 'Central Time (US & Canada)'
74
83
  current_time = options[:current_time] || Time.zone.now
75
-
76
84
  business_date = current_time.hour < business_day_start ? Time.zone.today - 1 : Time.zone.today
77
85
  end
78
86
 
@@ -73,6 +73,13 @@ describe BusinessHours do
73
73
  @business_time = BusinessHours.new({:times => @times, :time_zone => "Hawaii", :business_day_start => 2})
74
74
  end
75
75
  @business_time.business_day.collect{|date| date.to_s}.should == ["2012-03-11 03:00:00 -1000","2012-03-12 02:00:00 -1000"]
76
+
77
+ business_hours = BusinessHours.new(:times => @times, :business_day_start => 1)
78
+ business_hours.business_day(Date.parse('2012-04-05')).should == [Time.parse('2012-04-05 1:00:00 -0500'), Time.parse('2012-04-06 1:00:00 -0500')] # thursday 1:30 am
79
+
80
+ business_hours = BusinessHours.new(:times => @times, :business_day_start => 2, :time_zone => 'Hawaii')
81
+ business_hours.business_day(Date.parse('2012-04-07')).should == [Time.parse('2012-04-07 2:00:00 -1000'), Time.parse('2012-04-08 2:00:00 -1000')] # thursday 1:30 am
82
+
76
83
  end
77
84
 
78
85
  describe 'for_day' do
@@ -100,4 +107,62 @@ describe BusinessHours do
100
107
  end
101
108
  end
102
109
  end
110
+ describe 'business_day' do
111
+ it 'it should return the business day for today by default' do
112
+ Timecop.freeze(Time.parse('2012-04-01 10:00:00 -0500')) do # april 1st, 10am
113
+ business_hours = BusinessHours.new
114
+ business_hours.business_day.should == [Time.parse('2012-04-01 06:00:00 -0500'), Time.parse('2012-04-02 06:00:00 -0500')]
115
+ end
116
+ end
117
+ it 'should return the business_day of the business_date specified' do
118
+ business_hours = BusinessHours.new
119
+ business_hours.business_day(Date.parse('2012-04-10')).should == [Time.parse('2012-04-10 06:00:00 -0500'), Time.parse('2012-04-11 06:00:00 -0500')]
120
+ end
121
+ end
122
+ describe 'open_on_day?' do
123
+ it 'should return true when there is an open and a close time for today' do
124
+ business_hours = BusinessHours.new(:times => {:sunday => ['1pm','2pm']})
125
+ business_hours.open_on_day?(Date.parse('2012-04-01')).should be_true
126
+ end
127
+ it "should return false when either the open time or close time for today is missing" do
128
+ business_hours = BusinessHours.new(:times => {:sunday => ['1pm',nil]})
129
+ business_hours.open_on_day?(Date.parse('2012-04-01')).should be_false
130
+ end
131
+ it 'should return false when there are no times for today' do
132
+ business_hours = BusinessHours.new
133
+ business_hours.open_on_day?(Date.parse('2012-04-01')).should be_false
134
+ end
135
+ end
136
+ describe 'open_today?' do
137
+ it 'should return true when there is an open and a close time for today' do
138
+ Timecop.freeze(Time.parse('2012-04-02 06:00:00 -0500')) do #monday
139
+ business_hours = BusinessHours.new(:times => {:monday => ['1pm','2pm']})
140
+ business_hours.open_today?.should be_true
141
+ end
142
+ end
143
+ it "should return false when either the open time or close time for today is missing" do
144
+ Timecop.freeze(Time.parse('2012-04-02 06:00:00 -0500')) do #monday
145
+ business_hours = BusinessHours.new(:times => {:monday => ['1pm',nil]})
146
+ business_hours.open_today?.should be_false
147
+ end
148
+ end
149
+ it 'should return false when there are no times for today' do
150
+ Timecop.freeze(Time.parse('2012-04-02 06:00:00 -0500')) do #monday
151
+ business_hours = BusinessHours.new
152
+ business_hours.open_today?.should be_false
153
+ end
154
+ end
155
+ it 'should return true even if it is 1am, the next date, but the same business day' do
156
+ Timecop.freeze(Time.parse('2012-04-03 01:00:00 -0500')) do #tuesday, 1am
157
+ business_hours = BusinessHours.new(:times => {:monday => ['2pm', '12:30am']})
158
+ business_hours.open_today?.should be_true
159
+ end
160
+ end
161
+ it 'should return false if given business hours for monday but the current time is 7am the next day' do
162
+ Timecop.freeze(Time.parse('2012-04-03 07:00:00 -0500')) do #tuesday, 1am
163
+ business_hours = BusinessHours.new(:times => {:monday => ['2pm', '12:30am']})
164
+ business_hours.open_today?.should be_false
165
+ end
166
+ end
167
+ end
103
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business-hours
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-29 00:00:00.000000000Z
12
+ date: 2012-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70174715073920 !ruby/object:Gem::Requirement
16
+ requirement: &2161480100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70174715073920
24
+ version_requirements: *2161480100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: tzinfo
27
- requirement: &70174715073500 !ruby/object:Gem::Requirement
27
+ requirement: &2161479340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70174715073500
35
+ version_requirements: *2161479340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: i18n
38
- requirement: &70174715073080 !ruby/object:Gem::Requirement
38
+ requirement: &2161478840 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70174715073080
46
+ version_requirements: *2161478840
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activesupport
49
- requirement: &70174715072660 !ruby/object:Gem::Requirement
49
+ requirement: &2161478340 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70174715072660
57
+ version_requirements: *2161478340
58
58
  description: Defines close and open time for business which are open later then 12am
59
59
  email:
60
60
  - shea@scenetap.com
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project: business-hours
94
- rubygems_version: 1.8.6
94
+ rubygems_version: 1.8.10
95
95
  signing_key:
96
96
  specification_version: 3
97
97
  summary: Defines business hours for bars