Class: Obfuscator::DateObfuscator

Inherits:
Object
  • Object
show all
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

Examples:

Basic usage with preset format

obfuscator = DateObfuscator.new
obfuscator.obfuscate('2023-12-31')  # => "2025-07-15"

With custom format string

obfuscator = DateObfuscator.new(format: '%Y-%m-%d')
obfuscator.obfuscate('2023-12-31')  # => "2025-07-15"

With constraints

obfuscator = DateObfuscator.new(
  constraints: {
    min_year: 2020,        # Minimum year to generate
    max_year: 2025,        # Maximum year to generate
    preserve_month: true,   # Keep the same month
    preserve_weekday: true  # Keep the same day of week
  }
)

With seed for reproducible results

obfuscator = DateObfuscator.new(seed: 12345)
obfuscator.obfuscate('2023-12-31')  # => Same result for same seed

Raises:

  • (Error)

    If date string is invalid or doesn’t match the format

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

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

    obfuscated_date = generate_date(date)
    obfuscated_date.strftime(@format)
  rescue ArgumentError => e
    raise Error, "Invalid date or format: #{e.message}"
  end
end