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

VERSION =
"1.1.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



33
34
35
# File 'lib/slack-notifier.rb', line 33

def channel
  default_payload[:channel]
end

#channel=(channel) ⇒ Object



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

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

#http_clientObject



29
30
31
# File 'lib/slack-notifier.rb', line 29

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
# File 'lib/slack-notifier.rb', line 17

def ping message, options={}
  message      = LinkFormatter.format(message)
  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



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

def username
  default_payload[:username]
end

#username=(username) ⇒ Object



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

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