Class: Obfuscator::DateObfuscator
- Inherits:
-
Object
- Object
- Obfuscator::DateObfuscator
- Includes:
- Internal::RNG
- Defined in:
- lib/obfuscator/date_obfuscator.rb
Overview
Class for obfuscating dates while preserving their format and optionally some properties.
Supports various date formats through presets or custom format strings. Can preserve certain date characteristics (month, weekday) and respect year constraints. All generated dates are valid - for example, it won’t generate February 31st.
Available preset formats:
-
:eu => ‘%d.%m.%Y’ # 31.12.2023
-
:eu_short => ‘%d.%m.%y’ # 31.12.23
-
:rus => ‘%d.%m.%Y’ # 31.12.2023
-
:rus_short => ‘%d.%m.%y’ # 31.12.23
-
:iso => ‘%Y-%m-%d’ # 2023-12-31
-
:us => ‘%m/%d/%Y’ # 12/31/2023
-
:us_short => ‘%m/%d/%y’ # 12/31/23
-
:iso_full => ‘%Y-%m-%dT%H:%M:%S%z’ # 2023-12-31T00:00:00+00:00
Constant Summary collapse
- PRESET_FORMATS =
{ eu: '%d.%m.%Y', # 31.12.2023 eu_short: '%d.%m.%y', # 31.12.23 rus: '%d.%m.%Y', # 31.12.2023 rus_short: '%d.%m.%y', # 31.12.23 iso: '%Y-%m-%d', # 2023-12-31 us: '%m/%d/%Y', # 12/31/2023 us_short: '%m/%d/%y', # 12/31/23 iso_full: '%Y-%m-%dT%H:%M:%S%z' # 2023-12-31T00:00:00+00:00 }.freeze
Instance Method Summary collapse
-
#initialize(format: :iso, seed: nil, constraints: {}) ⇒ DateObfuscator
constructor
A new instance of DateObfuscator.
- #obfuscate(date_string) ⇒ Object
Constructor Details
#initialize(format: :iso, seed: nil, constraints: {}) ⇒ DateObfuscator
Returns a new instance of DateObfuscator.
68 69 70 71 72 73 |
# File 'lib/obfuscator/date_obfuscator.rb', line 68 def initialize(format: :iso, seed: nil, constraints: {}) @seed = seed # Store the seed setup_rng(seed) @format = PRESET_FORMATS[format] || format @constraints = default_constraints.merge(constraints) end |
Instance Method Details
#obfuscate(date_string) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/obfuscator/date_obfuscator.rb', line 75 def obfuscate(date_string) return date_string if !date_string.is_a?(Date) && (date_string.nil? || date_string.empty?) begin date = date_string.is_a?(Date) ? date_string : ::Date.strptime(date_string, @format) # Create a unique seed based on both the original seed and input date if @seed combined_seed = [@seed, date.to_time.to_i].hash setup_rng(combined_seed) end = generate_date(date) .strftime(@format) rescue ArgumentError => e raise Error, "Invalid date or format: #{e.message}" end end |