business-hours 0.0.4 → 0.0.6
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.
- data/Gemfile +5 -0
- data/business-hours.gemspec +3 -3
- data/lib/business-hours.rb +23 -8
- data/lib/business-hours/version.rb +1 -1
- data/spec/business_hours_spec.rb +91 -0
- data/spec/spec_helper.rb +15 -0
- metadata +20 -15
    
        data/Gemfile
    CHANGED
    
    
    
        data/business-hours.gemspec
    CHANGED
    
    | @@ -6,7 +6,7 @@ Gem::Specification.new do |s| | |
| 6 6 | 
             
              s.name        = "business-hours"
         | 
| 7 7 | 
             
              s.version     = BusinessHours::VERSION
         | 
| 8 8 | 
             
              s.authors     = ["Scenetap"]
         | 
| 9 | 
            -
              s.email       = ["shea@scenetap.com"]
         | 
| 9 | 
            +
              s.email       = ["shea@scenetap.com","ethan@scenetap.com"]
         | 
| 10 10 | 
             
              s.homepage    = "https://github.com/scenetap/business-hours"
         | 
| 11 11 | 
             
              s.summary     = %q{Defines business hours for bars}
         | 
| 12 12 | 
             
              s.description = %q{Defines close and open time for business which are open later then 12am}
         | 
| @@ -20,7 +20,7 @@ Gem::Specification.new do |s| | |
| 20 20 |  | 
| 21 21 | 
             
              # specify any dependencies here; for example:
         | 
| 22 22 | 
             
              s.add_development_dependency "rspec"
         | 
| 23 | 
            -
              s.add_runtime_dependency "chronic"
         | 
| 24 23 | 
             
              s.add_runtime_dependency "tzinfo"
         | 
| 25 | 
            -
              s.add_runtime_dependency " | 
| 24 | 
            +
              s.add_runtime_dependency "i18n"
         | 
| 25 | 
            +
              s.add_runtime_dependency "activesupport"
         | 
| 26 26 | 
             
            end
         | 
    
        data/lib/business-hours.rb
    CHANGED
    
    | @@ -1,9 +1,11 @@ | |
| 1 | 
            +
            require 'active_support/core_ext/date/zones'
         | 
| 1 2 | 
             
            require 'active_support/core_ext/time/zones'
         | 
| 3 | 
            +
            require 'active_support/core_ext/date/calculations'
         | 
| 2 4 | 
             
            require 'active_support/core_ext/time/calculations'
         | 
| 5 | 
            +
            require 'active_support/time_with_zone'
         | 
| 3 6 | 
             
            require "business-hours/version"
         | 
| 4 7 |  | 
| 5 8 | 
             
            class BusinessHours
         | 
| 6 | 
            -
              require 'chronic'
         | 
| 7 9 |  | 
| 8 10 | 
             
              attr_accessor :times, :time_zone, :business_date, :business_day_start
         | 
| 9 11 | 
             
              DAYS = %w(sunday monday tuesday wednesday thursday friday saturday)
         | 
| @@ -45,13 +47,25 @@ class BusinessHours | |
| 45 47 | 
             
                end
         | 
| 46 48 |  | 
| 47 49 | 
             
                open_day = date
         | 
| 48 | 
            -
                
         | 
| 49 | 
            -
                 | 
| 50 | 
            +
                open_time = nil
         | 
| 51 | 
            +
                close_time = nil
         | 
| 52 | 
            +
                if times
         | 
| 53 | 
            +
                  open_time = parse("#{open_day} #{times[0]}").utc if times[0].present?
         | 
| 54 | 
            +
                  close_time = parse("#{close_day} #{times[1]}").utc if times[1].present?
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
                [open_time, close_time]
         | 
| 50 57 | 
             
              end
         | 
| 51 58 |  | 
| 52 59 | 
             
              def open_and_close_times(time)
         | 
| 53 60 | 
             
                for_day((time.in_time_zone(time_zone) - business_day_start * 60 * 60).to_date)
         | 
| 54 61 | 
             
              end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
              def business_day
         | 
| 64 | 
            +
                Time.zone = time_zone
         | 
| 65 | 
            +
                @start_time = Time.zone.parse("#{business_date.to_s} #{business_day_start}:00")
         | 
| 66 | 
            +
                @end_time = Time.zone.parse("#{(business_date+1).to_s} #{business_day_start}:00")
         | 
| 67 | 
            +
                [@start_time, @end_time]
         | 
| 68 | 
            +
              end
         | 
| 55 69 |  | 
| 56 70 | 
             
              def self.business_date(options = {})
         | 
| 57 71 | 
             
                business_day_start = options[:business_day_start] || 6
         | 
| @@ -68,10 +82,12 @@ class BusinessHours | |
| 68 82 |  | 
| 69 83 | 
             
                def open_past_midnight(day, times)
         | 
| 70 84 | 
             
                  day = get_day(day)
         | 
| 71 | 
            -
                   | 
| 72 | 
            -
             | 
| 85 | 
            +
                  if times && times[0].present? && times[1].present?
         | 
| 86 | 
            +
                    open = parse("#{day} #{times[0]}")
         | 
| 87 | 
            +
                    close = parse("#{day} #{times[1]}")
         | 
| 73 88 |  | 
| 74 | 
            -
             | 
| 89 | 
            +
                    open > close
         | 
| 90 | 
            +
                  end
         | 
| 75 91 | 
             
                end
         | 
| 76 92 |  | 
| 77 93 | 
             
                def get_day(day_int = 0)
         | 
| @@ -79,7 +95,6 @@ class BusinessHours | |
| 79 95 | 
             
                end
         | 
| 80 96 |  | 
| 81 97 | 
             
                def parse(string)
         | 
| 82 | 
            -
                   | 
| 83 | 
            -
                  Chronic.parse(string)
         | 
| 98 | 
            +
                  Time.zone.parse(string)
         | 
| 84 99 | 
             
                end
         | 
| 85 100 | 
             
            end
         | 
| @@ -0,0 +1,91 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe BusinessHours do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              before(:each) do
         | 
| 6 | 
            +
                @business_day_start = 6
         | 
| 7 | 
            +
                @time_zone = "Central Time (US & Canada)"
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              it "is within valid business hours .open? should return true" do
         | 
| 11 | 
            +
                @times = {:sunday => ["6:00am","6:00am"]}
         | 
| 12 | 
            +
                Timecop.freeze(Time.local(2011, 11, 06, 13, 0, 0)) do # Sunday, November 6, 2011 at 1pm
         | 
| 13 | 
            +
                  BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}).open?.should_not be_true
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                @times = {:sunday => ["11:00am","2:00am"]}
         | 
| 17 | 
            +
                Timecop.freeze(Time.local(2011, 11, 06, 13, 0, 0)) do # Sunday, November 6, 2011 at 1pm
         | 
| 18 | 
            +
                  BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}).open?.should be_true
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                @times = {:sunday => ["11:00am","12:00am"]}
         | 
| 22 | 
            +
                Timecop.freeze(Time.local(2011, 11, 06, 13, 0, 0)) do # Sunday, November 6, 2011 at 1pm
         | 
| 23 | 
            +
                  BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}).open?.should be_true
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                @times = {:sunday => ["8:00am","6:00am"]}
         | 
| 27 | 
            +
                Timecop.freeze(Time.local(2011, 11, 06, 13, 0, 0)) do # Sunday, November 6, 2011 at 1pm
         | 
| 28 | 
            +
                  BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}).open?.should be_true
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                @times = {:sunday => ["6:00am","5:59am"]}
         | 
| 32 | 
            +
                Timecop.freeze(Time.local(2011, 11, 06, 13, 0, 0)) do # Sunday, November 6, 2011 at 1pm
         | 
| 33 | 
            +
                  BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}).open?.should be_true
         | 
