slack_message 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30f694caf399c0408dab35d5f63be0dc5a06b0c3611f3fd43a9533877f5ca7b1
4
- data.tar.gz: 18a0dbd642a9b505ec21c114cf36fc36b9a4319c02a6f9fa057ac8abe6136957
3
+ metadata.gz: fb63c39ef18b4b6e6fb1d25e62300fcff41b759f3d59902c10423c306f892007
4
+ data.tar.gz: 499f1d885b3a00e60af92ff9d0ba17c19f59ba3b1468d178498d08802c9429dd
5
5
  SHA512:
6
- metadata.gz: 2768ef640f9e2ba0d79227c14f38c4c3288a218464251be81d4f9d182814b1b4540588b16a834e1077d45ce77a82da716de63ebb4fd0de513a261f4da2dd5abf
7
- data.tar.gz: a62d68742f30637398c4a337b9fd815c4b0f035aabe51a1d0be2693c3a72d2fbb8fcbe54cccd9c8e6983c44c1d199909f9e36792ba86d396764cf37549af2966
6
+ metadata.gz: b0bf20b89e6ce48e9accb2ccd475d5f999686804d3c952e6a8903958af2f7b1500011f6abd904abba5f2cf73d635498f8c1feae7e4d00aa021b3ab0b6c1a5baa
7
+ data.tar.gz: 42a8e2ecc5b6ca51ec1ab534e2a91bb500e352e26636f1a5671bf81244a40b321162bed30e1e7e0a78e53490575fbb0052c9d39fb108951f73425269cfafbab5
data/CHANGELOG.md CHANGED
@@ -2,11 +2,16 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [1.3.0] - 2021-09-27
6
+ - Added ability to use custom names when posting.
7
+ - Added ability to post images within sections.
8
+ - Added warnings for potentially invalid URLs.
9
+
5
10
  ## [1.2.0] - 2021-09-26
6
11
  - Turns out gemspec was broken. Fixed that.
7
12
 
8
13
  ## [1.1.0] - 2021-09-26
9
- - Expanded the README significantly w/ usage instructions
14
+ - Expanded the README significantly w/ usage instructions.
10
15
  - Added lots of error handling to requests.
11
16
 
