Class: Checkoff::Internal::SearchUrl::DateParamConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/checkoff/internal/search_url/date_param_converter.rb,
sig/checkoff.rbs

Overview

Convert date parameters - ones where the param name itself doesn't encode any parameters'

Constant Summary collapse

API_PREFIX =

Returns:

  • (Object)
{
  'due_date' => 'due_on',
  'start_date' => 'start_on',
  'completion_date' => 'completed_on',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date_url_params:) ⇒ DateParamConverter

@param date_url_params — the simple params

Parameters:

  • date_url_params: (::Hash[String, ::Array[String]])


13
14
15
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 13

def initialize(date_url_params:)
  @date_url_params = date_url_params
end

Instance Attribute Details

#date_url_paramsHash{String => Array<String>} (readonly)

Returns:

  • (Hash{String => Array<String>})


170
171
172
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 170

def date_url_params
  @date_url_params
end

Instance Method Details

#convertArray(Hash{String => String}, Array<Symbol, Array>)

Returns:

  • (Array(Hash{String => String}, Array<Symbol, Array>))


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 18

def convert
  return [{}, []] if date_url_params.empty?

  out = nil

  %w[due_date start_date completion_date].each do |prefix|
    next unless date_url_params.key? "#{prefix}.operator"
    raise 'Teach me how to handle simultaneous date parameters' unless out.nil?

    out = convert_for_prefix(prefix)
  end

  raise "Teach me to handle these parameters: #{date_url_params.inspect}" unless date_url_params.empty?

  ensure_matched!(out)
end

#convert_for_prefix(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]

@param prefix

Parameters:

  • prefix (String)

Returns:

  • ([::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]])


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 47

def convert_for_prefix(prefix)
  # example params:
  #   due_date.operator=through_next
  #   due_date.value=0
  #   due_date.unit=day
  operator = get_single_param("#{prefix}.operator")

  out = case operator
        when 'through_next'
          handle_through_next(prefix)
        when 'between'
          handle_between(prefix)
        when 'within_last'
          handle_within_last(prefix)
        when 'within_next'
          handle_within_next(prefix)
        else
          raise "Teach me how to handle date mode: #{operator.inspect}."
        end

  # mark these as done by deleting from the hash
  date_url_params.delete_if { |k, _| k.start_with? prefix }

  out
end

#ensure_matched!(out) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]

@param out

Parameters:

  • _out ([::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]], nil)

Returns:

  • ([::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]])


39
40
41
42
43
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 39

def ensure_matched!(out)
  return out if out

  raise "no date param matched a known prefix: #{date_url_params.inspect}"
end

#get_single_param(param_key) ⇒ String

@param param_key

Parameters:

  • param_key (String)

Returns:

  • (String)


140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 140

def get_single_param(param_key)
  raise "Expected #{param_key} to have at least one value" unless date_url_params.key? param_key

  value = date_url_params.fetch(param_key)

  raise "Expected #{param_key} to have one value" if value.length != 1

  single_value = value[0]
  raise "Expected #{param_key} to have a non-nil value" if single_value.nil?

  single_value
end

#handle_between(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]

Parameters:

  • prefix (String)

Returns:

  • ([::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]])


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 99

def handle_between(prefix)
  after = get_single_param("#{prefix}.after")
  raise "Teach me how to handle #{prefix}.before" if date_url_params.key? "#{prefix}.before"

  validate_unit_not_provided!(prefix)

  # Example value: 1702857600000
  # +1 is because API seems to operate on inclusive ranges
  # @type [Date]
  after = Time.at(after.to_i / 1000).to_date + 1
  [{ "#{API_PREFIX.fetch(prefix)}.after" => after.to_s }, []]
end

#handle_through_next(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]

Parameters:

  • prefix (String)

Returns:

  • ([::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]])


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 83

def handle_through_next(prefix)
  value = get_single_param("#{prefix}.value").to_i

  validate_unit_is_day!(prefix)

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

  # 'due_on.before' => '2023-01-01',
  # 'due_on.after' => '2023-01-01',
  # [{ 'due_on.before' => '2023-09-01' }, []]
  [{ "#{API_PREFIX.fetch(prefix)}.before" => before.to_s }, []]
end

#handle_within_last(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]

Parameters:

  • prefix (String)

Returns:

  • ([::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]])


114
115
116
117
118
119
120
121
122
123
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 114

def handle_within_last(prefix)
  value = get_single_param("#{prefix}.value").to_i

  validate_unit_is_day!(prefix)

  # @type [Date]
  after = Date.today - value

  [{ "#{API_PREFIX.fetch(prefix)}.after" => after.to_s }, []]
end

#handle_within_next(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]

Parameters:

  • prefix (String)

Returns:

  • ([::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]])


127
128
129
130
131
132
133
134
135
136
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 127

def handle_within_next(prefix)
  value = get_single_param("#{prefix}.value").to_i

  validate_unit_is_day!(prefix)

  # @type [Date]
  before = Date.today + value + 1

  [{ "#{API_PREFIX.fetch(prefix)}.before" => before.to_s }, []]
end

#validate_unit_is_day!(prefix) ⇒ void

This method returns an undefined value.

@param prefix

Parameters:

  • prefix (String)


163
164
165
166
167
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 163

def validate_unit_is_day!(prefix)
  unit = date_url_params.fetch("#{prefix}.unit").fetch(0)

  raise "Teach me how to handle other time units: #{unit}" unless unit == 'day'
end

#validate_unit_not_provided!(prefix) ⇒ void

This method returns an undefined value.

@param prefix

Parameters:

  • prefix (String)


155
156
157
158
159
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 155

def validate_unit_not_provided!(prefix)
  return unless date_url_params.key? "#{prefix}.unit"

  raise "Teach me how to handle other #{prefix}.unit for these params: #{date_url_params.inspect}"
end