slack-poster 1.0.1 → 2.2.2
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 +5 -5
- data/.rspec +3 -0
- data/.rubocop.yml +33 -0
- data/.ruby-version +1 -1
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/README.md +117 -16
- data/Rakefile +7 -0
- data/bin/console +9 -0
- data/lib/{slack-poster → slack}/attachment.rb +11 -11
- data/lib/{slack-poster → slack}/field.rb +2 -0
- data/lib/{slack-poster → slack}/message.rb +2 -0
- data/lib/slack/poster.rb +69 -0
- data/lib/slack/version.rb +5 -0
- data/slack-poster.gemspec +16 -10
- data/spec/attachment_spec.rb +45 -0
- data/spec/cassettes/default_post.yml +44 -0
- data/spec/field_spec.rb +8 -0
- data/spec/message_spec.rb +57 -0
- data/spec/poster_spec.rb +71 -0
- data/spec/spec_helper.rb +36 -0
- metadata +107 -31
- data/.ruby-gemset +0 -1
- data/lib/slack-poster.rb +0 -9
- data/lib/slack-poster/author.rb +0 -25
- data/lib/slack-poster/poster.rb +0 -61
- data/lib/slack-poster/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 041fb7e0aaa80515a521a7383b58ebc983e9eb2d7338bc6e17a0a465272cbcef
|
4
|
+
data.tar.gz: 7eb17386a6a4e44a9c3ac049fc48cb6382b9232a2e7a2a07d2501cd3a0606ef4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b780b40dc2a3492b1bceeffecfe00f4372e1a6c834fadebf5b4e735ef3919d7f5718829901dd08d7d5cbdd374535f4eea0a1b11be16d13580eff4e07ede2338
|
7
|
+
data.tar.gz: cf7d90028bbeba198109c7fdfdf634cea4af58693154e91a0012b31b322a54c0fb09ecd740ac0b61d36742964f0fd6193a75648771bd5c3a48d26e396a1922bb
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Style/Documentation:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Layout/LineLength:
|
5
|
+
Max: 100
|
6
|
+
|
7
|
+
Metrics/BlockLength:
|
8
|
+
Exclude:
|
9
|
+
- 'spec/**/*_spec.rb'
|
10
|
+
|
11
|
+
Style/ExpandPathArguments:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Layout/SpaceAroundMethodCallOperator:
|
15
|
+
Enabled: true
|
16
|
+
|
17
|
+
Lint/RaiseException:
|
18
|
+
Enabled: true
|
19
|
+
|
20
|
+
Lint/StructNewOverride:
|
21
|
+
Enabled: true
|
22
|
+
|
23
|
+
Style/ExponentialNotation:
|
24
|
+
Enabled: true
|
25
|
+
|
26
|
+
Style/HashEachMethods:
|
27
|
+
Enabled: true
|
28
|
+
|
29
|
+
Style/HashTransformKeys:
|
30
|
+
Enabled: true
|
31
|
+
|
32
|
+
Style/HashTransformValues:
|
33
|
+
Enabled: true
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.5.8
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
[](https://travis-ci.org/rikas/slack-poster)
|
2
2
|
|
3
3
|
# Slack Poster
|
4
4
|
|
5
|
-
[Slack](https://slack.com/) is a mashup of chatrooms and collaborative sharing tools that are meant to do away with redundant conversations in multiple places.
|
6
|
-
|
7
5
|
slack-poster is a simple gem to make your integration with Slack easier. It supports only incoming communications (from you to Slack).
|
8
6
|
|
9
7
|
## Installation
|
@@ -26,21 +24,29 @@ Or install it yourself as:
|
|
26
24
|
$ gem install slack-poster
|
27
25
|
```
|
28
26
|
|
29
|
-
##
|
27
|
+
## Slack setup
|
30
28
|
|
31
|
-
First, you need to create
|
29
|
+
This gem will use a Incoming WebHook integration on Slack. First, you need to create a new Incoming Webhook integration at `https://team-name.slack.com/services/new/incoming-webhook` (where "team-name" should be your own team name).
|
32
30
|
|
33
|
-
|
31
|
+

|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
TOKEN = 'hd7heoo2oijd0'
|
33
|
+
Hit "Add Incoming WebHooks Integration" and go to the next screen. Here you can add a name to your
|
34
|
+
integration, customize the username that will post and the icon.
|
38
35
|
|
39
|
-
|
40
|
-
|
36
|
+

|
37
|
+
|
38
|
+
Copy the Webhook URL, because you'll need it. Click "Save Settings" and you're done.
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
First you have to initialize your poster and then you can use `send_message` to send your message.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
46
|
+
poster.send_message('Hello world!')
|
41
47
|
```
|
42
48
|
|
43
|
-
You can use an options array if you don't want to use the settings
|
49
|
+
You can use an options array if you don't want to use the default settings:
|
44
50
|
|
45
51
|
```ruby
|
46
52
|
options = {
|
@@ -51,14 +57,109 @@ options = {
|
|
51
57
|
}
|
52
58
|
```
|
53
59
|
|
54
|
-
And then use it as a
|
60
|
+
And then use it as a second parameter. Note that every option is optional (no pun intended!).
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL, options)
|
64
|
+
poster.send_message('Hello with options')
|
65
|
+
# posts message 'Hello with options' to #random with the username 'Tester'
|
66
|
+
```
|
67
|
+
|
68
|
+
Or you can change the options whenever you want
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
72
|
+
poster.username = 'TestUser'
|
73
|
+
poster.icon_emoji = ':ghost:'
|
74
|
+
poster.send_message('Hello World') # => "ok"
|
75
|
+
# posts message 'Hello World' to #random with the username 'TestUser' and ghost emoji as avatar
|
76
|
+
```
|
77
|
+
|
78
|
+
You can also send a `Slack::Message` object instead of `String`:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
message = Slack::Message.new('hello slack')
|
82
|
+
poster.send_message(message)
|
83
|
+
```
|
84
|
+
|
85
|
+
### Message attachments
|
86
|
+
|
87
|
+
Slack Poster supports message attachments. To do so you have to create `Slack::Attachment` objects
|
88
|
+
and then attach them to a `Slack::Message` object. Read the [official documentation](https://api.slack.com/docs/attachments) to understand how attachments work and see different examples of attachments in practice.
|
89
|
+
|
90
|
+
You can build a basic attachment and them attach it to a message and use a poster to post that
|
91
|
+
message for you:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
# As an alternative you could also initialize the attachment with all the fields
|
95
|
+
# Slack::Attachment.new(fallback: 'fallback', pretext: 'pretext', ....)
|
96
|
+
attachment = Slack::Attachment.new
|
97
|
+
|
98
|
+
# This text will be used in clients that don't show formatted text (eg. IRC, mobile notifications)
|
99
|
+
attachment.fallback = "You wouldn't believe who's a mighty pirate!"
|
100
|
+
|
101
|
+
attachment.pretext = 'Arrrgh'
|
102
|
+
attachment.color = 'danger' # good, warning, danger, or any hex color code (eg. #439FE0)
|
103
|
+
|
104
|
+
attachment.image_url = 'https://pbs.twimg.com/profile_images/446438045945180162/KH34Nkuq.jpeg'
|
105
|
+
# you can use thumb_url instead for a small thumb pulled to the right
|
106
|
+
|
107
|
+
attachment.text = 'To become a mighty pirate Guybrush had to go through the three trials.'
|
108
|
+
attachment.title = 'Mighty pirate'
|
109
|
+
attachment.title_link = 'http://scummbar.com/'
|
110
|
+
|
111
|
+
# If you want to include author information you can use the following methods:
|
112
|
+
# attachment.author_name
|
113
|
+
# attachment.author_icon
|
114
|
+
# attachment.author_link
|
115
|
+
# Check Slack documentation for more info.
|
116
|
+
|
117
|
+
message = Slack::Message.new('Monkey island news', attachment)
|
118
|
+
|
119
|
+
# You can also add the attachment after initializing the message:
|
120
|
+
# message = Slack::Message.new('Monkey island news')
|
121
|
+
# message.attachments << attachment
|
122
|
+
|
123
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
124
|
+
poster.username = 'Monkey Island Daily'
|
125
|
+
poster.icon_emoji = ':monkey_face:'
|
126
|
+
poster.send_message(message) # => "ok"
|
127
|
+
```
|
128
|
+
|
129
|
+
This will create a message like this one:
|
130
|
+
|
131
|
+

|
132
|
+
|
133
|
+
#### Fields
|
134
|
+
|
135
|
+
Slack attachment can contain fields that will be displayed in a table inside the message attachment.
|
136
|
+
You can add fields with `Slack::Attachment#add_field`:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
attachment = Slack::Attachment.new
|
140
|
+
attachment.add_field('Name1', 'Value1')
|
141
|
+
attachment.add_field('Name2', 'Value2')
|
142
|
+
attachment.add_field('Name3', 'Value3')
|
143
|
+
attachment.add_field('Name4', 'Value4')
|
144
|
+
|
145
|
+
message = Slack::Message.new('What a beautiful table of names and values', attachment)
|
146
|
+
|
147
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
148
|
+
poster.send_message(message) # => "ok"
|
149
|
+
```
|
150
|
+
|
151
|
+
You may notice that the message will display with a vertical list of fields. You can have them side
|
152
|
+
by side giving a third boolean parameter (is it a short field?).
|
55
153
|
|
56
154
|
```ruby
|
57
|
-
|
58
|
-
|
155
|
+
attachment = Slack::Attachment.new
|
156
|
+
attachment.add_field('Name1', 'Value1', true)
|
157
|
+
attachment.add_field('Name2', 'Value2', true)
|
158
|
+
attachment.add_field('Name3', 'Value3')
|
159
|
+
attachment.add_field('Name4', 'Value4')
|
59
160
|
```
|
60
161
|
|
61
|
-
|
162
|
+
This way the first two field will be displayed in the same row.
|
62
163
|
|
63
164
|
## Contributing
|
64
165
|
|
data/Rakefile
CHANGED
data/bin/console
ADDED
@@ -1,28 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Slack
|
2
4
|
class Attachment
|
3
|
-
ATTRIBUTES = %i
|
5
|
+
ATTRIBUTES = %i[
|
6
|
+
fallback text title title_link image_url thumb_url color pretext author
|
7
|
+
author_name author_link author_icon
|
8
|
+
].freeze
|
4
9
|
|
5
10
|
ATTRIBUTES.each do |attribute|
|
6
11
|
attr_accessor attribute
|
7
12
|
end
|
8
13
|
|
9
|
-
attr_reader :fields
|
14
|
+
attr_reader :fields
|
10
15
|
|
11
16
|
def initialize(options = {})
|
12
17
|
@fields = []
|
13
18
|
|
14
19
|
ATTRIBUTES.each do |attribute|
|
15
|
-
|
20
|
+
send("#{attribute}=", options.delete(attribute))
|
16
21
|
end
|
17
22
|
end
|
18
23
|
|
19
24
|
def add_field(title, value, short = false)
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
def author=(author)
|
25
|
-
@author = author
|
25
|
+
fields << Field.new(title, value, short)
|
26
26
|
end
|
27
27
|
|
28
28
|
def as_json
|
@@ -32,8 +32,8 @@ module Slack
|
|
32
32
|
hash[attribute] = send(attribute) if send(attribute)
|
33
33
|
end
|
34
34
|
|
35
|
-
hash
|
36
|
-
hash
|
35
|
+
hash[:fields] = fields.map(&:as_json) unless fields.empty?
|
36
|
+
hash[:author] = author.as_json if author
|
37
37
|
|
38
38
|
hash
|
39
39
|
end
|
data/lib/slack/poster.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'slack/version'
|
4
|
+
require 'slack/field'
|
5
|
+
require 'slack/message'
|
6
|
+
require 'slack/attachment'
|
7
|
+
require 'json'
|
8
|
+
require 'faraday'
|
9
|
+
|
10
|
+
module Slack
|
11
|
+
class Poster
|
12
|
+
attr_accessor :options
|
13
|
+
|
14
|
+
# Define getters and setters for the options hash keys. This will make assign of the options
|
15
|
+
# more flexible.
|
16
|
+
%i[username channel icon_url icon_emoji].each do |option_attr|
|
17
|
+
define_method(option_attr) { @options[option_attr] }
|
18
|
+
define_method("#{option_attr}=") { |value| @options[option_attr] = value }
|
19
|
+
end
|
20
|
+
|
21
|
+
# Initializes a Poster instance to post messages with an incoming webhook URL.
|
22
|
+
# It also accepts an options hash. If no options are given then the poster uses the default
|
23
|
+
# configuration from Slack integration.
|
24
|
+
#
|
25
|
+
# ==== Examples
|
26
|
+
#
|
27
|
+
# # Without options
|
28
|
+
# Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va')
|
29
|
+
#
|
30
|
+
# # With options using a custom username and icon avatar
|
31
|
+
# Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va',
|
32
|
+
# username: 'Ricardo',
|
33
|
+
# icon_url: 'http://www.gravatar.com/avatar/92e00fd27c64c94d04140cef88039468.png')
|
34
|
+
#
|
35
|
+
# # You can also use an emoji as avatar
|
36
|
+
# Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va',
|
37
|
+
# username: 'Ricardo',
|
38
|
+
# icon_emoji: 'ghost')
|
39
|
+
def initialize(webhook_url, options = {})
|
40
|
+
@base_uri = webhook_url
|
41
|
+
|
42
|
+
@options = options
|
43
|
+
|
44
|
+
raise ArgumentError, 'Webhook URL is required' if webhook_url.nil?
|
45
|
+
end
|
46
|
+
|
47
|
+
# Sends a message to Slack. The message can be either plain text or a Slack::Message object.
|
48
|
+
#
|
49
|
+
# ==== Examples
|
50
|
+
#
|
51
|
+
# # Plain text message
|
52
|
+
# poster.send_message('hello world')
|
53
|
+
#
|
54
|
+
# # Using a message object
|
55
|
+
# poster.send_message(Slack::Message.new(text: 'hello world'))
|
56
|
+
#
|
57
|
+
# You can have messages with attachments if you build your message with a Slack::Message object
|
58
|
+
# and add Slack::Attachment objects.
|
59
|
+
def send_message(message)
|
60
|
+
body = message.is_a?(String) ? options.merge(text: message) : options.merge(message.as_json)
|
61
|
+
|
62
|
+
conn = Faraday.new(url: @base_uri)
|
63
|
+
|
64
|
+
response = conn.post('', payload: body.to_json)
|
65
|
+
|
66
|
+
response
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/slack-poster.gemspec
CHANGED
@@ -1,26 +1,32 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
|
5
|
+
|
6
|
+
require 'slack/version'
|
5
7
|
|
6
8
|
Gem::Specification.new do |spec|
|
7
9
|
spec.name = 'slack-poster'
|
8
|
-
spec.version = Slack::VERSION
|
10
|
+
spec.version = Slack::VERSION.dup
|
9
11
|
spec.authors = ['Ricardo Otero']
|
10
12
|
spec.email = ['oterosantos@gmail.com']
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
+
spec.summary = 'Slack wrapper for Incoming WebHooks integrations.'
|
14
|
+
spec.description = 'Slack Poster is a gem to make your integration with Slack WebHooks easier.'
|
13
15
|
spec.homepage = 'https://github.com/rikas/slack-poster'
|
14
16
|
spec.license = 'MIT'
|
15
17
|
|
16
18
|
spec.files = `git ls-files -z`.split("\x0")
|
17
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(
|
20
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
21
|
spec.require_paths = ['lib']
|
20
22
|
|
21
|
-
spec.add_development_dependency 'bundler', '
|
22
|
-
spec.add_development_dependency '
|
23
|
-
spec.add_development_dependency '
|
23
|
+
spec.add_development_dependency 'bundler', '> 1.10.0'
|
24
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
25
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.4'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.49', '> 0.35'
|
28
|
+
spec.add_development_dependency 'vcr', '~> 3.0'
|
29
|
+
spec.add_development_dependency 'webmock', '~> 3.8.3'
|
24
30
|
|
25
|
-
spec.
|
31
|
+
spec.add_runtime_dependency 'faraday', '>= 1.0.0'
|
26
32
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Slack::Attachment do
|
6
|
+
describe '.new' do
|
7
|
+
let(:options) do
|
8
|
+
{
|
9
|
+
fallback: 'fallback',
|
10
|
+
text: 'text',
|
11
|
+
title: 'title',
|
12
|
+
title_link: 'title_link',
|
13
|
+
image_url: 'image_url',
|
14
|
+
thumb_url: 'thumb_url',
|
15
|
+
color: 'color',
|
16
|
+
author_name: 'author_name',
|
17
|
+
author_link: 'author_link',
|
18
|
+
author_icon: 'author_icon',
|
19
|
+
pretext: 'pretext'
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'accepts initialization with no attributes' do
|
24
|
+
att = described_class.new
|
25
|
+
|
26
|
+
expect(att).to_not be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'initializes all possible option' do
|
30
|
+
att = described_class.new(options)
|
31
|
+
|
32
|
+
options.each do |key, value|
|
33
|
+
expect(att.send(key)).to eq(value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#add_field' do
|
39
|
+
context 'when short is set to true' do
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#as_json' do
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: https://hooks.slack.com/services/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
|
6
|
+
body:
|
7
|
+
encoding: UTF-8
|
8
|
+
string: payload=%7B%22text%22%3A%22Hello%20world%22%7D
|
9
|
+
headers:
|
10
|
+
Accept-Encoding:
|
11
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
12
|
+
Accept:
|
13
|
+
- "*/*"
|
14
|
+
User-Agent:
|
15
|
+
- Ruby
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Access-Control-Allow-Origin:
|
22
|
+
- "*"
|
23
|
+
Content-Type:
|
24
|
+
- text/html
|
25
|
+
Date:
|
26
|
+
- Sun, 24 May 2015 14:05:58 GMT
|
27
|
+
Server:
|
28
|
+
- Apache
|
29
|
+
Strict-Transport-Security:
|
30
|
+
- max-age=31536000; includeSubDomains; preload
|
31
|
+
Vary:
|
32
|
+
- Accept-Encoding
|
33
|
+
X-Frame-Options:
|
34
|
+
- SAMEORIGIN
|
35
|
+
Content-Length:
|
36
|
+
- '22'
|
37
|
+
Connection:
|
38
|
+
- keep-alive
|
39
|
+
body:
|
40
|
+
encoding: ASCII-8BIT
|
41
|
+
string: ok
|
42
|
+
http_version:
|
43
|
+
recorded_at: Sun, 24 May 2015 14:05:58 GMT
|
44
|
+
recorded_with: VCR 2.9.3
|
data/spec/field_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Slack::Message do
|
6
|
+
let(:message) { described_class.new('hello') }
|
7
|
+
|
8
|
+
it 'can be initialized with an attachment' do
|
9
|
+
attachment = Slack::Attachment.new
|
10
|
+
|
11
|
+
message = described_class.new('hi', attachment)
|
12
|
+
|
13
|
+
expect(message.attachments).to eq([attachment])
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#add_attachment' do
|
17
|
+
it 'adds an attachment correctly' do
|
18
|
+
attachment = Slack::Attachment.new
|
19
|
+
|
20
|
+
message.add_attachment(attachment)
|
21
|
+
|
22
|
+
expect(message.attachments).to eq([attachment])
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'adds multiple attachments correctly' do
|
26
|
+
att1 = Slack::Attachment.new
|
27
|
+
att2 = Slack::Attachment.new
|
28
|
+
|
29
|
+
message.add_attachment(att1)
|
30
|
+
message.add_attachment(att2)
|
31
|
+
|
32
|
+
expect(message.attachments).to eq([att1, att2])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#as_json' do
|
37
|
+
context 'when no attachments were added' do
|
38
|
+
it 'returns the correct json' do
|
39
|
+
expect(message.as_json).to eq(text: 'hello')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when attachments are added' do
|
44
|
+
it 'returns the correct json' do
|
45
|
+
att1 = Slack::Attachment.new(text: 'Att1')
|
46
|
+
att2 = Slack::Attachment.new(title: 'Att2')
|
47
|
+
|
48
|
+
message.add_attachment(att1)
|
49
|
+
message.add_attachment(att2)
|
50
|
+
|
51
|
+
json = { text: 'hello', attachments: [{ text: 'Att1' }, { title: 'Att2' }] }
|
52
|
+
|
53
|
+
expect(message.as_json).to eq(json)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/poster_spec.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
describe Slack::Poster do
|
7
|
+
let(:hook) { ENV.fetch('SLACK_POSTER_TEST_WEBHOOK') }
|
8
|
+
let(:poster) { described_class.new(hook) }
|
9
|
+
|
10
|
+
let(:options) do
|
11
|
+
{
|
12
|
+
username: SecureRandom.hex[0..5],
|
13
|
+
channel: "##{SecureRandom.hex[0..5]}",
|
14
|
+
icon_url: SecureRandom.hex[0..5],
|
15
|
+
icon_emoji: SecureRandom.hex[0..5]
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:with_options) { described_class.new(hook, options) }
|
20
|
+
|
21
|
+
describe '.new' do
|
22
|
+
it 'requires the webhook_url' do
|
23
|
+
expect { described_class.new(nil) }.to raise_error(ArgumentError)
|
24
|
+
expect { described_class.new }.to raise_error(ArgumentError)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'does not need options' do
|
28
|
+
expect { described_class.new('my_webhook') }.to_not raise_exception
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
%i[username channel icon_url icon_emoji].each do |option_attr|
|
33
|
+
describe "#{option_attr}=" do
|
34
|
+
it "sets the #{option_attr} field in options hash" do
|
35
|
+
poster.send("#{option_attr}=", "test_#{option_attr}")
|
36
|
+
|
37
|
+
expect(poster.options[option_attr]).to eq("test_#{option_attr}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe option_attr.to_s do
|
42
|
+
it "gets the #{option_attr} field in options hash" do
|
43
|
+
expect(with_options.send(option_attr)).to eq(with_options.options[option_attr])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#send_message' do
|
49
|
+
context 'with no attachments' do
|
50
|
+
it 'accepts a message object' do
|
51
|
+
VCR.use_cassette('default_post') do
|
52
|
+
message = Slack::Message.new('Hello world')
|
53
|
+
|
54
|
+
response = poster.send_message(message)
|
55
|
+
|
56
|
+
expect(response).to_not be_nil
|
57
|
+
expect(response.env.status).to eq(200)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'accepts a string' do
|
62
|
+
VCR.use_cassette('default_post') do
|
63
|
+
response = poster.send_message('Hello world')
|
64
|
+
|
65
|
+
expect(response).to_not be_nil
|
66
|
+
expect(response.env.status).to eq(200)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
Bundler.require
|
6
|
+
|
7
|
+
# Use the poster test webhook from an env variable
|
8
|
+
ENV['SLACK_POSTER_TEST_WEBHOOK'] ||= 'https://hooks.slack.com/services/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'
|
9
|
+
|
10
|
+
require 'vcr'
|
11
|
+
require 'webmock/rspec'
|
12
|
+
|
13
|
+
WebMock.disable_net_connect!
|
14
|
+
|
15
|
+
VCR.configure do |config|
|
16
|
+
config.cassette_library_dir = 'spec/cassettes'
|
17
|
+
config.hook_into :webmock
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
22
|
+
# be too noisy due to issues in dependencies.
|
23
|
+
config.warnings = true
|
24
|
+
|
25
|
+
# Run specs in random order to surface order dependencies. If you find an
|
26
|
+
# order dependency and want to debug it, you can fix the order by providing
|
27
|
+
# the seed, which is printed after each run.
|
28
|
+
# --seed 1234
|
29
|
+
config.order = :random
|
30
|
+
|
31
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
32
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
33
|
+
# test failures related to randomization by passing the same `--seed` value
|
34
|
+
# as the one that triggered the failure.
|
35
|
+
Kernel.srand config.seed
|
36
|
+
end
|
metadata
CHANGED
@@ -1,98 +1,168 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack-poster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Otero
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.10.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.10.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
33
|
+
version: '0.10'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
40
|
+
version: '0.10'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 12.3.3
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 12.3.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
30
58
|
requirements:
|
31
59
|
- - "~>"
|
32
60
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
61
|
+
version: '3.4'
|
34
62
|
type: :development
|
35
63
|
prerelease: false
|
36
64
|
version_requirements: !ruby/object:Gem::Requirement
|
37
65
|
requirements:
|
38
66
|
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
68
|
+
version: '3.4'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
70
|
+
name: rubocop
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
44
72
|
requirements:
|
45
|
-
- - "
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.49'
|
76
|
+
- - ">"
|
46
77
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
78
|
+
version: '0.35'
|
48
79
|
type: :development
|
49
80
|
prerelease: false
|
50
81
|
version_requirements: !ruby/object:Gem::Requirement
|
51
82
|
requirements:
|
52
|
-
- - "
|
83
|
+
- - "~>"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0.49'
|
86
|
+
- - ">"
|
53
87
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
88
|
+
version: '0.35'
|
55
89
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
90
|
+
name: vcr
|
57
91
|
requirement: !ruby/object:Gem::Requirement
|
58
92
|
requirements:
|
59
93
|
- - "~>"
|
60
94
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0
|
62
|
-
type: :
|
95
|
+
version: '3.0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '3.0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: webmock
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 3.8.3
|
110
|
+
type: :development
|
63
111
|
prerelease: false
|
64
112
|
version_requirements: !ruby/object:Gem::Requirement
|
65
113
|
requirements:
|
66
114
|
- - "~>"
|
67
115
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
69
|
-
|
116
|
+
version: 3.8.3
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: faraday
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 1.0.0
|
124
|
+
type: :runtime
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 1.0.0
|
131
|
+
description: Slack Poster is a gem to make your integration with Slack WebHooks easier.
|
70
132
|
email:
|
71
133
|
- oterosantos@gmail.com
|
72
|
-
executables:
|
134
|
+
executables:
|
135
|
+
- console
|
73
136
|
extensions: []
|
74
137
|
extra_rdoc_files: []
|
75
138
|
files:
|
76
139
|
- ".gitignore"
|
77
|
-
- ".
|
140
|
+
- ".rspec"
|
141
|
+
- ".rubocop.yml"
|
78
142
|
- ".ruby-version"
|
143
|
+
- ".travis.yml"
|
79
144
|
- Gemfile
|
80
145
|
- LICENSE.txt
|
81
146
|
- README.md
|
82
147
|
- Rakefile
|
83
|
-
-
|
84
|
-
- lib/slack
|
85
|
-
- lib/slack
|
86
|
-
- lib/slack
|
87
|
-
- lib/slack
|
88
|
-
- lib/slack
|
89
|
-
- lib/slack-poster/version.rb
|
148
|
+
- bin/console
|
149
|
+
- lib/slack/attachment.rb
|
150
|
+
- lib/slack/field.rb
|
151
|
+
- lib/slack/message.rb
|
152
|
+
- lib/slack/poster.rb
|
153
|
+
- lib/slack/version.rb
|
90
154
|
- slack-poster.gemspec
|
155
|
+
- spec/attachment_spec.rb
|
156
|
+
- spec/cassettes/default_post.yml
|
157
|
+
- spec/field_spec.rb
|
158
|
+
- spec/message_spec.rb
|
159
|
+
- spec/poster_spec.rb
|
160
|
+
- spec/spec_helper.rb
|
91
161
|
homepage: https://github.com/rikas/slack-poster
|
92
162
|
licenses:
|
93
163
|
- MIT
|
94
164
|
metadata: {}
|
95
|
-
post_install_message:
|
165
|
+
post_install_message:
|
96
166
|
rdoc_options: []
|
97
167
|
require_paths:
|
98
168
|
- lib
|
@@ -107,9 +177,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
177
|
- !ruby/object:Gem::Version
|
108
178
|
version: '0'
|
109
179
|
requirements: []
|
110
|
-
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
112
|
-
signing_key:
|
180
|
+
rubyforge_project:
|
181
|
+
rubygems_version: 2.7.6.2
|
182
|
+
signing_key:
|
113
183
|
specification_version: 4
|
114
184
|
summary: Slack wrapper for Incoming WebHooks integrations.
|
115
|
-
test_files:
|
185
|
+
test_files:
|
186
|
+
- spec/attachment_spec.rb
|
187
|
+
- spec/cassettes/default_post.yml
|
188
|
+
- spec/field_spec.rb
|
189
|
+
- spec/message_spec.rb
|
190
|
+
- spec/poster_spec.rb
|
191
|
+
- spec/spec_helper.rb
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
slack-poster
|
data/lib/slack-poster.rb
DELETED
data/lib/slack-poster/author.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Slack
|
2
|
-
class Author
|
3
|
-
ATTRIBUTES = %i(name link icon)
|
4
|
-
|
5
|
-
ATTRIBUTES.each do |attribute|
|
6
|
-
attr_accessor attribute
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize(options = {})
|
10
|
-
ATTRIBUTES.each do |attribute|
|
11
|
-
self.send("#{attribute}=", options.delete(attribute))
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def as_json
|
16
|
-
hash = {}
|
17
|
-
|
18
|
-
ATTRIBUTES.each do |attribute|
|
19
|
-
hash[attribute] = send(attribute) if send(attribute)
|
20
|
-
end
|
21
|
-
|
22
|
-
hash
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
data/lib/slack-poster/poster.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'httparty'
|
2
|
-
|
3
|
-
module Slack
|
4
|
-
class Poster
|
5
|
-
include HTTParty
|
6
|
-
|
7
|
-
attr_accessor :url, :options, :attachments
|
8
|
-
|
9
|
-
# The format of the response. This comes back as 'ok' from slack.
|
10
|
-
format :plain
|
11
|
-
|
12
|
-
# Disable the use of rails query string format.
|
13
|
-
#
|
14
|
-
# With rails query string format enabled:
|
15
|
-
# => get '/', :query => {:selected_ids => [1,2,3]}
|
16
|
-
#
|
17
|
-
# Would translate to this:
|
18
|
-
# => /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
|
19
|
-
#
|
20
|
-
disable_rails_query_string_format
|
21
|
-
|
22
|
-
# Initializes a Poster instance to post messages. It uses the HTTParty.base_uri and
|
23
|
-
# HTTParty.default_params to set parameters of POST request.
|
24
|
-
# It also accepts a configuration hash.
|
25
|
-
#
|
26
|
-
# ==== Examples
|
27
|
-
#
|
28
|
-
# Without configuration hash
|
29
|
-
# => Slack::Poster.new('myteam', 'eNmZHQY6f591ziHyZdzePFz8')
|
30
|
-
#
|
31
|
-
# With configuration using username and icon avatar:
|
32
|
-
# => Slack::Poster.new('myteam', 'eNmZHQY6f591ziHyZdzePFz8', username: 'Ricardo',
|
33
|
-
# icon_url: 'http://www.gravatar.com/avatar/92e00fd27c64c94d04140cef88039468.png')
|
34
|
-
#
|
35
|
-
# You can also use an emoji as avatar:
|
36
|
-
# => Slack::Poster.new('myteam', 'eNmZHQY6f591ziHyZdzePFz8', username: 'Ricardo',
|
37
|
-
# icon_emoji: 'ghost')
|
38
|
-
#
|
39
|
-
def initialize(webhook_url, options = {})
|
40
|
-
self.class.base_uri(webhook_url)
|
41
|
-
|
42
|
-
@attachments = []
|
43
|
-
|
44
|
-
@options = options
|
45
|
-
|
46
|
-
raise ArgumentError, 'Webhook URL is required' if webhook_url.nil?
|
47
|
-
end
|
48
|
-
|
49
|
-
def send_message(message)
|
50
|
-
body = message.is_a?(String) ? options.merge(text: message) : options.merge(message.as_json)
|
51
|
-
|
52
|
-
attach_extras(body) unless attachments.empty?
|
53
|
-
|
54
|
-
response = self.class.post('', { body: { payload: body.to_json }})
|
55
|
-
|
56
|
-
puts body.to_json
|
57
|
-
|
58
|
-
"#{response.body} (#{response.code})"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
data/lib/slack-poster/version.rb
DELETED