# frozen_string_literal: true module Qismo module Resources module OfficeSettingResource # # Get office hours # # @return [OfficeSetting] # def office_hours resp = Qismo.client.get("/api/v1/admin/office_hours") body = resp.http_body data = body["data"] morfed_data = {} morfed_data["online_message"] = data["online_message"] morfed_data["offline_message"] = data["offline_message"] morfed_data["timezone"] = data["timezone"] morfed_data["send_online_if_resolved"] = data["send_online_if_resolved"] morfed_data["send_offline_each_message"] = data["send_offline_each_message"] office_hours = [] data["office_hours"].each do |oh| office_hours.append(OfficeHour.new(oh)) end morfed_data["office_hours"] = office_hours morfed_data["is_in_office_hour"] = check_is_in_office_hour?( morfed_data["timezone"], morfed_data["office_hours"] ) OfficeSetting.new(morfed_data) end private # # Check if current timestamp is in office hour # # @param timezone [String,Integer] # @param office_hours [Array] # # @return [TrueClass, FalseClass] # def check_is_in_office_hour?(timezone, office_hours) current = Time.now.getlocal(timezone) current_weekday = current.strftime("%u").to_i today_oh = office_hours.find { |oh| oh.day == current_weekday } return false if today_oh.nil? start_time = Time.parse("#{current.year}-#{current.month}-#{current.day} #{today_oh.starttime} #{timezone}") end_time = Time.parse("#{current.year}-#{current.month}-#{current.day} #{today_oh.endtime} #{timezone}") int_start_time = start_time.to_i int_end_time = end_time.to_i int_current = current.to_i (int_start_time..int_end_time).include?(int_current) end end end end