| 34 | 
            +
                end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                @times = {
         | 
| 37 | 
            +
                  :sunday => ["11:00am","12:59am"], 
         | 
| 38 | 
            +
                  :monday=>["5:00pm", "2:00am"], 
         | 
| 39 | 
            +
                  :tuesday=>["5:00pm", "2:00am"], 
         | 
| 40 | 
            +
                  :wednesday=>["", ""], 
         | 
| 41 | 
            +
                  :thursday=>["5:00pm", "2:00am"], 
         | 
| 42 | 
            +
                  :friday=>["5:00pm", "2:00am"], 
         | 
| 43 | 
            +
                  :saturday=>["11:00am", "3:00am"]
         | 
| 44 | 
            +
                }
         | 
| 45 | 
            +
                Timecop.freeze(Time.local(2012, 03, 04, 13, 0, 0)) do # Sunday, March 4, 2012 at 1pm
         | 
| 46 | 
            +
                  BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}).open?.should be_true
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              it "returns the correct start and end times for the business day" do
         | 
| 51 | 
            +
                Time.zone = @time_zone
         | 
| 52 | 
            +
                Timecop.freeze(Time.local(2011, 11, 05, 13, 0, 0)) do # Sunday, November 5, 2011 at 1pm
         | 
| 53 | 
            +
                  @business_time = BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}) 
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
                @business_time.business_day.collect{|date| date.to_s}.should == ["2011-11-05 06:00:00 -0500","2011-11-06 06:00:00 -0600"]
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                Timecop.freeze(Time.local(2011, 11, 06, 13, 0, 0)) do # Sunday, November 6, 2011 at 1pm
         | 
| 58 | 
            +
                  @business_time = BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => @business_day_start}) 
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
                @business_time.business_day.collect{|date| date.to_s}.should == ["2011-11-06 06:00:00 -0600","2011-11-07 06:00:00 -0600"]
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                Timecop.freeze(Time.local(2012, 03, 10, 13, 0, 0)) do # Saturday, March 10, 2012 at 1pm
         | 
| 63 | 
            +
                  @business_time = BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => 2}) 
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
                @business_time.business_day.collect{|date| date.to_s}.should == ["2012-03-10 02:00:00 -0600","2012-03-11 03:00:00 -0500"]
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                Timecop.freeze(Time.local(2012, 03, 11, 13, 0, 0)) do # Sunday, March 11, 2012 at 1pm
         | 
| 68 | 
            +
                  @business_time = BusinessHours.new({:times => @times, :time_zone => @time_zone, :business_day_start => 2}) 
         | 
| 69 | 
            +
                end
         | 
| 70 | 
            +
                @business_time.business_day.collect{|date| date.to_s}.should == ["2012-03-11 03:00:00 -0500","2012-03-12 02:00:00 -0500"]
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                Timecop.freeze(Time.local(2012, 03, 11, 13, 0, 0)) do # Sunday, March 11, 2012 at 1pm
         | 
| 73 | 
            +
                  @business_time = BusinessHours.new({:times => @times, :time_zone => "Hawaii", :business_day_start => 2}) 
         | 
| 74 | 
            +
                end
         | 
| 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 | 
            +
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              describe 'for_day' do
         | 
| 79 | 
            +
                it 'should return nil as an element of the return when the provided open_time or close_time was nil or emptystring' do
         | 
| 80 | 
            +
                  business_hours = BusinessHours.new(:times => {:friday => ['',nil], :tuesday => ['9am', '']})
         | 
| 81 | 
            +
                  business_hours.for_day(Date.parse('2012-03-19')).should == [nil,nil] #monday march 19th
         | 
| 82 | 
            +
                  business_hours.for_day(Date.parse('2012-03-20')).should == [Time.parse('2012-03-20 9am'),nil] #tuesday march 20th
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
              end
         | 
| 85 | 
            +
              describe 'open_past_midnight' do
         | 
| 86 | 
            +
                it 'should return nil when times is blank' do
         | 
| 87 | 
            +
                  business_hours = BusinessHours.new
         | 
| 88 | 
            +
                  business_hours.send(:open_past_midnight,1,'').should == nil
         | 
| 89 | 
            +
                end
         | 
| 90 | 
            +
              end
         | 
| 91 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            require 'rubygems'
         | 
| 2 | 
            +
            require 'bundler/setup'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require "tzinfo"
         | 
| 5 | 
            +
            require "i18n"
         | 
| 6 | 
            +
            require 'active_support/core_ext/date/zones'
         | 
