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.5.1"

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



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

def channel
  default_payload[:channel]
end

#channel=(channel) ⇒ Object



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

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

#escape(text) ⇒ Object



67
68
69
# File 'lib/slack-notifier.rb', line 67

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

#http_clientObject



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

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
35
36
37
38
39
40
41
42
# 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

#usernameObject



56
57
58
# File 'lib/slack-notifier.rb', line 56

def username
  default_payload[:username]
end

#username=(username) ⇒ Object



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

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

#wrap_array(object) ⇒ Object



71
72
73
74
75
76
77
78
79
# 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