Class: CollinsNotify::SlackAdapter

Inherits:
Notifier
  • Object
show all
Defined in:
lib/collins_notify/adapter/slack.rb

Overview

currently this only supports incoming webhooks to pust to slack tumblr.slack.com/services/new/incoming-webhook

Instance Method Summary collapse

Methods inherited from Notifier

adapter_name, adapters, get_adapter, #initialize, mimetypes, register_name, require_config, required_config_keys, requires_config?, #supports_html?, supports_mimetype, #supports_text?

Constructor Details

This class inherits a constructor from CollinsNotify::Notifier

Instance Method Details

#configure!Object



13
14
15
16
# File 'lib/collins_notify/adapter/slack.rb', line 13

def configure!
  @webhook_url = config.adapters[:slack][:webhook_url]
  logger.info "Configured Slack adapter"
end

#notify!(message_obj = OpenStruct.new, to = nil) ⇒ Object

Available in template binding: message_obj - Depends on call channel - channel sending to



21
22
23
24
25
26
27
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
# File 'lib/collins_notify/adapter/slack.rb', line 21

def notify! message_obj = OpenStruct.new, to = nil
  slack_hash = Hash.new
  channel = get_channel config.adapters[:slack], to
  slack_hash['channel'] = channel
  optional_parameters = [:username, :icon_url, :icon_emoji]
  optional_parameters.each do |op|
    if config.adapters[:slack][op]
      slack_hash[op] = config.adapters[:slack][op]
    end
  end

  slack_hash['text'] = get_message_body(binding)

  @logger.debug "slack parameters: #{slack_hash.inspect}"
    
  if config.test? then
    @logger.info "Not sending message in test mode"
    return true
  end

  begin
    @logger.debug "Posting to slack webhook: #{@webhook_url}"
    reply = HTTParty.post(@webhook_url, :body => JSON.dump(slack_hash))
    reply.response.value # this raises an error if the response said it was unsuccessful
    true
  rescue CollinsNotify::CollinsNotifyException => e
    @logger.error "error sending slack notification - #{e}"
    raise e
  rescue Exception => e
    @logger.error "#{e.class.to_s} - error sending slack notification - #{e}"
    raise CollinsNotify::CollinsNotifyException.new e
  end
end