Class: Checkoff::Internal::SearchUrl::DateParamConverter
- Inherits:
-
Object
- Object
- Checkoff::Internal::SearchUrl::DateParamConverter
- 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 =
{ 'due_date' => 'due_on', 'start_date' => 'start_on', 'completion_date' => 'completed_on', }.freeze
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#convert ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@sg-ignore.
-
#convert_for_prefix(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@param
prefix. -
#get_single_param(param_key) ⇒ String
@sg-ignore.
-
#handle_between(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@param
prefix. -
#handle_through_next(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@param
prefix. -
#handle_within_last(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@param
prefix. -
#handle_within_next(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@param
prefix. -
#initialize(date_url_params:) ⇒ DateParamConverter
constructor
@param
date_url_params— the simple params. -
#validate_unit_is_day!(prefix) ⇒ void
@param
prefix. -
#validate_unit_not_provided!(prefix) ⇒ void
@param
prefix.
Constructor Details
#initialize(date_url_params:) ⇒ DateParamConverter
@param date_url_params — the simple params
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_params ⇒ Hash{String => Array<String>} (readonly)
161 162 163 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 161 def date_url_params @date_url_params end |
Instance Method Details
#convert ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@sg-ignore
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 19 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? out end |
#convert_for_prefix(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@param prefix
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 40 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 |
#get_single_param(param_key) ⇒ String
@sg-ignore
@param param_key
134 135 136 137 138 139 140 141 142 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 134 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 value[0] end |
#handle_between(prefix) ⇒ [::Hash[String, String], ::Array[(Symbol | ::Array[untyped])]]
@param prefix
@return — See https://developers.asana.com/docs/search-tasks-in-a-workspace
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 92 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])]]
@param prefix
@return — See https://developers.asana.com/docs/search-tasks-in-a-workspace
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 76 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])]]
@param prefix
@return — See https://developers.asana.com/docs/search-tasks-in-a-workspace
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 107 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])]]
@param prefix
@return — See https://developers.asana.com/docs/search-tasks-in-a-workspace
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 120 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
154 155 156 157 158 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 154 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
146 147 148 149 150 |
# File 'lib/checkoff/internal/search_url/date_param_converter.rb', line 146 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 |