class ConfidentialInfoRedactor::Date

Constants

DE_DOW
DE_DOW_ABBR
DE_MONTHS
DE_MONTH_ABBR
DIGIT_ONLY_YEAR_FIRST_REGEX

Rubular: rubular.com/r/SRZ27XNlvR

DIGIT_ONLY_YEAR_LAST_REGEX

Rubular: rubular.com/r/mpVSeaKwdY

DMY_MDY_REGEX

Rubular: rubular.com/r/73CZ2HU0q6

EN_DOW
EN_DOW_ABBR
EN_MONTHS
EN_MONTH_ABBR
YMD_YDM_REGEX

Rubular: rubular.com/r/GWbuWXw4t0

Attributes

dow[R]
dow_abbr[R]
language[R]
months[R]
months_abbr[R]

Public Class Methods

new(language:) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 25
def initialize(language:)
  @language = language
  case language
  when 'en'
    @dow = EN_DOW
    @dow_abbr = EN_DOW_ABBR
    @months = EN_MONTHS
    @months_abbr = EN_MONTH_ABBR
  when 'de'
    @dow = DE_DOW
    @dow_abbr = DE_DOW_ABBR
    @months = DE_MONTHS
    @months_abbr = DE_MONTH_ABBR
  else
    @dow = EN_DOW
    @dow_abbr = EN_DOW_ABBR
    @months = EN_MONTHS
    @months_abbr = EN_MONTH_ABBR
  end
end

Public Instance Methods

includes_date?(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 46
def includes_date?(text)
  includes_long_date?(text) || includes_number_only_date?(text)
end
occurences(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 58
def occurences(text)
  replace(text).scan(/<redacted date>/).size
end
replace(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 50
def replace(text)
  return text unless is_an_array?
  counter = 0
  dow_abbr.map { |day| counter +=1 if text.include?('day') }
  text = redact_dates(counter, text)
  redact_regex(text)
end
replace_number_only_date(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 62
def replace_number_only_date(text)
  text.gsub(DMY_MDY_REGEX, ' <redacted date> ')
      .gsub(YMD_YDM_REGEX, ' <redacted date> ')
      .gsub(DIGIT_ONLY_YEAR_FIRST_REGEX, ' <redacted date> ')
      .gsub(DIGIT_ONLY_YEAR_LAST_REGEX, ' <redacted date> ')
end

Private Instance Methods

check_for_matches(day, month, text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 147
def check_for_matches(day, month, text)
  !(text !~ /#{Regexp.escape(day)}(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i) ||
  !(text !~ /#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i) ||
  !(text !~ /\d{4}\.*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*/i) ||
  !(text !~ /\d{4}(\.|-|\/)*#{Regexp.escape(month)}(\.|-|\/)*\d+/i) ||
  !(text !~ /#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*/i) ||
  !(text !~ /\d{2}(\.|-|\/)*#{Regexp.escape(month)}(\.|-|\/)*(\d{4}|\d{2})/i)
end
includes_long_date?(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 120
def includes_long_date?(text)
  includes_long_date_1?(text) || includes_long_date_2?(text)
end
includes_long_date_1?(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 124
def includes_long_date_1?(text)
  dow.each do |day|
    months.map { |month| return true if check_for_matches(day, month, text) }
    months_abbr.map { |month| return true if check_for_matches(day, month, text) }
  end
  false
end
includes_long_date_2?(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 132
def includes_long_date_2?(text)
  dow_abbr.each do |day|
    months.map { |month| return true if !(text !~ /#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*(,)*\s\d{4}/i) }
    months_abbr.map { |month| return true if !(text !~ /#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i) }
  end
  false
end
includes_number_only_date?(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 140
def includes_number_only_date?(text)
  !(text !~ DMY_MDY_REGEX) ||
  !(text !~ YMD_YDM_REGEX) ||
  !(text !~ DIGIT_ONLY_YEAR_FIRST_REGEX) ||
  !(text !~ DIGIT_ONLY_YEAR_LAST_REGEX)
end
is_an_array?() click to toggle source
# File lib/confidential_info_redactor/date.rb, line 71
def is_an_array?
  dow.kind_of?(Array) && dow_abbr.kind_of?(Array) && months.kind_of?(Array) && months_abbr.kind_of?(Array)
end
redact_date(text, day, month) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 109
def redact_date(text, day, month)
  text.gsub(/#{Regexp.escape(day)}(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i, ' <redacted date> ')
                   .gsub(/\d+\s+de\s+#{Regexp.escape(month)}\s\d{4}/i, ' <redacted date> ')
                   .gsub(/\d{2}(\.|-|\/)*\s?#{Regexp.escape(month)}(\.|-|\/)*\s?(\d{4}|\d{2})/i, ' <redacted date> ')
                   .gsub(/#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i, ' <redacted date> ')
                   .gsub(/\d{4}\.*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*/i, ' <redacted date> ')
                   .gsub(/\d{4}(\.|-|\/)*#{Regexp.escape(month)}(\.|-|\/)*\d+/i, ' <redacted date> ')
                   .gsub(/#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*/i, ' <redacted date> ')
                   .gsub(/#{Regexp.escape(month)}\sde\s\d+(rd|th|st)*/i, ' <redacted date> ')
end
redact_dates(counter, text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 75
def redact_dates(counter, text)
  if counter > 0
    text = redact_dow_abbr(text)
    text = redact_dow(text)
  else
    text = redact_dow(text)
    text = redact_dow_abbr(text)
  end
  text
end
redact_dow(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 93
def redact_dow(text)
  dow.each do |day|
    months.map { |month| text = redact_date(text, day, month) }
    months_abbr.map { |month| text = redact_date(text, day, month) }
  end
  text
end
redact_dow_abbr(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 101
def redact_dow_abbr(text)
  dow_abbr.each do |day|
    months.map { |month| text = text.gsub(/#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}\s\d+(rd|th|st)*(,)*\s\d{4}/i, ' <redacted date> ') }
    months_abbr.map { |month| text = text.gsub(/#{Regexp.escape(day)}(\.)*(,)*\s#{Regexp.escape(month)}(\.)*\s\d+(rd|th|st)*(,)*\s\d{4}/i, ' <redacted date> ') }
  end
  text
end
redact_regex(text) click to toggle source
# File lib/confidential_info_redactor/date.rb, line 86
def redact_regex(text)
  text.gsub(DMY_MDY_REGEX, ' <redacted date> ')
      .gsub(YMD_YDM_REGEX, ' <redacted date> ')
      .gsub(DIGIT_ONLY_YEAR_FIRST_REGEX, ' <redacted date> ')
      .gsub(DIGIT_ONLY_YEAR_LAST_REGEX, ' <redacted date> ')
end