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, :proxy_host, :proxy_port].freeze
- AttachmentParams =
[:fallback, :title, :title_link, :author_name, :author_link, :author_icon, :image_url, :thumb_url, :text, :pretext, :color, :fields, :footer, :footer_icon, :ts, :mrkdwn_in].freeze
- FieldParams =
[:title, :value, :short].freeze
- VERSION =
"0.4.1"
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
80 81 82 |
# File 'lib/slack/post.rb', line 80 def self.config @config ||= {} end |
.configure(opts) ⇒ Object
84 85 86 87 88 89 90 |
# 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) ⇒ Boolean
68 69 70 71 72 73 74 75 76 77 78 |
# 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 = {}) ⇒ Object
58 59 60 |
# File 'lib/slack/post.rb', line 58 def self.post(, chan = nil, opts = {}) (, [], chan, opts) end |
.post_url ⇒ Object
62 63 64 |
# 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 = {}) ⇒ 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 48 |
# 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, 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) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# 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) ⇒ Object
106 107 108 |
# 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) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/slack/post.rb', line 50 def self.() = prune(symbolize_keys(), AttachmentParams) if .key?(:fields) [:fields] = [:fields].map { |h| prune(symbolize_keys(h), FieldParams) } end return end |