business-hours 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in business-hours.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "business-hours/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "business-hours"
7
+ s.version = BusinessHours::VERSION
8
+ s.authors = ["Scenetap"]
9
+ s.email = ["shea@scenetap.com"]
10
+ s.homepage = "https://github.com/scenetap/business-hours"
11
+ s.summary = %q{Defines business hours for bars}
12
+ s.description = %q{Defines close and open time for business which are open later then 12am}
13
+
14
+ s.rubyforge_project = "business-hours"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "chronic"
24
+ end
@@ -0,0 +1,79 @@
1
+ require "business-hours/version"
2
+
3
+ class BusinessHours
4
+ require 'chronic'
5
+
6
+ attr_accessor :times, :time_zone, :business_date, :business_day_start
7
+ DAYS = %w(sunday monday tuesday wednesday thursday friday saturday)
8
+
9
+ # Valid options: :times, :time_zone, :business_date, :business_day_start
10
+ # :times => must be a Hash with an Array of time (example: { :sunday => [Time.now, Time.now + 10.hours] }). Defaults to an empty hash.
11
+ # :time_zone => must be a time zone string. Defaults to Central Time.
12
+ # :business_date => must be a Date object. Defaults to the business date determined by the lib.
13
+ # :business_day_start => the hour the business day flips over. Defaults to app config.
14
+ def initialize(options = {})
15
+ @times = options[:times] || {}
16
+ @time_zone = options[:time_zone] || "Central Time (US & Canada)"
17
+ @business_date = options[:business_date] || BusinessHours.business_date(options)
18
+ @business_day_start = options[:business_day_start] || 6
19
+
20
+ Time.zone = @time_zone
21
+ end
22
+
23
+ def open?(options = {})
24
+ current_time = options[:current_time] || Time.zone.now
25
+ times = for_today
26
+
27
+ times[0] <= current_time and current_time <= times[1]
28
+ end
29
+
30
+ def for_today
31
+ for_day(@business_date)
32
+ end
33
+
34
+ def for_day(date)
35
+ raise ArgumentError.new("Not a valid Date object") if date.class != Date
36
+
37
+ times = times_for_date(date)
38
+
39
+ if open_past_midnight(date.wday, times)
40
+ close_day = date.tomorrow
41
+ else
42
+ close_day = date
43
+ end
44
+
45
+ open_day = date
46
+
47
+ [parse("#{open_day} #{times[0]}"), parse("#{close_day} #{times[1]}")]
48
+ end
49
+
50
+ def self.business_date(options = {})
51
+ business_day_start = options[:business_day_start] || 6
52
+ Time.zone = options[:time_zone] || @time_zone
53
+ current_time = options[:current_time] || Time.zone.now
54
+
55
+ business_date = current_time.hour < business_day_start ? Time.zone.today - 1 : Time.zone.today
56
+ end
57
+
58
+ private
59
+ def times_for_date(date)
60
+ @times[get_day(date.wday).to_sym]
61
+ end
62
+
63
+ def open_past_midnight(day, times)
64
+ day = get_day(day)
65
+ open = parse("#{day} #{times[0]}")
66
+ close = parse("#{day} #{times[1]}")
67
+
68
+ open > close
69
+ end
70
+
71
+ def get_day(day_int = 0)
72
+ DAYS[day_int]
73
+ end
74
+
75
+ def parse(string)
76
+ Chronic.time_class = Time.zone
77
+ Chronic.parse(string)
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ class BusinessHours
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: business-hours
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scenetap
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70309850277200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70309850277200
25
+ - !ruby/object:Gem::Dependency
26
+ name: chronic
27
+ requirement: &70309850276780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70309850276780
36
+ description: Defines close and open time for business which are open later then 12am
37
+ email:
38
+ - shea@scenetap.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - Rakefile
46
+ - business-hours.gemspec
47
+ - lib/business-hours.rb
48
+ - lib/business-hours/version.rb
49
+ homepage: https://github.com/scenetap/business-hours
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project: business-hours
69
+ rubygems_version: 1.8.6
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Defines business hours for bars
73
+ test_files: []