dawarich/app/services/maps/date_parameter_coercer.rb

43 lines
838 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)
# Check if it's a numeric string (timestamp) or date string
if param.match?(/^\d+$/)
param.to_i
else
Time.parse(param).to_i
end
end
2025-09-16 14:41:53 -04:00
end
end