Class: RichMessageExtractor

Inherits:
Object
  • Object
show all
Extended by:
ActionView::Helpers::SanitizeHelper::ClassMethods
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/services/rich_message_extractor.rb

Constant Summary collapse

PROJECT_MARKER =
'~'
CONTEXT_MARKER =
'@'
TICKLER_MARKER =
'>'
DUE_MARKER =
'<'
TAG_MARKER =
'#'
STAR_MARKER =
'*'
ALL_MARKERS =
[
  PROJECT_MARKER,
  CONTEXT_MARKER,
  TICKLER_MARKER,
  DUE_MARKER,
  TAG_MARKER,
  STAR_MARKER
]

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ RichMessageExtractor

Returns a new instance of RichMessageExtractor


22
23
24
# File 'app/services/rich_message_extractor.rb', line 22

def initialize(message)
  @message = message
end

Instance Method Details

#contextObject


31
32
33
34
# File 'app/services/rich_message_extractor.rb', line 31

def context
  context = select_for(CONTEXT_MARKER)
  context.blank? ? '' : sanitize(context[1].strip)
end

#descriptionObject


26
27
28
29
# File 'app/services/rich_message_extractor.rb', line 26

def description
  desc = select_for('')
  desc.blank? ? '' : sanitize(desc[1].strip)
end

#dueObject


52
53
54
55
# File 'app/services/rich_message_extractor.rb', line 52

def due
  due = select_for DUE_MARKER
  due.blank? ? nil : Time.zone.parse(fix_date_string(due[1].strip))
end

#fix_date_string(yymmdd) ⇒ Object (private)


72
73
74
# File 'app/services/rich_message_extractor.rb', line 72

def fix_date_string yymmdd
  "20#{yymmdd[0..1]}-#{yymmdd[2..3]}-#{yymmdd[4..5]} 00:00"
end

#projectObject


36
37
38
39
# File 'app/services/rich_message_extractor.rb', line 36

def project
  project = select_for PROJECT_MARKER
  project.blank? ? nil : sanitize(project[1].strip)
end

#select_for(symbol) ⇒ Object (private)


68
69
70
# File 'app/services/rich_message_extractor.rb', line 68

def select_for symbol
  @message.match /#{symbol}(.*?)(?=[#{ALL_MARKERS.join}]|\Z)/
end

#show_fromObject


57
58
59
60
# File 'app/services/rich_message_extractor.rb', line 57

def show_from
  show_from = select_for TICKLER_MARKER
  show_from.blank? ? nil : Time.zone.parse(fix_date_string(show_from[1].strip))
end

#starred?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'app/services/rich_message_extractor.rb', line 62

def starred?
  @message.include? '*'
end

#tagsObject


41
42
43
44
45
46
47
48
49
50
# File 'app/services/rich_message_extractor.rb', line 41

def tags
  string = @message.dup
  tags = []
  # Regex only matches one tag, so recurse until we have them all
  while string.match /#(.*?)(?=[#{ALL_MARKERS.join}]|\Z)/
    tags << sanitize($1)
    string.gsub!(/##{$1}/,'')
  end
  tags.empty? ? nil : tags
end