slack_message 2.2.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -111,12 +111,14 @@ class SlackMessage::Dsl
111
111
  # replace emails w/ real user IDs
112
112
  def enrich_text(text_body)
113
113
  text_body.scan(SlackMessage::EMAIL_TAG_PATTERN).each do |email_tag|
114
- raw_email = email_tag.gsub(/[><]/, '')
115
- user_id = SlackMessage::Api::user_id_for(raw_email, profile)
114
+ begin
115
+ raw_email = email_tag.gsub(/[><]/, '')
116
+ user_id = SlackMessage::Api::user_id_for(raw_email, profile)
116
117
 
117
- text_body.gsub!(email_tag, "<@#{user_id}>") if user_id
118
- rescue SlackMessage::ApiError => e
119
- # swallow errors for not-found users
118
+ text_body.gsub!(email_tag, "<@#{user_id}>") if user_id
119
+ rescue SlackMessage::ApiError => e
120
+ # swallow errors for not-found users
121
+ end
120
122
  end
121
123
 
122
124
  text_body
@@ -0,0 +1,42 @@
1
+ class SlackMessage::Response
2
+ attr_reader :channel, :timestamp, :profile_handle, :scheduled_message_id, :original_response
3
+
4
+ def initialize(api_response, profile_handle)
5
+ @original_response = JSON.parse(api_response.body)
6
+ @ok = @original_response["ok"]
7
+ @channel = @original_response["channel"]
8
+
9
+ @timestamp = @original_response["ts"]
10
+ @scheduled_message_id = @original_response["scheduled_message_id"]
11
+
12
+ @profile_handle = profile_handle
13
+ end
14
+
15
+ def marshal_dump
16
+ [ @profile_handle, @channel, @timestamp, @original_response, @ok, @original_response ]
17
+ end
18
+
19
+ def marshal_load(data)
20
+ @profile_handle, @channel, @timestamp, @original_response, @ok, @original_response = data
21
+ end
22
+
23
+ def sent_to_user?
24
+ channel =~ /^D.*/ # users are D for DM, channels start w/ C
25
+ end
26
+
27
+ def scheduled?
28
+ !!scheduled_message_id
29
+ end
30
+
31
+ def inspect
32
+ identifier = if scheduled?
33
+ "scheduled_message_id=#{scheduled_message_id}"
34
+ else
35
+ "timestamp=#{timestamp}"
36
+ end
37
+
38
+ ok_msg = @ok ? "ok" : "error"
39
+
40
+ "<SlackMessage::Response #{ok_msg} profile_handle=:#{profile_handle} channel=#{channel} #{identifier}>"
41
+ end
42
+ end
@@ -14,6 +14,8 @@ require 'rspec/mocks'
14
14
  # it can be cleaned up properly.
15
15
  #
16
16
 
17
+ # TODO: testing for scheduled messages, editing and deleting
18
+
17
19
  module SlackMessage::RSpec
18
20
  extend RSpec::Matchers::DSL
19
21
 
@@ -206,11 +208,11 @@ module SlackMessage::RSpec
206
208
  SlackMessage::RSpec.unregister_expectation_listener(self)
207
209
 
208
210
  @captured_calls
209
- .filter { |call| !@channel || call[:channel] == @channel }
210
- .filter { |call| !@profile || [call[:profile][:handle], call[:username]].include?(@profile) }
211
- .filter { |call| !@content || call.fetch(:blocks).to_s =~ @content }
212
- .filter { |call| !@icon || call.fetch(:icon_emoji, call.fetch(:icon_url, '')) == @icon }
213
- .filter { |call| !@icon_matching || call.fetch(:icon_emoji, call.fetch(:icon_url, '')) =~ @icon_matching }
211
+ .select { |call| !@channel || call[:channel] == @channel }
212
+ .select { |call| !@profile || [call[:profile][:handle], call[:username]].include?(@profile) }
213
+ .select { |call| !@content || call.fetch(:blocks).to_s =~ @content }
214
+ .select { |call| !@icon || call.fetch(:icon_emoji, call.fetch(:icon_url, '')) == @icon }
215
+ .select { |call| !@icon_matching || call.fetch(:icon_emoji, call.fetch(:icon_url, '')) =~ @icon_matching }
214
216
  .any?
