grape-slack-bot 1.5.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/slack_bot/callback.rb +29 -11
- data/lib/slack_bot.rb +1 -1
- data/spec/slack_bot/callback_spec.rb +65 -123
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d248818a6772bc6b90aaaff5e6a32f34ed8f1be71d3865435929e018f30d48b
|
4
|
+
data.tar.gz: 8719e28b6995efa871e4a834aa5ac884cbda4133034805e256ddf53786c1fdb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a20d2e0f603d9f5a1cbfc24d8e08e9907b12d6d677442d63160cf0a5f57b3dc56234bd65b0d979749574e290a3a3fb00b8f014bf219c8bf969e2c4d1cd7f7f3
|
7
|
+
data.tar.gz: f3401998a0b1cd270e952107d86f2da78de98cc45b4260d7205998ff173e00f356d09185d08f50416bebe5b38593fe9cb40e829b2f4a4454bc3dafd53414227b
|
data/lib/slack_bot/callback.rb
CHANGED
@@ -3,27 +3,34 @@ require 'active_support/core_ext/numeric/time'
|
|
3
3
|
|
4
4
|
module SlackBot
|
5
5
|
class Callback
|
6
|
-
|
6
|
+
CALLBACK_KEY_PREFIX = "slack-bot-callback".freeze
|
7
7
|
CALLBACK_RECORD_EXPIRES_IN = 15.minutes.freeze
|
8
8
|
|
9
|
-
def self.find(id, config: nil)
|
9
|
+
def self.find(id, user: nil, config: nil)
|
10
10
|
return if id.blank?
|
11
11
|
|
12
|
-
callback = new(id: id, config: config)
|
12
|
+
callback = new(id: id, user: user, config: config)
|
13
13
|
callback.reload
|
14
14
|
rescue SlackBot::Errors::CallbackNotFound
|
15
15
|
nil
|
16
16
|
end
|
17
17
|
|
18
|
-
def self.create(class_name:, user:, channel_id: nil, config: nil, payload: nil, expires_in: nil)
|
18
|
+
def self.create(class_name:, user:, channel_id: nil, config: nil, payload: nil, expires_in: nil, user_scope: nil)
|
19
19
|
callback =
|
20
|
-
new(class_name: class_name, user: user, channel_id: channel_id, payload: payload, config: config, expires_in: expires_in)
|
20
|
+
new(class_name: class_name, user: user, channel_id: channel_id, payload: payload, config: config, expires_in: expires_in, user_scope: user_scope)
|
21
21
|
callback.save
|
22
22
|
callback
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
def self.find_or_create(id:, class_name:, user:, channel_id: nil, config: nil, payload: nil, expires_in: nil, user_scope: nil)
|
26
|
+
callback = find(id, user: user, config: config)
|
27
|
+
return callback if callback.present?
|
28
|
+
|
29
|
+
create(class_name: class_name, user: user, channel_id: channel_id, payload: payload, config: config, expires_in: expires_in, user_scope: user_scope)
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :id, :data, :args, :config, :expires_in, :user_scope
|
33
|
+
def initialize(id: nil, class_name: nil, user: nil, channel_id: nil, payload: nil, config: nil, expires_in: nil, user_scope: nil)
|
27
34
|
@id = id
|
28
35
|
@data = {
|
29
36
|
class_name: class_name,
|
@@ -34,6 +41,7 @@ module SlackBot
|
|
34
41
|
@args = SlackBot::Args.new
|
35
42
|
@config = config || SlackBot::Config.current_instance
|
36
43
|
@expires_in = expires_in || CALLBACK_RECORD_EXPIRES_IN
|
44
|
+
@user_scope = user_scope.nil? ? true : user_scope
|
37
45
|
end
|
38
46
|
|
39
47
|
def reload
|
@@ -121,20 +129,30 @@ module SlackBot
|
|
121
129
|
end
|
122
130
|
|
123
131
|
def generate_id
|
124
|
-
SecureRandom.
|
132
|
+
SecureRandom.hex(10)
|
133
|
+
end
|
134
|
+
|
135
|
+
def storage_key
|
136
|
+
if user_scope
|
137
|
+
raise "User is required for scoped callback" if user.blank?
|
138
|
+
|
139
|
+
"#{CALLBACK_KEY_PREFIX}:u#{user.id}:#{id}"
|
140
|
+
else
|
141
|
+
"#{CALLBACK_KEY_PREFIX}:#{id}"
|
142
|
+
end
|
125
143
|
end
|
126
144
|
|
127
145
|
def read_data
|
128
|
-
config.callback_storage_instance.read(
|
146
|
+
config.callback_storage_instance.read(storage_key)
|
129
147
|
end
|
130
148
|
|
131
149
|
def write_data(data, expires_in: nil)
|
132
150
|
expires_in ||= CALLBACK_RECORD_EXPIRES_IN
|
133
|
-
config.callback_storage_instance.write(
|
151
|
+
config.callback_storage_instance.write(storage_key, data, expires_in: expires_in)
|
134
152
|
end
|
135
153
|
|
136
154
|
def delete_data
|
137
|
-
config.callback_storage_instance.delete(
|
155
|
+
config.callback_storage_instance.delete(storage_key)
|
138
156
|
end
|
139
157
|
end
|
140
158
|
end
|
data/lib/slack_bot.rb
CHANGED
@@ -26,173 +26,115 @@ describe SlackBot::Callback do
|
|
26
26
|
let(:callback_id) { "test_callback_id" }
|
27
27
|
|
28
28
|
describe ".find" do
|
29
|
-
subject(:find) { described_class.find(callback_id, config: config) }
|
29
|
+
subject(:find) { described_class.find(callback_id, user: user, config: config) }
|
30
30
|
|
31
|
-
|
32
|
-
allow(callback_storage_instance).to receive(:read).with("slack-bot-callback:test_callback_id").and_return(data)
|
33
|
-
end
|
34
|
-
|
35
|
-
context "when callback is found" do
|
36
|
-
let(:data) { { class_name: "Test", user_id: 1, channel_id: "test_channel_id", payload: { test: "test" }, args: "" } }
|
37
|
-
|
38
|
-
it "returns callback" do
|
39
|
-
expect(find).to be_a(described_class)
|
40
|
-
expect(find.id).to eq(callback_id)
|
41
|
-
expect(find.class_name).to eq("Test")
|
42
|
-
expect(find.user).to eq(user)
|
43
|
-
expect(find.user_id).to eq(1)
|
44
|
-
expect(find.channel_id).to eq("test_channel_id")
|
45
|
-
expect(find.payload).to eq({ test: "test" })
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context "when callback is not found" do
|
50
|
-
let(:data) { nil }
|
51
|
-
|
52
|
-
it "returns nil" do
|
53
|
-
expect(find).to eq(nil)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe ".create" do
|
59
|
-
subject(:create) { described_class.create(class_name: "Test", user: user, channel_id: "test_channel_id", config: config) }
|
60
|
-
|
61
|
-
before do
|
62
|
-
allow_any_instance_of(described_class).to receive(:generate_id).and_return("test_callback_id")
|
63
|
-
allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
|
64
|
-
args: "",
|
65
|
-
class_name: "Test",
|
66
|
-
user_id: 1,
|
67
|
-
channel_id: "test_channel_id",
|
68
|
-
payload: nil
|
69
|
-
}, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
|
70
|
-
end
|
71
|
-
|
72
|
-
let(:data) { { class_name: "Test", user_id: 1, channel_id: "test_channel_id", payload: nil } }
|
73
|
-
|
74
|
-
it "returns callback" do
|
75
|
-
expect(create).to be_a(described_class)
|
76
|
-
expect(create.id).to eq("test_callback_id")
|
77
|
-
expect(create.class_name).to eq("Test")
|
78
|
-
expect(create.user).to eq(user)
|
79
|
-
expect(create.user_id).to eq(1)
|
80
|
-
expect(create.channel_id).to eq("test_channel_id")
|
81
|
-
expect(create.payload).to eq(nil)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe "#reload" do
|
86
|
-
subject(:reload) { callback.reload }
|
87
|
-
let(:callback) {
|
88
|
-
described_class.new(id: "test_callback_id", config: config)
|
89
|
-
}
|
90
|
-
let(:data) {
|
31
|
+
let(:cached_data) {
|
91
32
|
{
|
92
33
|
class_name: "Test",
|
93
|
-
user_id:
|
34
|
+
user_id: user.id,
|
94
35
|
channel_id: "test_channel_id",
|
95
|
-
payload: { test: "test
|
96
|
-
args: ""
|
36
|
+
payload: { test: "test" },
|
37
|
+
args: "test"
|
97
38
|
}
|
98
39
|
}
|
99
40
|
|
100
41
|
before do
|
101
|
-
allow(callback_storage_instance).to receive(:read).with("
|
42
|
+
allow(callback_storage_instance).to receive(:read).with("#{SlackBot::Callback::CALLBACK_KEY_PREFIX}:u#{user.id}:#{callback_id}").and_return(cached_data)
|
102
43
|
end
|
103
44
|
|
104
45
|
it "returns callback" do
|
105
|
-
expect(
|
106
|
-
expect(
|
107
|
-
expect(
|
108
|
-
expect(
|
109
|
-
expect(
|
110
|
-
expect(
|
111
|
-
expect(
|
46
|
+
expect(find).to be_a(described_class)
|
47
|
+
expect(find.id).to eq(callback_id)
|
48
|
+
expect(find.class_name).to eq("Test")
|
49
|
+
expect(find.user).to eq(user)
|
50
|
+
expect(find.channel_id).to eq("test_channel_id")
|
51
|
+
expect(find.payload).to eq({ test: "test" })
|
52
|
+
expect(find.args).to be_a(SlackBot::Args)
|
112
53
|
end
|
113
54
|
|
114
55
|
context "when callback is not found" do
|
115
|
-
let(:
|
56
|
+
let(:cached_data) { nil }
|
116
57
|
|
117
|
-
it "
|
118
|
-
expect
|
58
|
+
it "returns nil" do
|
59
|
+
expect(find).to be_nil
|
119
60
|
end
|
120
61
|
end
|
121
62
|
end
|
122
63
|
|
123
|
-
describe "
|
124
|
-
subject(:
|
125
|
-
|
126
|
-
described_class.new(
|
64
|
+
describe ".create" do
|
65
|
+
subject(:create) {
|
66
|
+
described_class.create(
|
127
67
|
class_name: "Test",
|
128
68
|
user: user,
|
129
69
|
channel_id: "test_channel_id",
|
130
|
-
payload: { test: "test
|
70
|
+
payload: { test: "test" },
|
131
71
|
config: config
|
132
72
|
)
|
133
73
|
}
|
134
74
|
|
135
|
-
|
136
|
-
|
137
|
-
allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
|
138
|
-
args: "",
|
75
|
+
let(:cached_data) {
|
76
|
+
{
|
139
77
|
class_name: "Test",
|
140
|
-
user_id:
|
78
|
+
user_id: user.id,
|
141
79
|
channel_id: "test_channel_id",
|
142
|
-
payload: { test: "test
|
143
|
-
|
80
|
+
payload: { test: "test" },
|
81
|
+
args: ""
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
before do
|
86
|
+
allow_any_instance_of(described_class).to receive(:generate_id).and_return(callback_id)
|
87
|
+
allow(callback_storage_instance).to receive(:write).with("#{SlackBot::Callback::CALLBACK_KEY_PREFIX}:u#{user.id}:#{callback_id}", cached_data, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN).and_return(cached_data)
|
88
|
+
allow(callback_storage_instance).to receive(:read).with("#{SlackBot::Callback::CALLBACK_KEY_PREFIX}:u#{user.id}:#{callback_id}").and_return(cached_data)
|
144
89
|
end
|
145
90
|
|
146
|
-
it "
|
147
|
-
|
148
|
-
expect(
|
149
|
-
expect(
|
150
|
-
expect(
|
91
|
+
it "creates callback" do
|
92
|
+
expect(create).to be_a(described_class)
|
93
|
+
expect(create.id).to be_present
|
94
|
+
expect(create.class_name).to eq("Test")
|
95
|
+
expect(create.user).to eq(user)
|
96
|
+
expect(create.channel_id).to eq("test_channel_id")
|
97
|
+
expect(create.payload).to eq({ test: "test" })
|
98
|
+
expect(create.args).to be_a(SlackBot::Args)
|
151
99
|
end
|
152
100
|
end
|
153
101
|
|
154
|
-
describe "
|
155
|
-
subject(:
|
156
|
-
|
157
|
-
|
158
|
-
id: "test_callback_id",
|
102
|
+
describe ".find_or_create" do
|
103
|
+
subject(:find_or_create) {
|
104
|
+
described_class.find_or_create(
|
105
|
+
id: callback_id,
|
159
106
|
class_name: "Test",
|
160
107
|
user: user,
|
161
108
|
channel_id: "test_channel_id",
|
162
|
-
payload: { test: "test
|
109
|
+
payload: { test: "test" },
|
163
110
|
config: config
|
164
111
|
)
|
165
112
|
}
|
166
113
|
|
167
|
-
|
168
|
-
|
169
|
-
args: "",
|
114
|
+
let(:cached_data) {
|
115
|
+
{
|
170
116
|
class_name: "Test",
|
171
|
-
user_id:
|
117
|
+
user_id: user.id,
|
172
118
|
channel_id: "test_channel_id",
|
173
|
-
payload: { test: "test
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
let(:payload) { { test: "test payload 2" } }
|
119
|
+
payload: { test: "test" },
|
120
|
+
args: ""
|
121
|
+
}
|
122
|
+
}
|
178
123
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
expect(callback.args).to be_a(SlackBot::Args)
|
124
|
+
before do
|
125
|
+
allow_any_instance_of(described_class).to receive(:generate_id).and_return(callback_id)
|
126
|
+
allow(callback_storage_instance).to receive(:write).with("#{SlackBot::Callback::CALLBACK_KEY_PREFIX}:u#{user.id}:#{callback_id}", cached_data, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN).and_return(cached_data)
|
127
|
+
allow(callback_storage_instance).to receive(:read).with("#{SlackBot::Callback::CALLBACK_KEY_PREFIX}:u#{user.id}:#{callback_id}").and_return(cached_data)
|
184
128
|
end
|
185
|
-
end
|
186
|
-
|
187
|
-
describe "#destroy" do
|
188
|
-
|
189
|
-
end
|
190
|
-
|
191
|
-
describe "#user" do
|
192
|
-
|
193
|
-
end
|
194
|
-
|
195
|
-
describe "#handler_class" do
|
196
129
|
|
130
|
+
it "finds or creates callback" do
|
131
|
+
expect(find_or_create).to be_a(described_class)
|
132
|
+
expect(find_or_create.id).to eq(callback_id)
|
133
|
+
expect(find_or_create.class_name).to eq("Test")
|
134
|
+
expect(find_or_create.user).to eq(user)
|
135
|
+
expect(find_or_create.channel_id).to eq("test_channel_id")
|
136
|
+
expect(find_or_create.payload).to eq({ test: "test" })
|
137
|
+
expect(find_or_create.args).to be_a(SlackBot::Args)
|
138
|
+
end
|
197
139
|
end
|
198
140
|
end
|