Sha256: 91b8e10cecdd2140f71ff9646f3a718933321adeda64fc9360d4d7555ea4b6a7

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

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
        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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slackit-1.1.2 lib/slackit.rb
slackit-1.1.1 lib/slackit.rb