Class: Slack::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_mailer.rb,
lib/slack_mailer/version.rb,
lib/slack_mailer/configuration.rb,
lib/slack_mailer/delivery_worker.rb

Defined Under Namespace

Classes: Configuration, DeliveryWorker

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.method_missing(method, *args) ⇒ Object



9
10
11
# File 'lib/slack_mailer.rb', line 9

def method_missing(method, *args)
  self.new.send(method, *args)
end

.send_direct_message(channel = '', name = '', message = '') ⇒ Object



18
19
20
21
22
# File 'lib/slack_mailer.rb', line 18

def send_direct_message(channel = '', name = '', message = '')
  return if channel.empty? || name.empty? || message.empty?
  url = Slack::Mailer::Configuration.config.slack_hook_urls.mrandom
  Slack::Notifier.new(url, username: name, channel: channel, link_names: 1).ping(message)
end

.send_message(channel = '', name = '', message = '') ⇒ Object



13
14
15
16
# File 'lib/slack_mailer.rb', line 13

def send_message(channel = '', name = '', message = '')
  return if channel.empty? || name.empty? || message.empty?
  Slack::Mailer::DeliveryWorker.perform_async(name: name, message: message, channel: channel)
end

Instance Method Details

#collect_message(template, instance_variables) ⇒ Object



36
37
38
39
40
41
# File 'lib/slack_mailer.rb', line 36

def collect_message(template, instance_variables)
  template = "#{template}.#{Slack::Mailer::Configuration.config.templates_type}"
  template + '.erb' if Slack::Mailer::Configuration.config.erb_in_templates
  ActionView::Base.new("#{Slack::Mailer::Configuration.config.templates_path}/#{self.class.name.underscore}",
                       {}, ActionController::Base.new).render(file: template, locals: instance_variables || {})
end

#mail(to: nil, template: nil, use_sidekiq: true) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/slack_mailer.rb', line 26

def mail(to: nil, template: nil, use_sidekiq: true)
  channel = '#' << to
  instance_variables = self.instance_variables.map { |instance_variable|
    { instance_variable => self.instance_variable_get(instance_variable) }
  }.reduce(:merge)
  message = collect_message(template, instance_variables)
  method = use_sidekiq ? :send_message : :send_direct_message
  self.class.send(method, channel, "#{self.class.name}##{template}", message)
end