0.01
No commit activity in last 3 years
No release in over 3 years
A gem for checking whether a given DateTime, Date or Time is within a predefined set of opening hours
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 2.14.0

Runtime

 Project Readme

in_business

Working with business opening times is pretty hard, or at least, a lot of effort to write for each application.

We have awesome gems like business_time which help, but there's a gap in the market for something that knows your hours, and can quickly tell you whether a particular time is open or closed concretely. As fun as crazy time calculations are!

I've been doing quite a lot of work recently building Twilio apps (such as my startup employer GoCardless's Nodephone, amongst others) where you need to work out if an incoming call is during office hours or not.

This gem allows you to do that kind of thing with simple .open? and .closed? methods, and even supports holidays!

in_business depends on Rails's ActiveSupport and is awesome combined with the holidays gem.

Usage

InBusiness.open? # => nil (since we've not set any hours yet!)

# We want to be open 9am til 6pm on a Monday
InBusiness.hours.monday = "09:00".."18:00"
InBusiness.open? # Returns true if it's Monday between 9am and 6pm, false otherwise
InBusiness.open? DateTime.parse('10am Monday') # => true
InBusiness.closed? DateTime.parse('9pm Monday') # => true

# In our imaginary land, 8th July 2013 is a Monday but a public holiday, so let's add it...
InBusiness.holidays << Date.parse('8th July 2013')
InBusiness.hours.monday # => "09:00".."18:00"
InBusiness.open? DateTime.parse("8th July 2013 12:00") # => false
InBusiness.is_holiday? DateTime.parse("8th July 2013 12:00") # => true

Installation

Add this line to your application's Gemfile:

gem 'in_business'

And then execute:

$ bundle

Or install it yourself as:

$ gem install in_business

Using with Rails

Make sure that the gem is in your Gemfile and that you've bundle install -ed, then create an initializer, for example config/initializers/in_business.rb.

In there, you'll want to set your daily hours and any holidays:

InBusiness.hours = {
  monday: "09:00".."18:00",
  tuesday: "10:00".."19:00",
  # ...
  saturday: "09:00".."12:00"
}

InBusiness.holidays << Date.parse("25th December 2013")

Using with the holidays gem

Just do something like this, perhaps in your Rails initializer:

Holidays.between(Date.civil(2013, 1, 1), 2.years.from_now, :gb).
  map{|holiday| InBusiness.holidays << holiday[:date]}

This little technique is inspired by business_time's readme!

Running specs

$ bundle
$ rspec spec

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request