grape-slack-bot 1.5.0 → 1.5.2

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: 0f8fc51cbef0f57cb132347ce71c02fba8ca5130749cca04ed3ffdbefb482ce1
4
- data.tar.gz: a1a96768a308005a8bf8aa709c3c3a29f927c31a624c4ad9bfc6d7e60b9b01ad
3
+ metadata.gz: '049b94a107e222f8e3662dc34cacef51262a1d50e4d0945bcad042a507352218'
4
+ data.tar.gz: 30e9fe036b313632de57ce8ea17ddf496569115dbeebf993d2c7d24bd34c7c19
5
5
  SHA512:
6
- metadata.gz: 2d372b748e6242a8766799b762afbfb205023491cb5c1fb6f453cc2454d389d89eecfb0e3f7257ef92b545b3215e579103c325dcc5ba0a40bb5a48f6ab4451d3
7
- data.tar.gz: 2c72725ce9911a95c74ae015e6f24aa77eae793afd97b7c2d5c934598c3c9195c61bd386bdd40661b2ce3d89bd875e426ed926c3ec711f709090ed2e591ffaf8
6
+ metadata.gz: 882f712999c11d787a68ff23b8c9c9a675191f4d95c56700fd06fb5020d11e23b39b532d364e511619d9976d63055e5365669994d596d1f370c51bc93c6d806c
7
+ data.tar.gz: 13fb5097d19007d8c907ccc59267c8b5f4d1c80878b58f665c8c8f5759677fdeafaa32160cb936a466e3b12b25cac6dce5be5cd3179a29b2414db3a3f24f0cdc
@@ -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
@@ -4,7 +4,8 @@ module SlackBot
4
4
  define_singleton_method(:view_klass) { klass }
5
5
  end
6
6
 
7
- attr_reader :current_user, :params, :callback, :config
7
+ attr_reader :current_user, :params, :config
8
+ attr_accessor :callback
8
9
  def initialize(current_user: nil, params: nil, callback: nil, config: nil)
9
10
  @current_user = current_user
10
11
  @params = params
@@ -28,6 +29,7 @@ module SlackBot
28
29
  self.class.view_klass
29
30
  .new(current_user: current_user, params: params, context: context)
30
31
  .send(view_method_name)
32
+ view = view.merge(callback_id: callback.id) if callback.present?
31
33
  response =
32
34
  SlackBot::ApiClient.new.views_publish(user_id: user_id, view: view)
33
35
 
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.2'.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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov