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].freeze
FieldParams =
[:title,:value,:short].freeze
VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.configObject



77
78
79
# File 'lib/slack/post.rb', line 77

def self.config
  @config ||= DefaultOpts
end

.configure(opts) ⇒ Object



81
82
83
# File 'lib/slack/post.rb', line 81

def self.configure(opts)
  @config = config.merge(prune(opts))
end

.configured?(needs_channel = true) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'lib/slack/post.rb', line 67

def self.configured?(needs_channel=true)
  return false if needs_channel and !config[:channel]

  # 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(message,chan=nil,opts={})
  post_with_attachments(message, [], chan, opts)
end

.post_urlObject



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.post_with_attachments(message,attachments,chan=nil,opts={})
  raise "You need to call Slack::Post.configure before trying to send messages." unless configured?(chan.nil?)
  pkt = {
    channel: chan || config[:channel],
    text: message,
  }
  if config[:username]
    pkt[:username] = config[:username]
  end
  if opts.has_key?(:icon_url) or config.has_key?(:icon_url)
    pkt[:icon_url] = opts[:icon_url] || config[:icon_url]
  end
  if opts.has_key?(:icon_emoji) or config.has_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)
  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
      raise "There was an error while trying to post. Error was: #{resp.body}"
  end
end

.prune(opts, allowed_elements = KnownConfigParams) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/slack/post.rb', line 89

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



99
100
101
# File 'lib/slack/post.rb', line 99

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.validated_attachment(attachment)
  valid_attachment = prune(symbolize_keys(attachment), AttachmentParams)
  if attachment.has_key?(:fields)
    valid_attachment[:fields] = attachment[:fields].map { |h| prune(symbolize_keys(h), FieldParams) }
  end
  return valid_attachment
end