Module: Slack::Post
- Defined in:
- lib/slack/post.rb,
lib/slack/post/version.rb
Constant Summary collapse
- DefaultOpts =
{ :channel => '#general' }.freeze
- LegacyConfigParams =
[:subdomain, :token].freeze
- KnownConfigParams =
[:webhook_url, :username, :channel, :subdomain, :token, :icon_url, :icon_emoji].freeze
- AttachmentParams =
[:fallback, :text, :pretext, :color, :fields, :image_url, :mrkdwn_in].freeze
- FieldParams =
[:title, :value, :short].freeze
- VERSION =
"0.3.5"
Class Method Summary collapse
- .config ⇒ Object
- .configure(opts) ⇒ Object
- .configured?(channel_was_overriden = false) ⇒ Boolean
- .post(message, chan = nil, opts = {}) ⇒ Object
- .post_url ⇒ Object
- .post_with_attachments(message, attachments, chan = nil, opts = {}) ⇒ Object
- .prune(opts, allowed_elements = KnownConfigParams) ⇒ Object
- .symbolize_keys(hash) ⇒ Object
- .validated_attachment(attachment) ⇒ Object
Class Method Details
.config ⇒ Object
79 80 81 |
# File 'lib/slack/post.rb', line 79 def self.config @config ||= {} end |
.configure(opts) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/slack/post.rb', line 83 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) ⇒ Boolean
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/slack/post.rb', line 67 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 = {}) ⇒ Object
57 58 59 |
# File 'lib/slack/post.rb', line 57 def self.post(, chan = nil, opts = {}) (, [], chan, opts) end |
.post_url ⇒ Object
61 62 63 |
# File 'lib/slack/post.rb', line 61 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 = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/slack/post.rb', line 14 def self.(, , chan = nil, opts = {}) fail "Slack::Post.configure was not called or configuration was invalid" unless configured?(chan) pkt = { :channel => chan || config[:channel], :text => } 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 .instance_of?(Array) && != [] pkt[:attachments] = .map { |a| (a) } end uri = URI.parse(post_url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.ssl_version = :TLSv1 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 "Recieved a #{resp.code} response while trying to post. Response body: #{resp.body}" end end |
.prune(opts, allowed_elements = KnownConfigParams) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/slack/post.rb', line 95 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) ⇒ Object
105 106 107 |
# File 'lib/slack/post.rb', line 105 def self.symbolize_keys(hash) return hash.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo } end |
.validated_attachment(attachment) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/slack/post.rb', line 49 def self.() = prune(symbolize_keys(), AttachmentParams) if .key?(:fields) [:fields] = [:fields].map { |h| prune(symbolize_keys(h), FieldParams) } end return end |