slack-post 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86078125f4d37d83582258b66f07212486dbb43f
4
- data.tar.gz: e4f33f59f2e3cb686e647e7512c734d98ab1f106
3
+ metadata.gz: 97dd6da19fa9488df3be2a71619bfcfed46e902a
4
+ data.tar.gz: b5168425d44fa2e27a02254c48c0a6d516fb9ea9
5
5
  SHA512:
6
- metadata.gz: 3a25872cff85e15826d1385b5adff552dc2b5af06f2ab641e770e22685544a9b6127f65fea0a0824c4dd161afd489ab35675368d2decfaf5f11dfa1a31689188
7
- data.tar.gz: da6ea5afcd462abf6c2030b9d4c9e94c3b5cec119dde52ab41bdfd1f1b183d5994cc49d159a40dd23247850bb748dfb81803bde6f0a142cec1ec08a55640c230
6
+ metadata.gz: 15496a4e904cdf047a66d927d8eae901f62ddedfac8b981655bccffca7dc1ad1d06be00ef0a7cf54f22337d7bd57b66757932a05b75c0f7a1ddb6d03b24d7ffc
7
+ data.tar.gz: d350f4b86c2be2f5248492e8b7a96b3fd28b26c444056cfeb87c0fb18615fb1dcd94a6719773e4e103e611550cb45cb543119efaa213407accc9cbecc5872037
data/README.md CHANGED
@@ -25,6 +25,31 @@ Slack::Post.configure(
25
25
  Slack::Post.post "Domo arigato.", '#general'
26
26
  ```
27
27
 
28
+ ### Attachments
29
+
30
+ slack-post supports message attachments per Slack's [Attachment](https://api.slack.com/docs/attachments) specification.
31
+
32
+ Use `Slack::Post.post_with_attachments` to send a message with any number of attachments:
33
+ ```ruby
34
+ attachments = [
35
+ {
36
+ fallback: "Required text summary...",
37
+ text: "Optional text that should appear within the attachment",
38
+ pretext: "Optional text that should appear above the formatted data",
39
+ color: "#36a64f",
40
+ fields: [
41
+ {
42
+ title: "Required Field Title",
43
+ value: "Text value of the field.",
44
+ short: false
45
+ }
46
+ ]
47
+ }
48
+ ]
49
+
50
+ Slack::Post.post_with_attachments "Domo arigato.", attachments, '#general'
51
+ ```
52
+
28
53
  ### slack-post Command
29
54
 
30
55
  slack-post comes with a `slack-post` command so you can send messages from the command line:
data/lib/slack/post.rb CHANGED
@@ -11,7 +11,7 @@ module Slack
11
11
  channel: '#general'
12
12
  }.freeze
13
13
 
14
- def self.post(message,chan=nil,opts={})
14
+ def self.post_with_attachments(message,attachments,chan=nil,opts={})
15
15
  raise "You need to call Slack::Post.configure before trying to send messages." unless configured?(chan.nil?)
16
16
  pkt = {
17
17
  channel: chan || config[:channel],
@@ -26,6 +26,9 @@ module Slack
26
26
  if opts.has_key?(:icon_emoji) or config.has_key?(:icon_emoji)
27
27
  pkt[:icon_emoji] = opts[:icon_emoji] || config[:icon_emoji]
28
28
  end
29
+ if attachments.instance_of?(Array) && attachments != []
30
+ pkt[:attachments] = attachments.map { |a| validated_attachment(a) }
31
+ end
29
32
  uri = URI.parse(post_url)
30
33
  http = Net::HTTP.new(uri.host, uri.port)
31
34
  http.use_ssl = true
@@ -42,6 +45,18 @@ module Slack
42
45
  raise "There was an error while trying to post. Error was: #{resp.body}"
43
46
  end
44
47
  end
48
+
49
+ def self.validated_attachment(attachment)
50
+ valid_attachment = prune(symbolize_keys(attachment), AttachmentParams)
51
+ if attachment.has_key?(:fields)
52
+ valid_attachment[:fields] = attachment[:fields].map { |h| prune(symbolize_keys(h), FieldParams) }
53
+ end
54
+ return valid_attachment
55
+ end
56
+
57
+ def self.post(message,chan=nil,opts={})
58
+ post_with_attachments(message, [], chan, opts)
59
+ end
45
60
 
46
61
  def self.post_url
47
62
  "https://#{config[:subdomain]}.slack.com/services/hooks/incoming-webhook?token=#{config[:token]}"
@@ -65,16 +80,22 @@ module Slack
65
80
  end
66
81
 
67
82
  KnownConfigParams = [:username,:channel,:subdomain,:token,:icon_url,:icon_emoji].freeze
83
+ AttachmentParams = [:fallback,:text,:pretext,:color,:fields].freeze
84
+ FieldParams = [:title,:value,:short].freeze
68
85
 
69
- def self.prune(opts)
86
+ def self.prune(opts, allowed_elements=KnownConfigParams)
70
87
  opts.inject({}) do |acc,(k,v)|
71
88
  k = k.to_sym
72
- if KnownConfigParams.include?(k)
89
+ if allowed_elements.include?(k)
73
90
  acc[k] = v
74
91
  end
75
92
  acc
76
93
  end
77
94
  end
95
+
96
+ def self.symbolize_keys(hash)
97
+ return hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
98
+ end
78
99
 
79
100
  end
80
101
  end
@@ -1,5 +1,5 @@
1
1
  module Slack
2
2
  module Post
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-post
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bragg