Sha256: 9df0adf96cd392c778f2e610b9fb4ad7c6ecd0716a2d49c60b1d4b0cb8c9bd46

Contents?: true

Size: 1.97 KB

Versions: 6

Compression:

Stored size: 1.97 KB

Contents

# 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<OfficeHour>]
      #
      # @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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
qismo-0.1.8 lib/qismo/resources/office_setting_resource.rb
qismo-0.1.5 lib/qismo/resources/office_setting_resource.rb
qismo-0.1.4 lib/qismo/resources/office_setting_resource.rb
qismo-0.1.2 lib/qismo/resources/office_setting_resource.rb
qismo-0.1.1 lib/qismo/resources/office_setting_resource.rb
qismo-0.1.0 lib/qismo/resources/office_setting_resource.rb