215
217
  end
216
218
 
data/lib/slack_message.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module SlackMessage
2
+ require 'slack_message/response'
2
3
  require 'slack_message/dsl'
3
4
  require 'slack_message/api'
4
5
  require 'slack_message/configuration'
@@ -21,7 +22,7 @@ module SlackMessage
21
22
  Api.user_id_for(email, profile)
22
23
  end
23
24
 
24
- def self.post_to(target, as: :default, &block)
25
+ def self.post_to(target, as: :default, at: nil, &block)
25
26
  profile = Configuration.profile(as)
26
27
 
27
28
  payload = Dsl.new(block, profile).tap do |instance|
@@ -30,23 +31,53 @@ module SlackMessage
30
31
 
31
32
  target = Api::user_id_for(target, profile) if target =~ EMAIL_PATTERN
32
33
 
33
- Api.post(payload, target, profile)
34
+ Api.post(payload, target, profile, at)
34
35
  end
35
36
 
36
- def self.post_as(profile_name, &block)
37
+ def self.post_as(profile_name, at: nil, &block)
37
38
  profile = Configuration.profile(profile_name)
38
39
  if profile[:default_channel].nil?
39
40
  raise ArgumentError, "Sorry, you need to specify a default_channel for profile #{profile_name} to use post_as"
40
41
  end
41
42
 
43
+ target = profile[:default_channel]
42
44
  payload = Dsl.new(block, profile).tap do |instance|
43
45
  instance.instance_eval(&block)
44
46
  end
45
47
 
46
- target = profile[:default_channel]
47
48
  target = Api::user_id_for(target, profile) if target =~ EMAIL_PATTERN
48
49
 
49
- Api.post(payload, target, profile)
50
+ Api.post(payload, target, profile, at)
51
+ end
52
+
53
+ def self.update(message, &block)
54
+ unless message.is_a?(SlackMessage::Response)
55
+ raise ArgumentError, "You must pass in a SlackMessage::Response to update a message"
56
+ end
57
+
58
+ if message.scheduled?
59
+ raise ArgumentError, "Sorry, scheduled messages cannot be updated. You will need to delete the message and schedule a new one."
60
+ end
61
+
62
+ profile = Configuration.profile(message.profile_handle)
63
+ payload = Dsl.new(block, profile).tap do |instance|
64
+ instance.instance_eval(&block)
65
+ end
66
+
67
+ Api.update(payload, message, profile)
68
+ end
69
+
70
+ def self.delete(message)
71
+ unless message.is_a?(SlackMessage::Response)
72
+ raise ArgumentError, "You must pass in a SlackMessage::Response to delete a message"
73
+ end
74
+
75
+ if message.sent_to_user?
76
+ raise ArgumentError, "It's not possible to delete messages sent directly to users."
77
+ end
78
+
79
+ profile = Configuration.profile(message.profile_handle)
80
+ Api.delete(message, profile)
50
81
  end
51
82
 
52
83
  def self.build(profile_name = :default, &block)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'slack_message'
3
- gem.version = "2.2.2"
3
+ gem.version = "3.0.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'
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  "source_code_uri" => "http://github.com/jmmastey/slack_message",
19
19
  }
20
20
 
21
+ gem.required_ruby_version = '>= 2.5.0'
22
+
21
23
  gem.add_development_dependency "rspec", "3.10.0"
22
24
  gem.add_development_dependency "pry", "0.14.1"
23
25
  gem.add_development_dependency "rb-readline", "0.5.5"
