Class: Slack::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/slack-notifier.rb,
lib/slack-notifier/config.rb,
lib/slack-notifier/version.rb,
lib/slack-notifier/util/escape.rb,
lib/slack-notifier/util/http_client.rb,
lib/slack-notifier/payload_middleware.rb,
lib/slack-notifier/util/link_formatter.rb,
lib/slack-notifier/payload_middleware/base.rb,
lib/slack-notifier/payload_middleware/stack.rb,
lib/slack-notifier/payload_middleware/format_message.rb,
lib/slack-notifier/payload_middleware/format_attachments.rb

Defined Under Namespace

Modules: Util Classes: Config, PayloadMiddleware

Constant Summary collapse

VERSION =

rubocop:disable Style/RedundantFreeze

"2.0.0".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url, options = {}, &block) ⇒ Notifier

Returns a new instance of Notifier.



15
16
17
18
19
20
21
22
23
# File 'lib/slack-notifier.rb', line 15

def initialize webhook_url, options={}, &block
  @endpoint = URI.parse webhook_url

  config.http_client(options.delete(:http_client)) if options.key?(:http_client)
  config.defaults options
  config.instance_exec(&block) if block_given?

  middleware.set config.middleware
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



13
14
15
# File 'lib/slack-notifier.rb', line 13

def endpoint
  @endpoint
end

Instance Method Details

#configObject



25
26
27
# File 'lib/slack-notifier.rb', line 25

def config
  @_config ||= Config.new
end

#ping(message, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/slack-notifier.rb', line 29

def ping message, options={}
  if message.is_a?(Hash)
    options = message
  else
    options[:text] = message
  end

  post options
end

#post(payload = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/slack-notifier.rb', line 39

def post payload={}
  params  = {}
  client  = payload.delete(:http_client) || config.http_client
  payload = config.defaults.merge(payload)

  params[:http_options] = payload.delete(:http_options) if payload.key?(:http_options)
  params[:payload]      = middleware.call(payload).to_json

  client.post endpoint, params
end