slack-post 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -0
- data/lib/slack/post.rb +24 -3
- data/lib/slack/post/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97dd6da19fa9488df3be2a71619bfcfed46e902a
|
4
|
+
data.tar.gz: b5168425d44fa2e27a02254c48c0a6d516fb9ea9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
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
|
data/lib/slack/post/version.rb
CHANGED