module Slack::Post

Constants

AttachmentParams
DefaultOpts
FieldParams
KnownConfigParams
LegacyConfigParams
VERSION

Public Class Methods

config() click to toggle source
# File lib/slack/post.rb, line 80
def self.config
        @config ||= {}
end
configure(opts) click to toggle source
# File lib/slack/post.rb, line 84
def self.configure(opts)
        @config = config.merge(prune(opts))

        # If a channel has not been configured, add the default channel
        # unless we are using a webhook_url, which provides its own default channel.
        @config.merge!(DefaultOpts) unless @config[:webhook_url] || @config[:channel]
end
configured?(channel_was_overriden = false) click to toggle source
# File lib/slack/post.rb, line 68
def self.configured?(channel_was_overriden = false)
        # if a channel was not manually specified, then we must have a channel option in the config OR
        # we must be using the webhook_url which provided its own default channel on the Slack-side config.
        return false if !channel_was_overriden && !config[:channel] && !config[:webhook_url]

        # we need _either_ a webhook url or all LegacyConfigParams
        return true if config[:webhook_url]
        LegacyConfigParams.all? do |parm|
                config[parm]
        end
end
post(message, chan = nil, opts = {}) click to toggle source
# File lib/slack/post.rb, line 58
def self.post(message, chan = nil, opts = {})
        post_with_attachments(message, [], chan, opts)
end
post_url() click to toggle source
# File lib/slack/post.rb, line 62
def self.post_url
        config[:webhook_url] || "https://#{config[:subdomain]}.slack.com/services/hooks/incoming-webhook?token=#{config[:token]}"
end
post_with_attachments(message, attachments, chan = nil, opts = {}) click to toggle source
# File lib/slack/post.rb, line 14
def self.post_with_attachments(message, attachments, chan = nil, opts = {})
        fail "Slack::Post.configure was not called or configuration was invalid" unless configured?(chan)
        pkt = {
                :channel => chan || config[:channel],
                :text => message
        }
        if config[:username]
                pkt[:username] = config[:username]
        end
        if opts.key?(:icon_url) || config.key?(:icon_url)
                pkt[:icon_url] = opts[:icon_url] || config[:icon_url]
        end
        if opts.key?(:icon_emoji) || config.key?(:icon_emoji)
                pkt[:icon_emoji] = opts[:icon_emoji] || config[:icon_emoji]
        end
        if attachments.instance_of?(Array) && attachments != []
                pkt[:attachments] = attachments.map { |a| validated_attachment(a) }
        end
        uri = URI.parse(post_url)

        http = Net::HTTP.new(uri.host, uri.port, config[:proxy_host], config[:proxy_port])
        http.use_ssl = true
        http.min_version = OpenSSL::SSL::TLS1_2_VERSION
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        req = Net::HTTP::Post.new(uri.request_uri)
        req.body = Yajl::Encoder.encode(pkt)
        req["Content-Type"] = 'application/json'
        resp = http.request(req)
        case resp
                when Net::HTTPSuccess
                        return true
                else
                        fail "Received a #{resp.code} response while trying to post. Response body: #{resp.body}"
        end
end
prune(opts, allowed_elements = KnownConfigParams) click to toggle source
# File lib/slack/post.rb, line 96
def self.prune(opts, allowed_elements = KnownConfigParams)
        opts.inject({}) do |acc, (k, v)|
                k = k.to_sym
                if allowed_elements.include?(k)
                        acc[k] = v
                end
                acc
        end
end
symbolize_keys(hash) click to toggle source
# File lib/slack/post.rb, line 106
def self.symbolize_keys(hash)
        return hash.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
end
validated_attachment(attachment) click to toggle source
# File lib/slack/post.rb, line 50
def self.validated_attachment(attachment)
        valid_attachment = prune(symbolize_keys(attachment), AttachmentParams)
        if attachment.key?(:fields)
                valid_attachment[:fields] = attachment[:fields].map { |h| prune(symbolize_keys(h), FieldParams) }
        end
        return valid_attachment
end