grape-slack-bot 1.2.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34be79021e713fec3adb16eaf1006303d01fb3e5da025daba80aa28ee6400bd8
4
- data.tar.gz: 7c978813cafc6cb78c97d8db3001e90a9f5961eac73269e0c748e0b4fedcebc5
3
+ metadata.gz: 99c07a4830556a1a896dea28c85f7fade663c8b49b6e649d084e2bf115a8eacd
4
+ data.tar.gz: 6dcce44e9edaf18c97f62dbf65fa248d6b309caad9e9ed6dcf7cdf327a16f063
5
5
  SHA512:
6
- metadata.gz: 1c55b60b6ea73f8528daee36229b9556f8c1fbe885caebd135181d561ed73629e5ebe4784ff2e7cdb9f3d581acc758db493112571f1d0c0eea8824e1613bd6ee
7
- data.tar.gz: ed3c34753a2affe1dc01031ebc43af4fd5dea85279cf09cab6272893f81930f5099de57716dbf2cd0f4d5a63166175721bfe657234ead03ba094186f62f8babf
6
+ metadata.gz: 64c24157366d85a69449488dde1aa029f562afde69e0b3c210241d2e4778675842ed9003012f9f19193e0ef04d5a45f48f0acd70bfae9a55cc831667d4c0ee25
7
+ data.tar.gz: a399297e42b45b7be8bd05587abeb9150fba0276953c1771a46c05bbaee7b29361181cb16fc5b672d58bd68fcfe869b706166dd664261b61ac86e45bd633b3a7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 1.2.2
2
+
3
+ * `SlackBot::Callback.find` method will raise `SlackBot::Errors::CallbackNotFound` if callback is not resolved or has wrong data
4
+
1
5
  # 1.2.1
2
6
 
3
7
  * 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)
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.2'.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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov