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/at.rb,
lib/slack-notifier/payload_middleware/base.rb,
lib/slack-notifier/payload_middleware/stack.rb,
lib/slack-notifier/payload_middleware/channels.rb,
lib/slack-notifier/payload_middleware/format_message.rb,
lib/slack-notifier/payload_middleware/format_attachments.rb

Defined Under Namespace

Modules: Util Classes: APIError, Config, PayloadMiddleware

Constant Summary collapse

VERSION =

rubocop:disable Style/RedundantFreeze

"2.4.0".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Notifier.



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

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.



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

def endpoint
  @endpoint
end

Instance Method Details

#configObject



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

def config
  @_config ||= Config.new
end

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



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

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

  post options
end

#post(payload = {}) ⇒ Object



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

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)

  middleware.call(payload).map do |pld|
    params[:payload] = pld.to_json
    client.post endpoint, params
  end
end