grape-slack-bot 1.2.1 → 1.2.3

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: 34be79021e713fec3adb16eaf1006303d01fb3e5da025daba80aa28ee6400bd8
4
- data.tar.gz: 7c978813cafc6cb78c97d8db3001e90a9f5961eac73269e0c748e0b4fedcebc5
3
+ metadata.gz: 3472ece85beebd447e192f73013849e3e37a366fdc22b4f4864ef0908f58f502
4
+ data.tar.gz: 7f25928036698338c354238d9615ccd980d136a0c88056be1a1989304c95eae9
5
5
  SHA512:
6
- metadata.gz: 1c55b60b6ea73f8528daee36229b9556f8c1fbe885caebd135181d561ed73629e5ebe4784ff2e7cdb9f3d581acc758db493112571f1d0c0eea8824e1613bd6ee
7
- data.tar.gz: ed3c34753a2affe1dc01031ebc43af4fd5dea85279cf09cab6272893f81930f5099de57716dbf2cd0f4d5a63166175721bfe657234ead03ba094186f62f8babf
6
+ metadata.gz: a2902bfc733cd9aec81df154e0c43ac2bfe9eca7fd6105cb7b6d2f1ea1631daae093e2a96698388accaf35b46c4da826218333bab3c78bed7781c17583e01312
7
+ data.tar.gz: 04a70f1a3a5251d44d2b4bb21cdaa113296d50c51b0fae010f27f5c1a7caeb5b41eb635c6d349672701a5aef13c58f39249f8e740a2310ab5024dd822e7341ce
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 1.2.3
2
+
3
+ * Minor fix for Events API
4
+
5
+ # 1.2.2
6
+
7
+ * `SlackBot::Callback.find` method will raise `SlackBot::Errors::CallbackNotFound` if callback is not resolved or has wrong data
8
+
1
9
  # 1.2.1
2
10
 
3
11
  * Extract `SlackBot::Logger` to separate file
@@ -6,8 +6,12 @@ module SlackBot
6
6
  CALLBACK_CACHE_KEY = "slack-bot-callback".freeze
7
7
 
8
8
  def self.find(id, config: nil)
9
+ return if id.blank?
10
+
9
11
  callback = new(id: id, config: config)
10
12
  callback.reload
13
+ rescue SlackBot::Errors::CallbackNotFound
14
+ nil
11
15
  end
12
16
 
13
17
  def self.create(class_name:, method_name:, user:, channel_id: nil, config: nil)
@@ -34,6 +38,7 @@ module SlackBot
34
38
  def reload
35
39
  @data = read_data
36
40
  SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{data}")
41
+ raise SlackBot::Errors::CallbackNotFound if data.nil?
37
42
 
38
43
  parse_args
39
44
  self
@@ -83,7 +88,7 @@ module SlackBot
83
88
  private
84
89
 
85
90
  def parse_args
86
- args.raw_args = data&.dig(:args)
91
+ args.raw_args = data.fetch(:args)
87
92
  end
88
93
 
89
94
  def serialize_args
@@ -18,6 +18,9 @@ module SlackBot
18
18
  class MenuOptionsNotImplemented < SlackBot::Error
19
19
  end
20
20
 
21
+ class CallbackNotFound < SlackBot::Error
22
+ end
23
+
21
24
  class SlackResponseError < SlackBot::Error
22
25
  attr_reader :error, :data, :payload
23
26
  def initialize(error, data: nil, payload: nil)
@@ -22,11 +22,11 @@ module SlackBot
22
22
  params["event"]["type"]
23
23
  end
24
24
 
25
- def publish_view(view_method_name)
25
+ def publish_view(view_method_name, context: nil)
26
26
  user_id = params["event"]["user"]
27
27
  view =
28
28
  self.class.view_klass
29
- .new(current_user: current_user, params: params)
29
+ .new(current_user: current_user, params: params, context: context)
30
30
  .send(view_method_name)
31
31
  response =
32
32
  SlackBot::ApiClient.new.views_publish(user_id: user_id, view: view)
@@ -84,11 +84,9 @@ module SlackBot
84
84
 
85
85
  def handle_block_actions_view(view:, user:, params:)
86
86
  callback_id = view&.dig("callback_id")
87
- callback = SlackBot::Callback.find(callback_id, config: config)
88
87
 
89
- if callback.blank?
90
- raise "Callback not found"
91
- end
88
+ callback = SlackBot::Callback.find(callback_id, config: config)
89
+ raise SlackBot::Errors::CallbackNotFound.new if callback.blank?
92
90
 
93
91
  SlackBot::DevConsole.log_check "SlackApi::Interactions##{__method__}: #{callback.id} #{callback.extra} #{callback.user_id} #{user&.id}"
94
92
 
data/lib/slack_bot.rb CHANGED
@@ -22,5 +22,5 @@ require 'slack_bot/pager'
22
22
  require 'slack_bot/grape_extension'
23
23
 
24
24
  module SlackBot
25
- VERSION = '1.2.1'.freeze
25
+ VERSION = '1.2.3'.freeze
26
26
  end
@@ -34,7 +34,7 @@ describe SlackBot::Callback do
34
34
  end
35
35
 
36
36
  context "when callback is found" do
37
- let(:data) { { class_name: "Test", method_name: "test", user_id: 1, channel_id: "test_channel_id", extra: { test: "test" } } }
37
+ let(:data) { { class_name: "Test", method_name: "test", user_id: 1, channel_id: "test_channel_id", extra: { test: "test" }, args: "" } }
38
38
 
39
39
  it "returns callback" do
40
40
  expect(find).to be_a(described_class)
@@ -47,6 +47,14 @@ describe SlackBot::Callback do
47
47
  expect(find.extra).to eq({ test: "test" })
48
48
  end
49
49
  end
50
+
51
+ context "when callback is not found" do
52
+ let(:data) { nil }
53
+
54
+ it "returns nil" do
55
+ expect(find).to eq(nil)
56
+ end
57
+ end
50
58
  end
51
59
 
52
60
  describe ".create" do
@@ -79,15 +87,112 @@ describe SlackBot::Callback do
79
87
  end
80
88
 
81
89
  describe "#reload" do
90
+ subject(:reload) { callback.reload }
91
+ let(:callback) {
92
+ described_class.new(id: "test_callback_id", config: config)
93
+ }
94
+ let(:data) {
95
+ {
96
+ class_name: "Test",
97
+ method_name: "test",
98
+ user_id: 1,
99
+ channel_id: "test_channel_id",
100
+ extra: { test: "test" },
101
+ args: ""
102
+ }
103
+ }
104
+
105
+ before do
106
+ allow(callback_storage_instance).to receive(:read).with("slack-bot-callback:test_callback_id").and_return(data)
107
+ end
82
108
 
109
+ it "returns callback" do
110
+ expect(reload).to be_a(described_class)
111
+ expect(reload.id).to eq("test_callback_id")
112
+ expect(reload.class_name).to eq("Test")
113
+ expect(reload.user).to eq(user)
114
+ expect(reload.user_id).to eq(1)
115
+ expect(reload.channel_id).to eq("test_channel_id")
116
+ expect(reload.method_name).to eq("test")
117
+ expect(reload.extra).to eq({ test: "test" })
118
+ end
119
+
120
+ context "when callback is not found" do
121
+ let(:data) { nil }
122
+
123
+ it "raises error" do
124
+ expect { reload }.to raise_error(SlackBot::Errors::CallbackNotFound)
125
+ end
126
+ end
83
127
  end
84
128
 
85
129
  describe "#save" do
130
+ subject(:save) { callback.save }
131
+ let(:callback) {
132
+ described_class.new(
133
+ class_name: "Test",
134
+ method_name: "test",
135
+ user: user,
136
+ channel_id: "test_channel_id",
137
+ extra: { test: "test" },
138
+ config: config
139
+ )
140
+ }
141
+
142
+ before do
143
+ allow_any_instance_of(described_class).to receive(:generate_id).and_return("test_callback_id")
144
+ allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
145
+ args: "",
146
+ class_name: "Test",
147
+ method_name: "test",
148
+ user_id: 1,
149
+ channel_id: "test_channel_id",
150
+ extra: { test: "test" }
151
+ }, expires_in: 1.hour)
152
+ end
86
153
 
154
+ it "returns callback" do
155
+ expect { save }.not_to raise_error
156
+ expect(callback).to be_a(described_class)
157
+ expect(callback.id).to eq("test_callback_id")
158
+ expect(callback.args).to be_a(SlackBot::Args)
159
+ end
87
160
  end
88
161
 
89
162
  describe "#update" do
163
+ subject(:update) { callback.update(payload) }
164
+ let(:callback) {
165
+ described_class.new(
166
+ id: "test_callback_id",
167
+ class_name: "Test",
168
+ method_name: "test",
169
+ user: user,
170
+ channel_id: "test_channel_id",
171
+ extra: { test: "test" },
172
+ config: config
173
+ )
174
+ }
90
175
 
176
+ before do
177
+ allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
178
+ args: "",
179
+ class_name: "Test",
180
+ method_name: "test",
181
+ user_id: 1,
182
+ channel_id: "test_channel_id",
183
+ extra: { test: "test" },
184
+ test: "test"
185
+ }, expires_in: 1.hour)
186
+ end
187
+
188
+ let(:payload) { { test: "test" } }
189
+
190
+ it "returns callback" do
191
+ expect { update }.not_to raise_error
192
+ expect(callback).to be_a(described_class)
193
+ expect(callback.id).to eq("test_callback_id")
194
+ expect(callback.args).to be_a(SlackBot::Args)
195
+ end
91
196
  end
92
197
 
93
198
  describe "#destroy" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-slack-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov