Class: MentionSystem::MentionProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/mention_system/mention_processor.rb

Overview

MentionProcessor class

This class defines mention processor behavior in mention system

Instance Method Summary collapse

Constructor Details

#initializeMentionProcessor

Constructor of the MentionProcessor class



16
17
18
# File 'lib/mention_system/mention_processor.rb', line 16

def initialize
  @callbacks = { after: [], before: [] }
end

Instance Method Details

#add_after_callback(callback) ⇒ Object

Adds an after callback

Parameters:

  • callback (Proc)
    • the callback to add



25
26
27
# File 'lib/mention_system/mention_processor.rb', line 25

def add_after_callback(callback)
  @callbacks[:after].push(callback)
end

#add_before_callback(callback) ⇒ Object

Adds a before callback

Parameters:

  • callback (Proc)
    • the callback to add



34
35
36
# File 'lib/mention_system/mention_processor.rb', line 34

def add_before_callback(callback)
  @callbacks[:before].push(callback)
end

#extract_handles_from_mentioner(mentioner) ⇒ Array

Extract handles from mentioner

Parameters:

Returns:

  • (Array)


44
45
46
47
# File 'lib/mention_system/mention_processor.rb', line 44

def extract_handles_from_mentioner(mentioner)
  content = extract_mentioner_content(mentioner)
  handles = content.scan(handle_regexp).map { |handle| handle.gsub("#{mention_prefix}","") }
end

#extract_mentioner_content(mentioner) ⇒ String

Extracts the mentioner content

Parameters:

Returns:

  • (String)


55
56
57
# File 'lib/mention_system/mention_processor.rb', line 55

def extract_mentioner_content(mentioner)
  raise "Must be implemented in subclass"
end

#find_mentionees_by_handles(*handles) ⇒ Array

Finds mentionees by handles

Parameters:

Returns:

  • (Array)


65
66
67
# File 'lib/mention_system/mention_processor.rb', line 65

def find_mentionees_by_handles(*handles)
  raise "Must be implemented in subclass"
end

#handle_regexpRegexp (private)

Retrieves the handle regexp

Returns:

  • (Regexp)


93
94
95
# File 'lib/mention_system/mention_processor.rb', line 93

def handle_regexp
  /(?<!\w)#{mention_prefix}\w+/
end

#mention_prefixString (private)

Returns the mention prefix

Returns:

  • (String)


102
103
104
# File 'lib/mention_system/mention_processor.rb', line 102

def mention_prefix
  "@"
end

#process_after_callbacks(mentioner, mentionee) ⇒ Object (private)

Process after callbacks

Parameters:

  • mentioner (Mentioner)
    • the mentioner of the callback

  • mentionee (Mentionee)
    • the mentionee of the callback



112
113
114
115
116
117
118
119
120
121
# File 'lib/mention_system/mention_processor.rb', line 112

def process_after_callbacks(mentioner, mentionee)
  result = true
  @callbacks[:after].each do |callback|
    unless callback.call(mentioner, mentionee)
      result = false
      break
    end
  end
  result
end

#process_before_callbacks(mentioner, mentionee) ⇒ Object (private)

Process before callbacks

Parameters:

  • mentioner (Mentioner)
    • the mentioner of the callback

  • mentionee (Mentionee)
    • the mentionee of the callback



129
130
131
132
133
134
135
136
137
138
# File 'lib/mention_system/mention_processor.rb', line 129

def process_before_callbacks(mentioner, mentionee)
  result = true
  @callbacks[:before].each do |callback|
    unless callback.call(mentioner, mentionee)
      result = false
      break
    end
  end
  result
end

#process_mentions(mentioner) ⇒ Object

Process mentions

Parameters:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mention_system/mention_processor.rb', line 74

def process_mentions(mentioner)
  handles = extract_handles_from_mentioner(mentioner)
  mentionees = find_mentionees_by_handles(handles)

  mentionees.each do |mentionee|
    if process_before_callbacks(mentioner, mentionee)
      if mentioner.mention(mentionee)
        process_after_callbacks(mentioner, mentionee)
      end
    end
  end
end