Class: Slack::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/slack-notifier.rb,
lib/slack-notifier/version.rb,
lib/slack-notifier/link_formatter.rb,
lib/slack-notifier/default_http_client.rb

Defined Under Namespace

Classes: DefaultHTTPClient, LinkFormatter

Constant Summary collapse

HTML_ESCAPE_REGEXP =
/[&><]/
HTML_ESCAPE =
{ '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;' }
VERSION =
"1.4.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Notifier.



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

def initialize webhook_url, options={}
  @endpoint        = URI.parse webhook_url
  @default_payload = options
end

Instance Attribute Details

#default_payloadObject (readonly)

Returns the value of attribute default_payload.



10
11
12
# File 'lib/slack-notifier.rb', line 10

def default_payload
  @default_payload
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/slack-notifier.rb', line 10

def endpoint
  @endpoint
end

Instance Method Details

#channelObject



40
41
42
# File 'lib/slack-notifier.rb', line 40

def channel
  default_payload[:channel]
end

#channel=(channel) ⇒ Object



44
45
46
# File 'lib/slack-notifier.rb', line 44

def channel= channel
  default_payload[:channel] = channel
end

#escape(text) ⇒ Object



59
60
61
# File 'lib/slack-notifier.rb', line 59

def escape(text)
  text.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE)
end

#http_clientObject



36
37
38
# File 'lib/slack-notifier.rb', line 36

def http_client
  default_payload.fetch :http_client, DefaultHTTPClient
end

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/slack-notifier.rb', line 17

def ping message, options={}
  message      = LinkFormatter.format(message)
  if attachments = options[:attachments] || options["attachments"]
    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).merge(text: message)
  client       = payload.delete(:http_client) || http_client
  http_options = payload.delete(:http_options)

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

  client.post endpoint, params
end

#usernameObject



48
49
50
# File 'lib/slack-notifier.rb', line 48

def username
  default_payload[:username]
end

#username=(username) ⇒ Object



52
53
54
# File 'lib/slack-notifier.rb', line 52

def username= username
  default_payload[:username] = username
end