Class: ConfidentialInfoRedactor::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/confidential_info_redactor/date.rb

Constant Summary collapse

EN_DOW =
%w(monday tuesday wednesday thursday friday saturday sunday).freeze
EN_DOW_ABBR =
%w(mon tu tue tues wed th thu thur thurs fri sat sun).freeze
EN_MONTHS =
%w(january february march april may june july august september october november december).freeze
EN_MONTH_ABBR =
%w(jan feb mar apr jun jul aug sep sept oct nov dec).freeze
DE_DOW =
%w(montag dienstag mittwoch donnerstag freitag samstag sonntag sonnabend).freeze
DE_DOW_ABBR =
%w(mo di mi do fr sa so).freeze
DE_MONTHS =
%w(januar februar märz april mai juni juli august september oktober november dezember).freeze
DE_MONTH_ABBR =
%w(jan jän feb märz apr mai juni juli aug sep sept okt nov dez).freeze
DMY_MDY_REGEX =
/(\d{1,2}(\/|\.|-)){2}\d{4}/
YMD_YDM_REGEX =
/\d{4}(\/|\.|-)(\d{1,2}(\/|\.|-)){2}/
DIGIT_ONLY_YEAR_FIRST_REGEX =
/[12]\d{7}\D/
DIGIT_ONLY_YEAR_LAST_REGEX =
/\d{4}[12]\d{3}\D/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language:) ⇒ Date

Returns a new instance of Date.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#dowObject (readonly)

Returns the value of attribute dow.



24
25
26
# File 'lib/confidential_info_redactor/date.rb', line 24

def dow
  @dow
end

#dow_abbrObject (readonly)

Returns the value of attribute dow_abbr.



24
25
26
# File 'lib/confidential_info_redactor/date.rb', line 24

def dow_abbr
  @dow_abbr
end

#languageObject (readonly)

Returns the value of attribute language.



24
25
26
# File 'lib/confidential_info_redactor/date.rb', line 24

def language
  @language
end

#monthsObject (readonly)

Returns the value of attribute months.



24
25
26
# File 'lib/confidential_info_redactor/date.rb', line 24

def months
  @months
end

#months_abbrObject (readonly)

Returns the value of attribute months_abbr.



24
25
26
# File 'lib/confidential_info_redactor/date.rb', line 24

def months_abbr
  @months_abbr
end

Instance Method Details

#includes_date?(text) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# 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) ⇒ Object



58
59
60
# File 'lib/confidential_info_redactor/date.rb', line 58

def occurences(text)
  replace(text).scan(/<redacted date>/).size
end

#replace(text) ⇒ Object



50
51
52
53
54
55
56
# 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) ⇒ Object



62
63
64
65
66
67
# 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