| 7 | 
            +
            require 'active_support/core_ext/time/zones'
         | 
| 8 | 
            +
            require 'active_support/core_ext/date/calculations'
         | 
| 9 | 
            +
            require 'active_support/core_ext/time/calculations'
         | 
| 10 | 
            +
            require 'business-hours'
         | 
| 11 | 
            +
            require 'timecop'
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            RSpec.configure do |config|
         | 
| 14 | 
            +
              config.color_enabled = true
         | 
| 15 | 
            +
            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. | 
| 4 | 
            +
              version: 0.0.6
         | 
| 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- | 
| 12 | 
            +
            date: 2012-03-29 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rspec
         | 
| 16 | 
            -
              requirement: & | 
| 16 | 
            +
              requirement: &2164349720 !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: * | 
| 24 | 
            +
              version_requirements: *2164349720
         | 
| 25 25 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 26 | 
            -
              name:  | 
| 27 | 
            -
              requirement: & | 
| 26 | 
            +
              name: tzinfo
         | 
| 27 | 
            +
              requirement: &2164378120 !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: * | 
| 35 | 
            +
              version_requirements: *2164378120
         | 
| 36 36 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 37 | 
            -
              name:  | 
| 38 | 
            -
              requirement: & | 
| 37 | 
            +
              name: i18n
         | 
| 38 | 
            +
              requirement: &2164377100 !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: * | 
| 46 | 
            +
              version_requirements: *2164377100
         | 
| 47 47 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 48 | 
            -
              name:  | 
| 49 | 
            -
              requirement: & | 
| 48 | 
            +
              name: activesupport
         | 
| 49 | 
            +
              requirement: &2164375860 !ruby/object:Gem::Requirement
         | 
| 50 50 | 
             
                none: false
         | 
| 51 51 | 
             
                requirements:
         | 
| 52 52 | 
             
                - - ! '>='
         | 
| @@ -54,10 +54,11 @@ dependencies: | |
| 54 54 | 
             
                    version: '0'
         | 
| 55 55 | 
             
              type: :runtime
         | 
| 56 56 | 
             
              prerelease: false
         | 
| 57 | 
            -
              version_requirements: * | 
| 57 | 
            +
              version_requirements: *2164375860
         | 
| 58 58 | 
             
            description: Defines close and open time for business which are open later then 12am
         | 
| 59 59 | 
             
            email:
         | 
| 60 60 | 
             
            - shea@scenetap.com
         | 
| 61 | 
            +
            - ethan@scenetap.com
         | 
| 61 62 | 
             
            executables: []
         | 
| 62 63 | 
             
            extensions: []
         | 
| 63 64 | 
             
            extra_rdoc_files: []
         | 
| @@ -68,6 +69,8 @@ files: | |
| 68 69 | 
             
            - business-hours.gemspec
         | 
| 69 70 | 
             
            - lib/business-hours.rb
         | 
| 70 71 | 
             
            - lib/business-hours/version.rb
         | 
| 72 | 
            +
            - spec/business_hours_spec.rb
         | 
| 73 | 
            +
            - spec/spec_helper.rb
         | 
| 71 74 | 
             
            homepage: https://github.com/scenetap/business-hours
         | 
| 72 75 | 
             
            licenses: []
         | 
| 73 76 | 
             
            post_install_message: 
         | 
| @@ -88,8 +91,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 88 91 | 
             
                  version: '0'
         | 
| 89 92 | 
             
            requirements: []
         | 
| 90 93 | 
             
            rubyforge_project: business-hours
         | 
| 91 | 
            -
            rubygems_version: 1.8. | 
| 94 | 
            +
            rubygems_version: 1.8.10
         | 
| 92 95 | 
             
            signing_key: 
         | 
| 93 96 | 
             
            specification_version: 3
         | 
| 94 97 | 
             
            summary: Defines business hours for bars
         | 
| 95 | 
            -
            test_files: | 
| 98 | 
            +
            test_files:
         | 
| 99 | 
            +
            - spec/business_hours_spec.rb
         | 
| 100 | 
            +
            - spec/spec_helper.rb
         |