Sha256: 0848471e92f4c2fea3b105f547e2d285a03bda5230106cb5ea5d75376aee42e9

Contents?: true

Size: 1.46 KB

Versions: 86

Compression:

Stored size: 1.46 KB

Contents

require "time"

module GovukError
  class GovukDataSync
    class MalformedDataSyncPeriod < RuntimeError
      def initialize(invalid_value)
        super("\"#{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

86 entries across 86 versions & 1 rubygems

Version Path
govuk_app_config-9.16.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.16.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.8 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.7 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.6 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.5 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.4 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.3 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.2 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.15.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.14.6 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.14.5 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.14.4 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.14.3 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.14.2 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.14.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.14.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.13.1 lib/govuk_app_config/govuk_error/govuk_data_sync.rb
govuk_app_config-9.13.0 lib/govuk_app_config/govuk_error/govuk_data_sync.rb