@@ -142,13 +142,21 @@ RSpec.describe SlackMessage do
142
142
  expect {
143
143
  SlackMessage.post_to('#general') { text("Not Tagged: hello@joemastey.com ") }
144
144
  }.to post_to_slack.with_content_matching(/hello@joemastey.com/)
145
+ end
145
146
 
146
-
147
- allow(SlackMessage::Api).to receive(:user_id_for).and_raise(SlackMessage::ApiError)
147
+ it "is graceful about those failures" do
148
+ allow(SlackMessage::Api).to receive(:user_id_for).with('nuffin@nuffin.nuffin', any_args).and_raise(SlackMessage::ApiError)
149
+ allow(SlackMessage::Api).to receive(:user_id_for).with('hello@joemastey.com', any_args).and_return('ABC123')
148
150
 
149
151
  expect {
150
152
  SlackMessage.post_to('#general') { text("Not User: <nuffin@nuffin.nuffin>") }
151
153
  }.to post_to_slack.with_content_matching(/\<nuffin@nuffin.nuffin\>/)
154
+
155
+ expect {
156
+ SlackMessage.post_to('#general') { text("Not User: <nuffin@nuffin.nuffin>, User: <hello@joemastey.com>") }
157
+ }.to post_to_slack.with_content_matching(/ABC123/)
152
158
  end
153
159
  end
160
+
161
+ # tests for actual sending methods? what would actually be useful?
154
162
  end
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: 2.2.2
4
+ version: 3.0.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-11-30 00:00:00.000000000 Z
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -64,13 +64,21 @@ files:
64
64
  - CHANGELOG.md
65
65
  - CODE_OF_CONDUCT.md
66
66
  - Gemfile
67
- - Gemfile.lock
68
67
  - MIT-LICENSE
69
68
  - README.md
69
+ - docs/01_configuration.md
70
+ - docs/02_posting_a_message.md
71
+ - docs/03_message_dsl.md
72
+ - docs/04_editing_messages.md
73
+ - docs/05_deleting_messages.md
74
+ - docs/06_notifying_users.md
75
+ - docs/07_testing.md
76
+ - docs/index.md
70
77
  - lib/slack_message.rb
71
78
  - lib/slack_message/api.rb
72
79
  - lib/slack_message/configuration.rb
73
80
  - lib/slack_message/dsl.rb
81
+ - lib/slack_message/response.rb
74
82
  - lib/slack_message/rspec.rb
75
83
  - slack_message.gemspec
76
84
  - spec/slack_message_spec.rb
@@ -90,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
98
  requirements:
91
99
  - - ">="
92
100
  - !ruby/object:Gem::Version
93
- version: '0'
101
+ version: 2.5.0
94
102
  required_rubygems_version: !ruby/object:Gem::Requirement
95
103
  requirements:
96
104
  - - ">="
data/Gemfile.lock DELETED
@@ -1,40 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- slack_message (2.2.2)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- coderay (1.1.3)
10
- diff-lcs (1.4.4)
11
- method_source (1.0.0)
12
- pry (0.14.1)
13
- coderay (~> 1.1)
14
- method_source (~> 1.0)
15
- rb-readline (0.5.5)
16
- rspec (3.10.0)
17
- rspec-core (~> 3.10.0)
18
- rspec-expectations (~> 3.10.0)
19
- rspec-mocks (~> 3.10.0)
20
- rspec-core (3.10.1)
21
- rspec-support (~> 3.10.0)
22
- rspec-expectations (3.10.1)
23
- diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.10.0)
25
- rspec-mocks (3.10.2)
26
- diff-lcs (>= 1.2.0, < 2.0)
27
- rspec-support (~> 3.10.0)
28
- rspec-support (3.10.2)
29
-
30
- PLATFORMS
31
- ruby
32
-
33
- DEPENDENCIES
34
- pry (= 0.14.1)
35
- rb-readline (= 0.5.5)
36
- rspec (= 3.10.0)
37
- slack_message!
38
-
39
- BUNDLED WITH
40
- 2.1.4