Module: Nfe::DateNormalizer
- Defined in:
- lib/nfe/date_normalizer.rb,
sig/nfe/date_normalizer.rbs
Overview
Normalizes date inputs to the canonical YYYY-MM-DD string the lookup APIs
expect. Accepts either an already-formatted ISO date string or a Ruby
date/time object, and raises InvalidRequestError (Portuguese-language
message) on any malformed, out-of-range, or unsupported input.
Constant Summary collapse
- ISO_DATE =
/\A\d{4}-\d{2}-\d{2}\z/
Class Method Summary collapse
- .from_string(value) ⇒ Object private
-
.to_iso_date(input) ⇒ String
The date formatted as
YYYY-MM-DD.
Instance Method Summary collapse
Class Method Details
.from_string(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 32 33 34 35 |
# File 'lib/nfe/date_normalizer.rb', line 29 def from_string(value) raise Nfe::InvalidRequestError, "data inválida: use o formato YYYY-MM-DD" unless ISO_DATE.match?(value) Date.iso8601(value).strftime("%Y-%m-%d") rescue ArgumentError raise Nfe::InvalidRequestError, "data inválida: #{value.inspect} está fora do intervalo válido" end |
.to_iso_date(input) ⇒ String
Returns the date formatted as YYYY-MM-DD.
19 20 21 22 23 24 25 26 |
# File 'lib/nfe/date_normalizer.rb', line 19 def to_iso_date(input) case input when String then from_string(input) when Date, Time, DateTime then input.strftime("%Y-%m-%d") else raise Nfe::InvalidRequestError, "data inválida: tipo não suportado (#{input.class})" end end |
Instance Method Details
#self?.from_string ⇒ String
6 |
# File 'sig/nfe/date_normalizer.rbs', line 6
def self?.from_string: (String value) -> String
|
#self?.to_iso_date ⇒ String
5 |
# File 'sig/nfe/date_normalizer.rbs', line 5
def self?.to_iso_date: (String | Date | Time | DateTime) -> String
|