class Slackit

To follow

Constants

VERSION

Public Class Methods

new(options = {}) click to toggle source
# File lib/slackit.rb, line 13
def initialize(options = {})
    @webhook_url       = options[:webhook_url]
    @username          = options[:username]
    @channel           = options[:channel]
    @icon_emoji        = options[:icon_emoji]

    @validation_test   = options[:validation_test]
    @validated_webhook = options[:validated_webhook]

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

    if @validation_test
        if valid_webhook(@webhook_url)
            @channel = 'general'
            send_message('This is a validation message')
            "#{@webhook_url} is a valid url".success
        else
            "#{@webhook_url} is NOT a valid url".error
        end
        return
    end

    raise ArgumentError.new("Invalid webhook URL: #{@webhook_url} - should start with https://hooks.slack.com/services/") unless @webhook_url.start_with?('https://hooks.slack.com/services/')

    return if valid_webhook(@webhook_url)

    raise ArgumentError.new("Invalid webhook URL: #{@webhook_url} - please check your configuration") unless valid_webhook(@webhook_url)
end

Public Instance Methods

convert_to_json(json_string) click to toggle source

Convery the string to json

# File lib/slackit.rb, line 89
def convert_to_json(json_string)
    begin
        return JSON.parse(json_string)
    rescue JSON::ParserError
        raise ArgumentError.new('Invalid json')
    end
end
send(text) click to toggle source

Add an alias for backwards compatibility

# File lib/slackit.rb, line 66
def send(text)
    return send_message(text)
end
send_attachment(attachment) click to toggle source

Legacy attachments

# File lib/slackit.rb, line 73
def send_attachment(attachment)
    payload = { 'attachments' => [ convert_to_json(attachment) ] }
    return send_payload(payload)
end
send_block(block) click to toggle source

New shiney blocks

# File lib/slackit.rb, line 81
def send_block(block)
    payload = convert_to_json(block)
    return send_payload(payload)
end
send_message(text) click to toggle source

Raw message

# File lib/slackit.rb, line 55
def send_message(text)
    text = text.gsub('\\n', "\n") # ensure newlines are not escaped

    payload = { 'text' => text }

    return send_payload(payload)
end
send_payload(payload) click to toggle source
# File lib/slackit.rb, line 97
def send_payload(payload)
    headers = { 'Content-Type' => 'application/json' }

    # Add the additional payload items
    payload['icon_emoji'] ||= @icon_emoji
    payload['username'] ||= @username
    payload['channel'] ||= @channel

    begin
        uri = URI.parse(@webhook_url)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        request = Net::HTTP::Post.new(uri.request_uri, headers)
        request.body = payload.to_json
        response = http.request(request)

        return true if response.code == '200'

        raise "Invalid webhook URL: #{@webhook_url} - please check your configuration" if response.code == '301' || response.code == '302'

        raise "Unknown error for webhook URL: #{@webhook_url}" if response.body.empty?

        raise response.body
    rescue Exception => e
        raise e
    end
end
valid_webhook(url) click to toggle source
# File lib/slackit.rb, line 42
def valid_webhook(url)
    uri = URI.parse(url)

    r = Net::HTTP.get_response(uri)

    return false if (r.code == '301') || (r.code == '302')

    return true
end