Class: Slackit

Inherits:
Object
  • Object
show all
Defined in:
lib/slackit.rb,
lib/slackit/version.rb

Overview

To follow

Constant Summary collapse

VERSION =
'1.1.1'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Slackit

Returns a new instance of Slackit.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
# File 'lib/slackit.rb', line 8

def initialize(options = {})
    @webhook_url  = options[:webhook_url]
    @username     = options[:username]
    @channel      = options[:channel]
    @icon_emoji   = options[:icon_emoji]

    raise ArgumentError.new('Webhook URL required') if @webhook_url.nil?
end

Instance Method Details

#send(text) ⇒ Object

sends a notification returns true after a successfull pust



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/slackit.rb', line 19

def send(text)
    # send as json
    headers = { 'Content-Type' => 'application/json' }

    # payload
    body = { 'text' => text, 'icon_emoji' => @icon_emoji, 'username' => @username }

    # add the channel if there is one otherwise the default channel
    body['channel'] = @channel || '#general'

    begin
        response = HTTParty.post(@webhook_url, body: body.to_json, headers: headers)

        return true if response.code == 200

        return false
    rescue HTTParty::Error, SocketError => _e
        return false
    end
end