slack-poster 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +3 -0
- data/.rubocop.yml +5 -0
- data/.ruby-version +1 -1
- data/.travis.yml +7 -0
- data/README.md +117 -15
- data/Rakefile +5 -0
- data/bin/console +7 -0
- data/lib/slack-poster/attachment.rb +7 -9
- data/lib/slack-poster/poster.rb +36 -23
- data/lib/slack-poster/version.rb +1 -1
- data/lib/{slack-poster.rb → slack_poster.rb} +0 -4
- data/slack-poster.gemspec +11 -6
- data/spec/attachment_spec.rb +43 -0
- data/spec/cassettes/default_post.yml +44 -0
- data/spec/field_spec.rb +6 -0
- data/spec/message_spec.rb +55 -0
- data/spec/poster_spec.rb +68 -0
- data/spec/spec_helper.rb +108 -0
- metadata +119 -17
- data/.ruby-gemset +0 -1
- data/lib/slack-poster/author.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c7479325afff0e574ff8589b13fe01fd3229975
|
4
|
+
data.tar.gz: 442f9ee35a4f8e9e35c58836ae4162145f9efc8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe8ecdbecc1287a776c3288633550e646f8560ea0be0823664f041ba35f9e49e7d389babf2fb4d47193fc3cb0263e7dc7c4f7b2faa31c1fe41cf0470241b0fd8
|
7
|
+
data.tar.gz: 2941acd2b4fd0dbdf07479fd449f562e4f702ef21445c681279c8463211a7c8db5cb405be2c5fff606821ac9b32081dc7416a323200734850c0a8cec195b3bec
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.2.
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
[![Dependency Status](https://gemnasium.com/rikas/slack-poster.svg)](https://gemnasium.com/rikas/slack-poster)
|
2
|
+
[![Build Status](https://travis-ci.org/rikas/slack-poster.svg?branch=master)](https://travis-ci.org/rikas/slack-poster)
|
2
3
|
|
3
4
|
# Slack Poster
|
4
5
|
|
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
6
|
slack-poster is a simple gem to make your integration with Slack easier. It supports only incoming communications (from you to Slack).
|
8
7
|
|
9
8
|
## Installation
|
@@ -26,21 +25,29 @@ Or install it yourself as:
|
|
26
25
|
$ gem install slack-poster
|
27
26
|
```
|
28
27
|
|
29
|
-
##
|
28
|
+
## Slack setup
|
30
29
|
|
31
|
-
First, you need to create
|
30
|
+
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
31
|
|
33
|
-
|
32
|
+
![](http://cl.ly/image/2D2Y0x2B2847/slack_setup1.png)
|
34
33
|
|
35
|
-
|
36
|
-
|
37
|
-
TOKEN = 'hd7heoo2oijd0'
|
34
|
+
Hit "Add Incoming WebHooks Integration" and go to the next screen. Here you can add a name to your
|
35
|
+
integration, customize the username that will post and the icon.
|
38
36
|
|
39
|
-
|
40
|
-
|
37
|
+
![](http://cl.ly/image/2s3r343K0w3T/slack_setup2.png)
|
38
|
+
|
39
|
+
Copy the Webhook URL, because you'll need it. Click "Save Settings" and you're done.
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
First you have to initialize your poster and then you can use `send_message` to send your message.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
47
|
+
poster.send_message('Hello world!')
|
41
48
|
```
|
42
49
|
|
43
|
-
You can use an options array if you don't want to use the settings
|
50
|
+
You can use an options array if you don't want to use the default settings:
|
44
51
|
|
45
52
|
```ruby
|
46
53
|
options = {
|
@@ -51,14 +58,109 @@ options = {
|
|
51
58
|
}
|
52
59
|
```
|
53
60
|
|
54
|
-
And then use it as a
|
61
|
+
And then use it as a second parameter. Note that every option is optional (no pun intended!).
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL, options)
|
65
|
+
poster.send_message('Hello with options')
|
66
|
+
# posts message 'Hello with options' to #random with the username 'Tester'
|
67
|
+
```
|
68
|
+
|
69
|
+
Or you can change the options whenever you want
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
73
|
+
poster.username = 'TestUser'
|
74
|
+
poster.icon_emoji = ':ghost:'
|
75
|
+
poster.send_message('Hello World') # => "ok"
|
76
|
+
# posts message 'Hello World' to #random with the username 'TestUser' and ghost emoji as avatar
|
77
|
+
```
|
78
|
+
|
79
|
+
You can also send a `Slack::Message` object instead of `String`:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
message = Slack::Message.new('hello slack')
|
83
|
+
poster.send_message(message)
|
84
|
+
```
|
85
|
+
|
86
|
+
### Message attachments
|
87
|
+
|
88
|
+
Slack Poster supports message attachments. To do so you have to create `Slack::Attachment` objects
|
89
|
+
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.
|
90
|
+
|
91
|
+
You can build a basic attachment and them attach it to a message and use a poster to post that
|
92
|
+
message for you:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
# As an alternative you could also initialize the attachment with all the fields
|
96
|
+
# Slack::Attachment.new(fallback: 'fallback', pretext: 'pretext', ....)
|
97
|
+
attachment = Slack::Attachment.new
|
98
|
+
|
99
|
+
# This text will be used in clients that don't show formatted text (eg. IRC, mobile notifications)
|
100
|
+
attachment.fallback = "You wouldn't believe who's a mighty pirate!"
|
101
|
+
|
102
|
+
attachment.pretext = 'Arrrgh'
|
103
|
+
attachment.color = 'danger' # good, warning, danger, or any hex color code (eg. #439FE0)
|
104
|
+
|
105
|
+
attachment.image_url = 'https://pbs.twimg.com/profile_images/446438045945180162/KH34Nkuq.jpeg'
|
106
|
+
# you can use thumb_url instead for a small thumb pulled to the right
|
107
|
+
|
108
|
+
attachment.text = 'To become a mighty pirate Guybrush had to go through the three trials.'
|
109
|
+
attachment.title = 'Mighty pirate'
|
110
|
+
attachment.title_link = 'http://scummbar.com/'
|
111
|
+
|
112
|
+
# If you want to include author information you can use the following methods:
|
113
|
+
# attachment.author_name
|
114
|
+
# attachment.author_icon
|
115
|
+
# attachment.author_link
|
116
|
+
# Check Slack documentation for more info.
|
117
|
+
|
118
|
+
message = Slack::Message.new('Monkey island news', attachment)
|
119
|
+
|
120
|
+
# You can also add the attachment after initializing the message:
|
121
|
+
# message = Slack::Message.new('Monkey island news')
|
122
|
+
# message.attachments << attachment
|
123
|
+
|
124
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
125
|
+
poster.username = 'Monkey Island Daily'
|
126
|
+
poster.icon_emoji = ':monkey_face:'
|
127
|
+
poster.send_message(message) # => "ok"
|
128
|
+
```
|
129
|
+
|
130
|
+
This will produce a message like this one:
|
131
|
+
|
132
|
+
![](http://f.cl.ly/items/2S0E03450f2d3h2Y2w26/guybrush1.png)
|
133
|
+
|
134
|
+
#### Fields
|
135
|
+
|
136
|
+
Slack attachment can contain fields that will be displayed in a table inside the message attachment.
|
137
|
+
You can add fields with `Slack::Attachment#add_field`:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
attachment = Slack::Attachment.new
|
141
|
+
attachment.add_field('Name1', 'Value1')
|
142
|
+
attachment.add_field('Name2', 'Value2')
|
143
|
+
attachment.add_field('Name3', 'Value3')
|
144
|
+
attachment.add_field('Name4', 'Value4')
|
145
|
+
|
146
|
+
message = Slack::Message.new('What a beautiful table of names and values', attachment)
|
147
|
+
|
148
|
+
poster = Slack::Poster.new(YOUR_WEBHOOK_URL)
|
149
|
+
poster.send_message(message) # => "ok"
|
150
|
+
```
|
151
|
+
|
152
|
+
You may notice that the message will display with a vertical list of fields. You can have them side
|
153
|
+
by side giving a third boolean parameter (is it a short field?).
|
55
154
|
|
56
155
|
```ruby
|
57
|
-
|
58
|
-
|
156
|
+
attachment = Slack::Attachment.new
|
157
|
+
attachment.add_field('Name1', 'Value1', true)
|
158
|
+
attachment.add_field('Name2', 'Value2', true)
|
159
|
+
attachment.add_field('Name3', 'Value3')
|
160
|
+
attachment.add_field('Name4', 'Value4')
|
59
161
|
```
|
60
162
|
|
61
|
-
|
163
|
+
This way the first two field will be displayed in the same row.
|
62
164
|
|
63
165
|
## Contributing
|
64
166
|
|
data/Rakefile
CHANGED
data/bin/console
ADDED
@@ -1,28 +1,26 @@
|
|
1
1
|
module Slack
|
2
2
|
class Attachment
|
3
|
-
ATTRIBUTES =
|
3
|
+
ATTRIBUTES = [
|
4
|
+
:fallback, :text, :title, :title_link, :image_url, :thumb_url, :color, :pretext, :author,
|
5
|
+
:author_name, :author_link, :author_icon
|
6
|
+
]
|
4
7
|
|
5
8
|
ATTRIBUTES.each do |attribute|
|
6
9
|
attr_accessor attribute
|
7
10
|
end
|
8
11
|
|
9
|
-
attr_reader :fields
|
12
|
+
attr_reader :fields
|
10
13
|
|
11
14
|
def initialize(options = {})
|
12
15
|
@fields = []
|
13
16
|
|
14
17
|
ATTRIBUTES.each do |attribute|
|
15
|
-
|
18
|
+
send("#{attribute}=", options.delete(attribute))
|
16
19
|
end
|
17
20
|
end
|
18
21
|
|
19
22
|
def add_field(title, value, short = false)
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
def author=(author)
|
25
|
-
@author = author
|
23
|
+
fields << Field.new(title, value, short)
|
26
24
|
end
|
27
25
|
|
28
26
|
def as_json
|
data/lib/slack-poster/poster.rb
CHANGED
@@ -4,7 +4,7 @@ module Slack
|
|
4
4
|
class Poster
|
5
5
|
include HTTParty
|
6
6
|
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :options
|
8
8
|
|
9
9
|
# The format of the response. This comes back as 'ok' from slack.
|
10
10
|
format :plain
|
@@ -16,46 +16,59 @@ module Slack
|
|
16
16
|
#
|
17
17
|
# Would translate to this:
|
18
18
|
# => /?selected_ids[]=1&selected_ids[]=2&selected_ids[]=3
|
19
|
-
#
|
20
19
|
disable_rails_query_string_format
|
21
20
|
|
22
|
-
#
|
23
|
-
#
|
24
|
-
|
21
|
+
# Define getters and setters for the options hash keys. This will make assign of the options
|
22
|
+
# more flexible.
|
23
|
+
[:username, :channel, :icon_url, :icon_emoji].each do |option_attr|
|
24
|
+
define_method(option_attr) { @options[option_attr] }
|
25
|
+
define_method("#{option_attr}=") { |value| @options[option_attr] = value }
|
26
|
+
end
|
27
|
+
|
28
|
+
# Initializes a Poster instance to post messages with an incoming webhook URL.
|
29
|
+
# It also accepts an options hash. If no options are given then the poster uses the default
|
30
|
+
# configuration from Slack integration.
|
25
31
|
#
|
26
32
|
# ==== Examples
|
27
33
|
#
|
28
|
-
# Without
|
29
|
-
#
|
34
|
+
# # Without options
|
35
|
+
# Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va')
|
30
36
|
#
|
31
|
-
# With
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# You can also use an emoji as avatar:
|
36
|
-
# => Slack::Poster.new('myteam', 'eNmZHQY6f591ziHyZdzePFz8', username: 'Ricardo',
|
37
|
-
# icon_emoji: 'ghost')
|
37
|
+
# # With options using a custom username and icon avatar
|
38
|
+
# Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va',
|
39
|
+
# username: 'Ricardo',
|
40
|
+
# icon_url: 'http://www.gravatar.com/avatar/92e00fd27c64c94d04140cef88039468.png')
|
38
41
|
#
|
42
|
+
# # You can also use an emoji as avatar
|
43
|
+
# Slack::Poster.new('https://hooks.slack.com/services/T044G6VBA//TCIzZQQd7IKhQzCKc6W310va',
|
44
|
+
# username: 'Ricardo',
|
45
|
+
# icon_emoji: 'ghost')
|
39
46
|
def initialize(webhook_url, options = {})
|
40
47
|
self.class.base_uri(webhook_url)
|
41
48
|
|
42
|
-
@attachments = []
|
43
|
-
|
44
49
|
@options = options
|
45
50
|
|
46
|
-
|
51
|
+
fail ArgumentError, 'Webhook URL is required' if webhook_url.nil?
|
47
52
|
end
|
48
53
|
|
54
|
+
# Sends a message to Slack. The message can be either plain text or a Slack::Message object.
|
55
|
+
#
|
56
|
+
# ==== Examples
|
57
|
+
#
|
58
|
+
# # Plain text message
|
59
|
+
# poster.send_message('hello world')
|
60
|
+
#
|
61
|
+
# # Using a message object
|
62
|
+
# poster.send_message(Slack::Message.new(text: 'hello world'))
|
63
|
+
#
|
64
|
+
# You can have messages with attachments if you build your message with a Slack::Message object
|
65
|
+
# and add Slack::Attachment objects.
|
49
66
|
def send_message(message)
|
50
67
|
body = message.is_a?(String) ? options.merge(text: message) : options.merge(message.as_json)
|
51
68
|
|
52
|
-
|
53
|
-
|
54
|
-
response = self.class.post('', { body: { payload: body.to_json }})
|
55
|
-
|
56
|
-
puts body.to_json
|
69
|
+
response = self.class.post('', body: { payload: body.to_json })
|
57
70
|
|
58
|
-
|
71
|
+
response
|
59
72
|
end
|
60
73
|
end
|
61
74
|
end
|
data/lib/slack-poster/version.rb
CHANGED
data/slack-poster.gemspec
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
4
5
|
require 'slack-poster/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
@@ -8,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
8
9
|
spec.version = Slack::VERSION
|
9
10
|
spec.authors = ['Ricardo Otero']
|
10
11
|
spec.email = ['oterosantos@gmail.com']
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
12
|
+
spec.summary = 'Slack wrapper for Incoming WebHooks integrations.'
|
13
|
+
spec.description = 'Slack Poster is a gem to make your integration with Slack WebHooks easier.'
|
13
14
|
spec.homepage = 'https://github.com/rikas/slack-poster'
|
14
15
|
spec.license = 'MIT'
|
15
16
|
|
@@ -18,9 +19,13 @@ Gem::Specification.new do |spec|
|
|
18
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
20
|
spec.require_paths = ['lib']
|
20
21
|
|
21
|
-
spec.add_development_dependency 'bundler', '~> 1.
|
22
|
-
spec.add_development_dependency 'rake', '~> 10.
|
23
|
-
spec.add_development_dependency '
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.10.6', '>= 1.10.6'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.4.2', '>= 10.4.2'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.2.0', '>= 3.2.0'
|
25
|
+
spec.add_development_dependency 'pry', '~> 0.10.3'
|
26
|
+
spec.add_development_dependency 'webmock', '~> 1.22.3', '>= 1.22.3'
|
27
|
+
spec.add_development_dependency 'vcr', '~> 3.0.0', '>= 3.0.0'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.35.1'
|
24
29
|
|
25
|
-
spec.
|
30
|
+
spec.add_runtime_dependency 'httparty', '~> 0.13.7'
|
26
31
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Slack::Attachment do
|
4
|
+
describe '.new' do
|
5
|
+
let(:options) do
|
6
|
+
{
|
7
|
+
fallback: 'fallback',
|
8
|
+
text: 'text',
|
9
|
+
title: 'title',
|
10
|
+
title_link: 'title_link',
|
11
|
+
image_url: 'image_url',
|
12
|
+
thumb_url: 'thumb_url',
|
13
|
+
color: 'color',
|
14
|
+
author_name: 'author_name',
|
15
|
+
author_link: 'author_link',
|
16
|
+
author_icon: 'author_icon',
|
17
|
+
pretext: 'pretext'
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'accepts initialization with no attributes' do
|
22
|
+
att = described_class.new
|
23
|
+
|
24
|
+
expect(att).to_not be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'initializes all possible option' do
|
28
|
+
att = described_class.new(options)
|
29
|
+
|
30
|
+
options.each do |key, value|
|
31
|
+
expect(att.send(key)).to eq(value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#add_field' do
|
37
|
+
context 'when short is set to true' do
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#as_json' do
|
42
|
+
end
|
43
|
+
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,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Slack::Message do
|
4
|
+
let(:message) { described_class.new('hello') }
|
5
|
+
|
6
|
+
it 'can be initialized with an attachment' do
|
7
|
+
attachment = Slack::Attachment.new
|
8
|
+
|
9
|
+
message = described_class.new('hi', attachment)
|
10
|
+
|
11
|
+
expect(message.attachments).to eq([attachment])
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#add_attachment' do
|
15
|
+
it 'adds an attachment correctly' do
|
16
|
+
attachment = Slack::Attachment.new
|
17
|
+
|
18
|
+
message.add_attachment(attachment)
|
19
|
+
|
20
|
+
expect(message.attachments).to eq([attachment])
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'adds multiple attachments correctly' do
|
24
|
+
att1 = Slack::Attachment.new
|
25
|
+
att2 = Slack::Attachment.new
|
26
|
+
|
27
|
+
message.add_attachment(att1)
|
28
|
+
message.add_attachment(att2)
|
29
|
+
|
30
|
+
expect(message.attachments).to eq([att1, att2])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#as_json' do
|
35
|
+
context 'when no attachments were added' do
|
36
|
+
it 'returns the correct json' do
|
37
|
+
expect(message.as_json).to eq(text: 'hello')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when attachments are added' do
|
42
|
+
it 'returns the correct json' do
|
43
|
+
att1 = Slack::Attachment.new(text: 'Att1')
|
44
|
+
att2 = Slack::Attachment.new(title: 'Att2')
|
45
|
+
|
46
|
+
message.add_attachment(att1)
|
47
|
+
message.add_attachment(att2)
|
48
|
+
|
49
|
+
json = { text: 'hello', attachments: [{ text: 'Att1' }, { title: 'Att2' }] }
|
50
|
+
|
51
|
+
expect(message.as_json).to eq(json)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/poster_spec.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Slack::Poster do
|
4
|
+
let(:hook) { ENV.fetch('SLACK_POSTER_TEST_WEBHOOK') }
|
5
|
+
let(:poster) { described_class.new(hook) }
|
6
|
+
|
7
|
+
let(:options) do
|
8
|
+
{
|
9
|
+
username: SecureRandom.hex[0..5],
|
10
|
+
channel: "##{SecureRandom.hex[0..5]}",
|
11
|
+
icon_url: SecureRandom.hex[0..5],
|
12
|
+
icon_emoji: SecureRandom.hex[0..5]
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:with_options) { described_class.new(hook, options) }
|
17
|
+
|
18
|
+
describe '.new' do
|
19
|
+
it 'requires the webhook_url' do
|
20
|
+
expect { described_class.new(nil) }.to raise_error(ArgumentError)
|
21
|
+
expect { described_class.new }.to raise_error(ArgumentError)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'does not need options' do
|
25
|
+
expect { described_class.new('my_webhook') }.to_not raise_exception
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
[:username, :channel, :icon_url, :icon_emoji].each do |option_attr|
|
30
|
+
describe "#{option_attr}=" do
|
31
|
+
it "sets the #{option_attr} field in options hash" do
|
32
|
+
poster.send("#{option_attr}=", "test_#{option_attr}")
|
33
|
+
|
34
|
+
expect(poster.options[option_attr]).to eq("test_#{option_attr}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#{option_attr}" do
|
39
|
+
it "gets the #{option_attr} field in options hash" do
|
40
|
+
expect(with_options.send(option_attr)).to eq(with_options.options[option_attr])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#send_message' do
|
46
|
+
context 'with no attachments' do
|
47
|
+
it 'accepts a message object' do
|
48
|
+
VCR.use_cassette('default_post') do
|
49
|
+
message = Slack::Message.new('Hello world')
|
50
|
+
|
51
|
+
response = poster.send_message(message)
|
52
|
+
|
53
|
+
expect(response).to_not be_nil
|
54
|
+
expect(response.code).to eq(200)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'accepts a string' do
|
59
|
+
VCR.use_cassette('default_post') do
|
60
|
+
response = poster.send_message('Hello world')
|
61
|
+
|
62
|
+
expect(response).to_not be_nil
|
63
|
+
expect(response.code).to eq(200)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
require 'bundler/setup'
|
20
|
+
Bundler.setup
|
21
|
+
|
22
|
+
require 'slack_poster'
|
23
|
+
|
24
|
+
# Use the poster test webhook from an env variable
|
25
|
+
ENV['SLACK_POSTER_TEST_WEBHOOK'] ||= 'https://hooks.slack.com/services/TEST'
|
26
|
+
|
27
|
+
require 'webmock/rspec'
|
28
|
+
|
29
|
+
WebMock.disable_net_connect!
|
30
|
+
|
31
|
+
require 'vcr'
|
32
|
+
|
33
|
+
VCR.configure do |config|
|
34
|
+
config.cassette_library_dir = 'spec/cassettes'
|
35
|
+
config.hook_into :webmock
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec.configure do |config|
|
39
|
+
# rspec-expectations config goes here. You can use an alternate
|
40
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
41
|
+
# assertions if you prefer.
|
42
|
+
config.expect_with :rspec do |expectations|
|
43
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
44
|
+
# and `failure_message` of custom matchers include text for helper methods
|
45
|
+
# defined using `chain`, e.g.:
|
46
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
47
|
+
# # => "be bigger than 2 and smaller than 4"
|
48
|
+
# ...rather than:
|
49
|
+
# # => "be bigger than 2"
|
50
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
51
|
+
end
|
52
|
+
|
53
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
54
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
55
|
+
config.mock_with :rspec do |mocks|
|
56
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
57
|
+
# a real object. This is generally recommended, and will default to
|
58
|
+
# `true` in RSpec 4.
|
59
|
+
mocks.verify_partial_doubles = true
|
60
|
+
end
|
61
|
+
|
62
|
+
# The settings below are suggested to provide a good initial experience
|
63
|
+
# with RSpec, but feel free to customize to your heart's content.
|
64
|
+
|
65
|
+
# These two settings work together to allow you to limit a spec run
|
66
|
+
# to individual examples or groups you care about by tagging them with
|
67
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
68
|
+
# get run.
|
69
|
+
config.filter_run :focus
|
70
|
+
config.run_all_when_everything_filtered = true
|
71
|
+
|
72
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
73
|
+
# recommended. For more details, see:
|
74
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
75
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
76
|
+
# config.disable_monkey_patching!
|
77
|
+
|
78
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
79
|
+
# be too noisy due to issues in dependencies.
|
80
|
+
config.warnings = true
|
81
|
+
|
82
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
83
|
+
# file, and it's useful to allow more verbose output when running an
|
84
|
+
# individual spec file.
|
85
|
+
# if config.files_to_run.one?
|
86
|
+
# # Use the documentation formatter for detailed output,
|
87
|
+
# # unless a formatter has already been configured
|
88
|
+
# # (e.g. via a command-line flag).
|
89
|
+
# config.default_formatter = 'doc'
|
90
|
+
# end
|
91
|
+
|
92
|
+
# Print the 10 slowest examples and example groups at the
|
93
|
+
# end of the spec run, to help surface which specs are running
|
94
|
+
# particularly slow.
|
95
|
+
# config.profile_examples = 10
|
96
|
+
|
97
|
+
# Run specs in random order to surface order dependencies. If you find an
|
98
|
+
# order dependency and want to debug it, you can fix the order by providing
|
99
|
+
# the seed, which is printed after each run.
|
100
|
+
# --seed 1234
|
101
|
+
config.order = :random
|
102
|
+
|
103
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
104
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
105
|
+
# test failures related to randomization by passing the same `--seed` value
|
106
|
+
# as the one that triggered the failure.
|
107
|
+
Kernel.srand config.seed
|
108
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack-poster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Otero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,78 +16,173 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.10.6
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.10.6
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
29
|
+
version: 1.10.6
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.10.6
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
37
|
- - "~>"
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
39
|
+
version: 10.4.2
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 10.4.2
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 10.4.2
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 10.4.2
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 3.2.0
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.2.0
|
34
63
|
type: :development
|
35
64
|
prerelease: false
|
36
65
|
version_requirements: !ruby/object:Gem::Requirement
|
37
66
|
requirements:
|
38
67
|
- - "~>"
|
39
68
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
69
|
+
version: 3.2.0
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 3.2.0
|
41
73
|
- !ruby/object:Gem::Dependency
|
42
74
|
name: pry
|
43
75
|
requirement: !ruby/object:Gem::Requirement
|
44
76
|
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 0.10.3
|
80
|
+
type: :development
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - "~>"
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 0.10.3
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: webmock
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - "~>"
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.22.3
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.22.3
|
97
|
+
type: :development
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.22.3
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 1.22.3
|
107
|
+
- !ruby/object:Gem::Dependency
|
108
|
+
name: vcr
|
109
|
+
requirement: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - "~>"
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 3.0.0
|
45
114
|
- - ">="
|
46
115
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
116
|
+
version: 3.0.0
|
48
117
|
type: :development
|
49
118
|
prerelease: false
|
50
119
|
version_requirements: !ruby/object:Gem::Requirement
|
51
120
|
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 3.0.0
|
52
124
|
- - ">="
|
53
125
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
126
|
+
version: 3.0.0
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: rubocop
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - "~>"
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 0.35.1
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - "~>"
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: 0.35.1
|
55
141
|
- !ruby/object:Gem::Dependency
|
56
142
|
name: httparty
|
57
143
|
requirement: !ruby/object:Gem::Requirement
|
58
144
|
requirements:
|
59
145
|
- - "~>"
|
60
146
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
147
|
+
version: 0.13.7
|
62
148
|
type: :runtime
|
63
149
|
prerelease: false
|
64
150
|
version_requirements: !ruby/object:Gem::Requirement
|
65
151
|
requirements:
|
66
152
|
- - "~>"
|
67
153
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
69
|
-
description:
|
154
|
+
version: 0.13.7
|
155
|
+
description: Slack Poster is a gem to make your integration with Slack WebHooks easier.
|
70
156
|
email:
|
71
157
|
- oterosantos@gmail.com
|
72
|
-
executables:
|
158
|
+
executables:
|
159
|
+
- console
|
73
160
|
extensions: []
|
74
161
|
extra_rdoc_files: []
|
75
162
|
files:
|
76
163
|
- ".gitignore"
|
77
|
-
- ".
|
164
|
+
- ".rspec"
|
165
|
+
- ".rubocop.yml"
|
78
166
|
- ".ruby-version"
|
167
|
+
- ".travis.yml"
|
79
168
|
- Gemfile
|
80
169
|
- LICENSE.txt
|
81
170
|
- README.md
|
82
171
|
- Rakefile
|
83
|
-
-
|
172
|
+
- bin/console
|
84
173
|
- lib/slack-poster/attachment.rb
|
85
|
-
- lib/slack-poster/author.rb
|
86
174
|
- lib/slack-poster/field.rb
|
87
175
|
- lib/slack-poster/message.rb
|
88
176
|
- lib/slack-poster/poster.rb
|
89
177
|
- lib/slack-poster/version.rb
|
178
|
+
- lib/slack_poster.rb
|
90
179
|
- slack-poster.gemspec
|
180
|
+
- spec/attachment_spec.rb
|
181
|
+
- spec/cassettes/default_post.yml
|
182
|
+
- spec/field_spec.rb
|
183
|
+
- spec/message_spec.rb
|
184
|
+
- spec/poster_spec.rb
|
185
|
+
- spec/spec_helper.rb
|
91
186
|
homepage: https://github.com/rikas/slack-poster
|
92
187
|
licenses:
|
93
188
|
- MIT
|
@@ -108,8 +203,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
203
|
version: '0'
|
109
204
|
requirements: []
|
110
205
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.4.
|
206
|
+
rubygems_version: 2.4.5.1
|
112
207
|
signing_key:
|
113
208
|
specification_version: 4
|
114
209
|
summary: Slack wrapper for Incoming WebHooks integrations.
|
115
|
-
test_files:
|
210
|
+
test_files:
|
211
|
+
- spec/attachment_spec.rb
|
212
|
+
- spec/cassettes/default_post.yml
|
213
|
+
- spec/field_spec.rb
|
214
|
+
- spec/message_spec.rb
|
215
|
+
- spec/poster_spec.rb
|
216
|
+
- spec/spec_helper.rb
|
217
|
+
has_rdoc:
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
slack-poster
|
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
|