Sha256: 5735269efd08290d00c60baae5264fd96ffb7aedc75609fa99ddeace99130793

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

require "time"

module GovukError
  class GovukDataSync
    class MalformedDataSyncPeriod < RuntimeError
      attr_reader :invalid_value

      def initialize(invalid_value)
        @invalid_value = invalid_value
      end

      def message
        "\"#{invalid_value}\" is not a valid value (should be of form '22:00-03:00')."
      end
    end

    attr_reader :from, :to

    def initialize(govuk_data_sync_period)
      return if govuk_data_sync_period.nil?

      parts = govuk_data_sync_period.split("-")
      raise MalformedDataSyncPeriod, govuk_data_sync_period unless parts.count == 2

      @from, @to = parts.map { |time| Time.parse(time) }
    rescue ArgumentError
      raise MalformedDataSyncPeriod, govuk_data_sync_period
    end

    def in_progress?
      !from.nil? && !to.nil? && in_time_range?(from, to)
    end

  private

    # `from`/`to` times are in relation to the local server time, which is expected to be in UTC as per:
    # https://github.com/alphagov/govuk-puppet/blob/b588e4ade996e97b8975e69cb00800521fff4a48/modules/govuk_envsys/files/etc/environment#L3
    def in_time_range?(from, to)
      hour_is_in_range = Time.now.hour >= from.hour || Time.now.hour <= to.hour
      minute_is_in_range = if Time.now.hour == from.hour
                             Time.now.min >= from.min
                           elsif Time.now.hour == to.hour
                             Time.now.min <= to.min
                           else
                             true
                           end
      hour_is_in_range && minute_is_in_range
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
govuk_app_config-3.1.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-3.1.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-3.0.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.10.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.9.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.9.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.8.4 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.8.3 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.8.2 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.8.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.8.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-2.7.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb