Sha256: 6a08b62da0c5429e757992ce5470f0f6b32e6a9d12e2c5671fa287defd4f1126

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require_relative 'custom_field_param_converter'

module Checkoff
  module Internal
    module SearchUrl
      # Convert date parameters - ones where the param name itself
      # doesn't encode any parameters'
      class DateParamConverter
        # @param date_url_params [Hash<String, Array<String>>] the simple params
        def initialize(date_url_params:)
          @date_url_params = date_url_params
        end

        # @return [Array(Hash<String, String>, Array<[Symbol, Array]>)] See https://developers.asana.com/docs/search-tasks-in-a-workspace
        def convert
          return [{}, []] if date_url_params.empty?

          # example params:
          #   due_date.operator=through_next
          #   due_date.value=0
          #   due_date.unit=day
          validate_due_date_through_next!

          value = date_url_params.fetch('due_date.value').fetch(0).to_i

          # @sg-ignore
          # @type [Date]
          before = Date.today + value

          validate_unit_is_day!

          # 'due_on.before' => '2023-01-01',
          # 'due_on.after' => '2023-01-01',
          # [{ 'due_on.before' => '2023-09-01' }, []]
          [{ 'due_on.before' => before.to_s }, []]
        end

        private

        # @return [void]
        def validate_unit_is_day!
          unit = date_url_params.fetch('due_date.unit').fetch(0)

          raise 'Teach me how to handle other time units' unless unit == 'day'
        end

        # @return [void]
        def validate_due_date_through_next!
          return if date_url_params.fetch('due_date.operator') == ['through_next']

          raise 'Teach me how to handle other date modes'
        end

        # @return [Hash<String, Array<String>>]
        attr_reader :date_url_params
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
checkoff-0.46.0 lib/checkoff/internal/search_url/date_param_converter.rb