Time based conditions in AGI application


Currently I am re-writing an VoIP back-end with very flexible business time management.
I have an SOHO PBX system where processing of incoming call are time dependent:

  • During night call should be delivered to voicemail;
  • At morning, lunch time, evening should be forwarded to cell phone;
  • At business time - processed normally;
  • Friday's time is shorter, at Saturday company have custom business times, Sunday - closed (voicemail);
  • Calls on public holidays should be forwarded to the voicemail.

And also customer want to manage times and add any exceptions.

So, I have created (long-time ago) a following class. Below you see AR migration and model and test results as well.

Tests:

1.9.3-p327 :001 > Campaign.last.business_time.operational?
  Campaign Load (0.1ms)  SELECT "campaigns".* FROM "campaigns" ORDER BY "campaigns"."id" DESC LIMIT 1
  BusinessTime Load (0.2ms)  SELECT "business_times".* FROM "business_times" WHERE "business_times"."id" = 1 LIMIT 1
  BusinessTimeMember Load (0.3ms)  SELECT "business_time_members".* FROM "business_time_members" WHERE "business_time_members"."business_time_id" = 1 ORDER BY
 business_time_id,
 day_of_month desc,
 month desc,
 year desc,
 weekday desc,
 time_from desc,
 time_to asc

 => 0

Items is polymorphic association and are subject of another discussion, with class Items I can easily organize any kind of voice call processing.
Command operational? returns an nil if time conditions did not met (office closed!), or item_id (zero for "default" processing).
Just sharing this code, hope it could be useful for someone.

Code: