class SmartTodo::Dispatchers::Base

Public Class Methods

class_for(dispatcher) click to toggle source

Factory pattern to retrive the right dispatcher class.

@param dispatcher [String]

@return [Class]

# File lib/smart_todo/dispatchers/base.rb, line 11
def self.class_for(dispatcher)
  case dispatcher
  when "slack"
    Slack
  when nil, 'output'
    Output
  end
end
new(event_message, todo_node, file, options) click to toggle source

@param event_message [String] the success message associated

a specific event

@param todo_node [SmartTodo::Parser::TodoNode] @param file [String] the file containing the TODO @param options [Hash]

# File lib/smart_todo/dispatchers/base.rb, line 36
def initialize(event_message, todo_node, file, options)
  @event_message = event_message
  @todo_node = todo_node
  @options = options
  @file = file
  @assignee = @todo_node.metadata.assignee
end
validate_options!(_options) click to toggle source

Subclasses should define what options from the CLI they need in order to properly deliver the message. For instance the Slack dispatcher requires an API key.

@param _options [Hash]

@return void

# File lib/smart_todo/dispatchers/base.rb, line 27
def self.validate_options!(_options)
  raise(NotImplemetedError, 'subclass responsability')
end

Public Instance Methods

dispatch() click to toggle source

This method gets called when a TODO reminder is expired and needs to be delivered. Dispatchers should implement this method to deliver the message where they need.

@return void

# File lib/smart_todo/dispatchers/base.rb, line 48
def dispatch
  raise(NotImplemetedError, 'subclass responsability')
end

Private Instance Methods

existing_user() click to toggle source

@param user [Hash]

# File lib/smart_todo/dispatchers/base.rb, line 87
def existing_user
  "Hello :wave:,"
end
slack_message(user) click to toggle source

Prepare the content of the message to send to the TODO assignee

@param user [Hash] contain information about a user @return [String]

# File lib/smart_todo/dispatchers/base.rb, line 58
      def slack_message(user)
        header = if user.key?('fallback')
          unexisting_user
        else
          existing_user
        end

        <<~EOM
          #{header}

          You have an assigned TODO in the `#{@file}` file.
          #{@event_message}

          Here is the associated comment on your TODO:

          ```
          #{@todo_node.comment.strip}
          ```
        EOM
      end
unexisting_user() click to toggle source

Message in case a TODO's assignee doesn't exist in the Slack organization

@return [String]

# File lib/smart_todo/dispatchers/base.rb, line 82
def unexisting_user
  "Hello :wave:,\n\n`#{@assignee}` had an assigned TODO but this user or channel doesn't exist on Slack anymore."
end