12
17
  ## [1.0.0] - 2021-09-25
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slack_message (1.0.0)
4
+ slack_message (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -6,6 +6,14 @@ API](https://app.slack.com/block-kit-builder/) to make it easy to read and
6
6
  write messages to slack in your ruby application. It has zero dependencies and
7
7
  is built to be opinionated to keep your configuration needs low.
8
8
 
9
+ Posting a message to Slack should be this easy:
10
+
11
+ ```ruby
12
+ SlackMessage.post_to('#general') do
13
+ text "We did it @here! :thumbsup:"
14
+ end
15
+ ```
16
+
9
17
  To install, just add `slack_message` to your bundle and you're ready to go.
10
18
 
11
19
 
@@ -63,11 +71,11 @@ end
63
71
 
64
72
  ### Usage
65
73
 
66
- Basic usage is pretty straightforward:
74
+ As mentioned at the top, posting a message to Slack is dang easy:
67
75
 
68
76
  ```ruby
69
77
  SlackMessage.post_to('#general') do
70
- text "We did it! :thumbsup:"
78
+ text "We did it @here! :thumbsup:"
71
79
  end
72
80
  ```
73
81
 
@@ -130,7 +138,7 @@ SlackMessage is able to build all kinds of rich messages for you, and has been
130
138
  a real joy to use for the author at least. To understand a bit more about the
131
139
  possibilities of blocks, see Slack's [Block Kit
132
140
  Builder](https://app.slack.com/block-kit-builder/) to understand the structure
133
- better:
141
+ better. There are lots of options:
134
142
 
135
143
  ```ruby
136
144
  SlackMessage.post_to('#general') do
@@ -174,6 +182,15 @@ SlackMessage.post_to('#general', as: :sidekiq_bot) do
174
182
  end
175
183
  ```
176
184
 
185
+ You can also use a custom name when sending a message:
186
+
187
+ ```ruby
188
+ SlackMessage.post_to('#general') do
189
+ bot_name "CoffeeBot"
190
+
191
+ text ":coffee::clock: Time to take a break!"
192
+ end
193
+ ```
177
194
 
178
195
  What it Doesn't Do
179
196
  ------------
@@ -190,7 +207,8 @@ DSL to include more of the block API itself.
190
207
  Also, some behaviors that are still planned but not yet added:
191
208
 
192
209
  * allow custom http_options in configuration
193
- * allow custom slack username per built message
210
+ * more of BlockKit's options
211
+ * any interactive elements at all (I don't understand them yet)
194
212
 
195
213
  Contributing
196
214
  ------------
@@ -29,17 +29,19 @@ module SlackMessage::Configuration
29
29
 
30
30
  def self.add_profile(handle = :default, name:, url:)
31
31
  if @@profiles.include?(handle)
32
- warn("WARNING: Overriding profile '#{handle}' in SlackMessage config")
32
+ warn "WARNING: Overriding profile '#{handle}' in SlackMessage config"
33
33
  end
34
34
 
35
35
  @@profiles[handle] = { name: name, url: url, handle: handle }
36
36
  end
37
37
 
38
- def self.profile(handle)
38
+ def self.profile(handle, custom_name: nil)
39
39
  unless @@profiles.include?(handle)
40
40
  raise ArgumentError, "Unknown SlackMessage profile '#{handle}'."
41
41
  end
42
42
 
43
- @@profiles[handle]
43
+ @@profiles[handle].tap do |profile|
44
+ profile[:name] = custom_name if !custom_name.nil?
45
+ end
44
46
  end
45
47
  end
@@ -1,9 +1,10 @@
1
1
  class SlackMessage::Dsl
2
- attr_reader :body, :default_section
2
+ attr_reader :body, :default_section, :custom_bot_name
3
3
 
4
4
  def initialize
5
5
  @body = []
6
6
  @default_section = Section.new
7
+ @custom_bot_name = nil
7
8
  end
8
9
 
9
10
  # allowable top-level entities within a block
@@ -38,12 +39,26 @@ class SlackMessage::Dsl
38
39
 
39
40
  def text(*args); default_section.text(*args); end
40
41
  def link_button(*args); default_section.link_button(*args); end
42
+ def image(*args); default_section.image(*args); end
41
43
  def blank_line(*args); default_section.blank_line(*args); end
42
44
  def link(*args); default_section.link(*args); end
43
45
  def list_item(*args); default_section.list_item(*args); end
44
46
 
45
47
  # end delegation
46
48
 
49
+ # custom bot name
50
+
51
+ def bot_name(name)
52
+ @custom_bot_name = name
53
+ end
54
+
55
+ # end bot name
56
+
57
+ def render
58
+ finalize_default_section
59
+ @body
60
+ end
61
+
47
62
  private
48
63
 
49
64
  # when doing things that would generate new top-levels, first try
@@ -56,11 +71,6 @@ class SlackMessage::Dsl
56
71
  @default_section = Section.new
57
72
  end
58
73
 
59
- def render
60
- finalize_default_section
61
- @body
62
- end
63
-
64
74
  class Section
65
75
  attr_reader :body
66
76
 
@@ -80,6 +90,15 @@ class SlackMessage::Dsl
80
90
 
81
91
  # styles: default, primary, danger
82
92
  def link_button(label, target, style: :primary)
93
+ if !@body[:accessory].nil?
94
+ previous_type = @body[:accessory][:type]
95
+ warn "WARNING: Overriding previous #{previous_type} in section to use link_button instead: #{label}"
96
+ end
97
+
98
+ unless /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/i =~ target
99
+ warn "WARNING: Passing a probably-invalid URL to link button #{label} (url: '#{target}')"
100
+ end
101
+
83
102
  config = {
84
103
  accessory: {
85
104
  type: "button",
@@ -99,6 +118,24 @@ class SlackMessage::Dsl
99
118
  @body.merge!(config)
100
119
  end
101
120
 
121
+ def image(url, alt_text: nil)
122
+ if !@body[:accessory].nil?
123
+ previous_type = @body[:accessory][:type]
124
+ warn "WARNING: Overriding previous #{previous_type} in section to use image instead: #{url}"
125
+ end
126
+
127
+ config = {
128
+ accessory: {
129
+ type: "image",
130
+ image_url: url
131
+ }
132
+ }
133
+
134
+ config[:accessory][:alt_text] = alt_text if !alt_text.nil?
135
+
136
+ @body.merge!(config)
137
+ end
138
+
102
139
  # for markdown links
103
140
  def link(label, target)
104
141
  "<#{target}|#{label}>"
data/lib/slack_message.rb CHANGED
@@ -16,11 +16,14 @@ module SlackMessage
16
16
  end
17
17
 
18
18
  def self.post_to(target, as: :default, &block)
19
- payload = build(&block)
20
- profile = Configuration.profile(as)
19
+ payload = Dsl.new.tap do |instance|
20
+ instance.instance_eval(&block)
21
+ end
22
+
23
+ profile = Configuration.profile(as, custom_name: payload.custom_bot_name)
21
24
  target = user_id_for(target) if target =~ /^\S{1,}@\S{2,}\.\S{2,}$/
22
25
 
23
- Api.post(payload, target, profile)
26
+ Api.post(payload.render, target, profile)
24
27
  end
25
28
 
26
29
  def self.build(&block)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'slack_message'
3
- gem.version = "1.2.0"
3
+ gem.version = "1.3.0"
4
4
  gem.summary = "A nice DSL for composing rich messages in Slack"
5
5
  gem.authors = ["Joe Mastey"]
6
6
  gem.email = 'hello@joemastey.com'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack_message
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Mastey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-26 00:00:00.000000000 Z
11
+ date: 2021-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec