class NineOneOne::SlackService

Attributes

channel[R]
http[R]
path[R]
username[R]

Public Class Methods

new(webhook_url, opts = {}) click to toggle source
# File lib/nine_one_one/slack_service.rb, line 3
def initialize(webhook_url, opts = {})
  uri = URI(webhook_url)

  @channel = opts[:channel]
  @http = opts[:http] || Http.new(uri.host, uri.scheme)
  @path = uri.path
  @username = opts[:username]
end

Public Instance Methods

notify(message) click to toggle source
# File lib/nine_one_one/slack_service.rb, line 12
def notify(message)
  body = request_body(message)
  headers = { 'Content-Type' => 'application/json' }
  http.post(path, body, headers)
end

Private Instance Methods

request_body(message) click to toggle source
# File lib/nine_one_one/slack_service.rb, line 22
def request_body(message)
  body = message.is_a?(Hash) ? message : { text: message }
  body[:channel] = channel unless channel.nil?
  body[:username] = username unless username.nil?

  body.to_json
end