dawarich/app/services/maps/date_parameter_coercer.rb

40 lines
749 B
Ruby
Raw Normal View History

2025-09-16 14:41:53 -04:00
# frozen_string_literal: true
module Maps
class DateParameterCoercer
class InvalidDateFormatError < StandardError; end
def initialize(param)
@param = param
end
def call
coerce_date(@param)
end
private
attr_reader :param
def coerce_date(param)
case param
when String
2025-09-16 19:55:42 -04:00
coerce_string_param(param)
2025-09-16 14:41:53 -04:00
when Integer
param
else
param.to_i
end
rescue ArgumentError => e
Rails.logger.error "Invalid date format: #{param} - #{e.message}"
raise InvalidDateFormatError, "Invalid date format: #{param}"
end
2025-09-16 19:55:42 -04:00
def coerce_string_param(param)
2025-09-18 14:15:49 -04:00
return param.to_i if param.match?(/^\d+$/)
Time.parse(param).to_i
2025-09-16 19:55:42 -04:00
end
2025-09-16 14:41:53 -04:00
end
end