grape-slack-bot 1.5.0 → 1.5.1

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: 0f8fc51cbef0f57cb132347ce71c02fba8ca5130749cca04ed3ffdbefb482ce1
4
- data.tar.gz: a1a96768a308005a8bf8aa709c3c3a29f927c31a624c4ad9bfc6d7e60b9b01ad
3
+ metadata.gz: 2d248818a6772bc6b90aaaff5e6a32f34ed8f1be71d3865435929e018f30d48b
4
+ data.tar.gz: 8719e28b6995efa871e4a834aa5ac884cbda4133034805e256ddf53786c1fdb2
5
5
  SHA512:
6
- metadata.gz: 2d372b748e6242a8766799b762afbfb205023491cb5c1fb6f453cc2454d389d89eecfb0e3f7257ef92b545b3215e579103c325dcc5ba0a40bb5a48f6ab4451d3
7
- data.tar.gz: 2c72725ce9911a95c74ae015e6f24aa77eae793afd97b7c2d5c934598c3c9195c61bd386bdd40661b2ce3d89bd875e426ed926c3ec711f709090ed2e591ffaf8
6
+ metadata.gz: 3a20d2e0f603d9f5a1cbfc24d8e08e9907b12d6d677442d63160cf0a5f57b3dc56234bd65b0d979749574e290a3a3fb00b8f014bf219c8bf969e2c4d1cd7f7f3
7
+ data.tar.gz: f3401998a0b1cd270e952107d86f2da78de98cc45b4260d7205998ff173e00f356d09185d08f50416bebe5b38593fe9cb40e829b2f4a4454bc3dafd53414227b
@@ -3,27 +3,34 @@ require 'active_support/core_ext/numeric/time'
3
3
 
4
4
  module SlackBot
5
5
  class Callback
6
- CALLBACK_CACHE_KEY = "slack-bot-callback".freeze
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
- attr_reader :id, :data, :args, :config, :expires_in
26
- def initialize(id: nil, class_name: nil, user: nil, channel_id: nil, payload: nil, config: nil, expires_in: nil)
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.uuid
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("#{CALLBACK_CACHE_KEY}:#{id}")
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("#{CALLBACK_CACHE_KEY}:#{id}", data, expires_in: expires_in)
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("#{CALLBACK_CACHE_KEY}:#{id}")
155
+ config.callback_storage_instance.delete(storage_key)
138
156
  end
139
157
  end
140
158
  end
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.5.0'.freeze
25
+ VERSION = '1.5.1'.freeze
26
26
  end
@@ -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
- before do
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: 1,
34
+ user_id: user.id,
94
35
  channel_id: "test_channel_id",
95
- payload: { test: "test payload" },
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("slack-bot-callback:test_callback_id").and_return(data)
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(reload).to be_a(described_class)
106
- expect(reload.id).to eq("test_callback_id")
107
- expect(reload.class_name).to eq("Test")
108
- expect(reload.user).to eq(user)
109
- expect(reload.user_id).to eq(1)
110
- expect(reload.channel_id).to eq("test_channel_id")
111
- expect(reload.payload).to eq({ test: "test payload" })
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(:data) { nil }
56
+ let(:cached_data) { nil }
116
57
 
117
- it "raises error" do
118
- expect { reload }.to raise_error(SlackBot::Errors::CallbackNotFound)
58
+ it "returns nil" do
59
+ expect(find).to be_nil
119
60
  end
120
61
  end
121
62
  end
122
63
 
123
- describe "#save" do
124
- subject(:save) { callback.save }
125
- let(:callback) {
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 payload" },
70
+ payload: { test: "test" },
131
71
  config: config
132
72
  )
133
73
  }
134
74
 
135
- before do
136
- allow_any_instance_of(described_class).to receive(:generate_id).and_return("test_callback_id")
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: 1,
78
+ user_id: user.id,
141
79
  channel_id: "test_channel_id",
142
- payload: { test: "test payload" }
143
- }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
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 "returns callback" do
147
- save
148
- expect(callback).to be_a(described_class)
149
- expect(callback.id).to eq("test_callback_id")
150
- expect(callback.args).to be_a(SlackBot::Args)
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 "#update" do
155
- subject(:update) { callback.update(payload) }
156
- let(:callback) {
157
- described_class.new(
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 payload" },
109
+ payload: { test: "test" },
163
110
  config: config
164
111
  )
165
112
  }
166
113
 
167
- before do
168
- allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
169
- args: "",
114
+ let(:cached_data) {
115
+ {
170
116
  class_name: "Test",
171
- user_id: 1,
117
+ user_id: user.id,
172
118
  channel_id: "test_channel_id",
173
- payload: { test: "test payload 2" }
174
- }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
175
- end
176
-
177
- let(:payload) { { test: "test payload 2" } }
119
+ payload: { test: "test" },
120
+ args: ""
121
+ }
122
+ }
178
123
 
179
- it "returns callback" do
180
- update
181
- expect(callback).to be_a(described_class)
182
- expect(callback.id).to eq("test_callback_id")
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
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.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov