module BusinessCalendar
Constants
- CountryNotSupported
- OrganizationNotSupported
- VERSION
Public Class Methods
for(country, options = {})
click to toggle source
# File lib/business_calendar.rb, line 7 def for(country, options = {}) if options["use_cached_calendar"] calendar_cache[country] ||= Calendar.new(holiday_determiner(country)) else Calendar.new(holiday_determiner(country), options) end end
for_endpoint(additions, removals, options = {})
click to toggle source
# File lib/business_calendar.rb, line 19 def for_endpoint(additions, removals, options = {}) Calendar.new(holiday_determiner_for_endpoint(additions, removals, options), options) end
for_organization(org, options = {})
click to toggle source
# File lib/business_calendar.rb, line 15 def for_organization(org, options = {}) Calendar.new(holiday_determiner_for_organization(org), options) end
Private Class Methods
calendar_cache()
click to toggle source
# File lib/business_calendar.rb, line 75 def calendar_cache @calendar_cache ||= {} end
config(country)
click to toggle source
# File lib/business_calendar.rb, line 79 def config(country) @config ||= load_config @config[country.to_s] end
holiday_dates_for_endpoint(client, endpoint)
click to toggle source
# File lib/business_calendar.rb, line 47 def holiday_dates_for_endpoint(client, endpoint) Proc.new { JSON.parse(client.get(endpoint).body).fetch('holidays').map { |s| Date.parse s } } end
holiday_determiner(country)
click to toggle source
# File lib/business_calendar.rb, line 25 def holiday_determiner(country) cfg = config(country) or raise CountryNotSupported.new(country.inspect) @holiday_procs ||= {} @holiday_procs[country] ||= HolidayDeterminer.new( cfg["regions"], cfg["holiday_names"], :additions => (cfg["additions"] || []).map { |s| Date.parse s }, :removals => (cfg["removals"] || []).map { |s| Date.parse s }, :additions_only => cfg['additions_only'] ) end
holiday_determiner_for_endpoint(additions_endpoint, removals_endpoint, opts)
click to toggle source
# File lib/business_calendar.rb, line 51 def holiday_determiner_for_endpoint(additions_endpoint, removals_endpoint, opts) client = Faraday.new do |conn| conn.response :raise_error conn.adapter :net_http end additions = if additions_endpoint holiday_dates_for_endpoint(client, additions_endpoint) end removals = if removals_endpoint holiday_dates_for_endpoint(client, removals_endpoint) end HolidayDeterminer.new( opts["regions"] || [], opts["holiday_names"] || [], :additions => additions, :removals => removals, :additions_only => opts["additions_only"] || [], :ttl => opts['ttl'] ) end
holiday_determiner_for_organization(org)
click to toggle source
# File lib/business_calendar.rb, line 36 def holiday_determiner_for_organization(org) cfg = org_config(org) or raise OrganizationNotSupported.new(org.inspect) @holiday_procs ||= {} @holiday_procs[org] ||= HolidayDeterminer.new( cfg["regions"], cfg["holiday_names"], :additions => (cfg["additions"] || []).map { |s| Date.parse s }, :removals => (cfg["removals"] || []).map { |s| Date.parse s }, :additions_only => cfg['additions_only'] ) end
load_config()
click to toggle source
# File lib/business_calendar.rb, line 84 def load_config files = Dir[File.join(File.dirname(File.expand_path(__FILE__)), '../data/*.yml')] files.reduce({}) { |hash, file| hash.merge! YAML.load_file(file) } end
load_config_for_organizations()
click to toggle source
# File lib/business_calendar.rb, line 95 def load_config_for_organizations files = Dir[File.join(File.dirname(File.expand_path(__FILE__)), '../data/org/*.yml')] files.reduce({}) { |hash, file| hash.merge! YAML.load_file(file) } end
org_config(org)
click to toggle source
# File lib/business_calendar.rb, line 90 def org_config(org) @org_config ||= load_config_for_organizations @org_config[org.to_s] end