Class: Todoist::Utilities
- Inherits:
-
Object
- Object
- Todoist::Utilities
- Defined in:
- lib/todoist/utilities.rb
Constant Summary collapse
- MON_FIRST_REGEX =
/(?<mon>#{mon_opts})\s+(?<day>\d{1,2})(?:(?:st\b)|(?:nd\b)|(?:rd\b)|(?:th\b))?/- MON_LAST_REGEX =
/(?<day>\d{1,2})(?:(?:st\b)|(?:nd\b)|(?:rd\b)|(?:th\b))?\s+(?<mon>#{mon_opts})/- DOW_REGEX =
/(?<dow>(?:#{dow_opts})s?)/- DUE_REGEX =
%r{ ^ # start of line \s*\bev(?:ery)? # ev or every (?<bang>!)? # possible bang in group bang \s+ (?<dates>.*?) # dates in group dates \s* (?:\bat)?\s* # optional keyword at (?<time>\b\d\d:\d\d\b)?\s* # optional time in group time (?:(?:\bstarting)\s+(?<start_date>.*?))?\s* # optional starting keyword followed by starting date in group starting (?:(?:\bending)\s+(?<end_date>.*?))?\s* # optional ending keyword followed by ending date in group ending $ # end of line }xi- ORDINAL_REGEX =
/\b(\d+) # first capture group which is a string of digits ( # start of 2nd capture group (?:st\b)|(?:nd\b)|(?:rd\b)|(?:th\b) # a series of anonymous capture groups of two letters # any of these letter pairs will match )/x
Class Method Summary collapse
Class Method Details
.normalize_due_string(orig_due_string) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/todoist/utilities.rb', line 28 def normalize_due_string(orig_due_string) orig_due_string = orig_due_string.downcase mm = orig_due_string.match(DUE_REGEX) if mm.nil? raise ArgumentError, "Due String not recognized: #{orig_due_string}" end if mm[:dates] dates = mm[:dates].split(/\s*,\s*/).map(&:downcase) new_dates = [] dates.each do |d| if (dm = d.match(/(?<num>\d+)\s+weeks?/)) if dm[:num].to_i <= 7 d = "#{dm[:num].to_i * 7} days" end elsif (dm = d.match(/(?<mon>\d{1,2})\/(?<day>\d{1,2})/)) d = "#{dm[:day]} #{Date::ABBR_MONTHNAMES[dm[:mon].to_i].downcase}" elsif (dm = d.match(MON_FIRST_REGEX) || d.match(MON_LAST_REGEX)) d = "#{dm[:day]} #{dm[:mon][0..2]}" elsif (dm = d.match(DOW_REGEX)) d = d.gsub(dm[:dow], dm[:dow][0..2]) end new_dates << d end new_dates = new_dates.join(", ") new_dates.gsub!(ORDINAL_REGEX, '\1') end new_due = "ev#{mm[:bang]} #{new_dates}" new_due += " at #{mm[:time]}" if mm[:time] new_due += " ending #{mm[:end_date]}" if mm[:end_date] if new_due != orig_due_string new_due.strip end end |