Sha256: 1ac9b8659b9b4d1003bc35ae2b0789935646efb10c2c64406cee47285ea6dddc

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require 'slackit/version'
require 'httparty'

#
# To follow
#
class Slackit
    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

    # sends a notification
    # returns true after a successfull pust
    def send(text)
        # send as json
        headers = { 'Content-Type' => 'application/json' }

        # payload
        text = text.gsub('\\n', "\n") # ensure newlines are not escaped
        body = { 'text' => text, 'icon_emoji' => @icon_emoji, 'username' => @username }

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

        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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slackit-1.1.8 lib/slackit.rb
slackit-1.1.7 lib/slackit.rb
slackit-1.1.6 lib/slackit.rb