class Slack::Notifier

Constants

HTML_ESCAPE
HTML_ESCAPE_REGEXP
VERSION

Attributes

default_payload[R]
endpoint[R]

Public Class Methods

new(webhook_url, options={}) click to toggle source
# File lib/slack-notifier.rb, line 12
def initialize webhook_url, options={}
  @endpoint        = URI.parse webhook_url
  @default_payload = options
end

Public Instance Methods

channel() click to toggle source
# File lib/slack-notifier.rb, line 48
def channel
  default_payload[:channel]
end
channel=(channel) click to toggle source
# File lib/slack-notifier.rb, line 52
def channel= channel
  default_payload[:channel] = channel
end
escape(text) click to toggle source
# File lib/slack-notifier.rb, line 67
def escape(text)
  text.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE)
end
http_client() click to toggle source
# File lib/slack-notifier.rb, line 44
def http_client
  default_payload.fetch :http_client, DefaultHTTPClient
end
ping(message, options={}) click to toggle source
# File lib/slack-notifier.rb, line 17
def ping message, options={}
  if message.is_a?(Hash)
    message, options = nil, message
  end

  if attachments = options[:attachments] || options["attachments"]
    wrap_array(attachments).each do |attachment|
      ["text", :text].each do |key|
        attachment[key] = LinkFormatter.format(attachment[key]) if attachment.has_key?(key)
      end
    end
  end

  payload      = default_payload.merge(options)
  client       = payload.delete(:http_client) || http_client
  http_options = payload.delete(:http_options)

  unless message.nil?
    payload.merge!(text: LinkFormatter.format(message))
  end

  params = { payload: payload.to_json }
  params[:http_options] = http_options if http_options

  client.post endpoint, params
end
username() click to toggle source
# File lib/slack-notifier.rb, line 56
def username
  default_payload[:username]
end
username=(username) click to toggle source
# File lib/slack-notifier.rb, line 60
def username= username
  default_payload[:username] = username
end
wrap_array(object) click to toggle source
# File lib/slack-notifier.rb, line 71
def wrap_array(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end