grape-slack-bot 1.2.3 → 1.5.0

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: 3472ece85beebd447e192f73013849e3e37a366fdc22b4f4864ef0908f58f502
4
- data.tar.gz: 7f25928036698338c354238d9615ccd980d136a0c88056be1a1989304c95eae9
3
+ metadata.gz: 0f8fc51cbef0f57cb132347ce71c02fba8ca5130749cca04ed3ffdbefb482ce1
4
+ data.tar.gz: a1a96768a308005a8bf8aa709c3c3a29f927c31a624c4ad9bfc6d7e60b9b01ad
5
5
  SHA512:
6
- metadata.gz: a2902bfc733cd9aec81df154e0c43ac2bfe9eca7fd6105cb7b6d2f1ea1631daae093e2a96698388accaf35b46c4da826218333bab3c78bed7781c17583e01312
7
- data.tar.gz: 04a70f1a3a5251d44d2b4bb21cdaa113296d50c51b0fae010f27f5c1a7caeb5b41eb635c6d349672701a5aef13c58f39249f8e740a2310ab5024dd822e7341ce
6
+ metadata.gz: 2d372b748e6242a8766799b762afbfb205023491cb5c1fb6f453cc2454d389d89eecfb0e3f7257ef92b545b3215e579103c325dcc5ba0a40bb5a48f6ab4451d3
7
+ data.tar.gz: 2c72725ce9911a95c74ae015e6f24aa77eae793afd97b7c2d5c934598c3c9195c61bd386bdd40661b2ce3d89bd875e426ed926c3ec711f709090ed2e591ffaf8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 1.5.0
2
+
3
+ * Complete upgrade of callback storage logic
4
+
5
+ # 1.4.0
6
+
7
+ * Allow setting callback expiration time on save and update
8
+
9
+ # 1.3.0
10
+
11
+ * Clean up callback arguments, remove unused `method_name`
12
+
1
13
  # 1.2.3
2
14
 
3
15
  * Minor fix for Events API
@@ -4,6 +4,7 @@ require 'active_support/core_ext/numeric/time'
4
4
  module SlackBot
5
5
  class Callback
6
6
  CALLBACK_CACHE_KEY = "slack-bot-callback".freeze
7
+ CALLBACK_RECORD_EXPIRES_IN = 15.minutes.freeze
7
8
 
8
9
  def self.find(id, config: nil)
9
10
  return if id.blank?
@@ -14,32 +15,33 @@ module SlackBot
14
15
  nil
15
16
  end
16
17
 
17
- def self.create(class_name:, method_name:, user:, channel_id: nil, config: nil)
18
+ def self.create(class_name:, user:, channel_id: nil, config: nil, payload: nil, expires_in: nil)
18
19
  callback =
19
- new(class_name: class_name, method_name: method_name, user: user, channel_id: channel_id, config: config)
20
+ new(class_name: class_name, user: user, channel_id: channel_id, payload: payload, config: config, expires_in: expires_in)
20
21
  callback.save
21
22
  callback
22
23
  end
23
24
 
24
- attr_reader :id, :data, :args, :config
25
- def initialize(id: nil, class_name: nil, method_name: nil, user: nil, channel_id: nil, extra: nil, config: nil)
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)
26
27
  @id = id
27
28
  @data = {
28
29
  class_name: class_name,
29
- method_name: method_name,
30
30
  user_id: user&.id,
31
31
  channel_id: channel_id,
32
- extra: extra
32
+ payload: payload
33
33
  }
34
34
  @args = SlackBot::Args.new
35
35
  @config = config || SlackBot::Config.current_instance
36
+ @expires_in = expires_in || CALLBACK_RECORD_EXPIRES_IN
36
37
  end
37
38
 
38
39
  def reload
39
- @data = read_data
40
- SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{data}")
41
- raise SlackBot::Errors::CallbackNotFound if data.nil?
40
+ cached_data = read_data
41
+ SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{cached_data}")
42
+ raise SlackBot::Errors::CallbackNotFound if cached_data.nil?
42
43
 
44
+ @data = cached_data
43
45
  parse_args
44
46
  self
45
47
  end
@@ -56,7 +58,12 @@ module SlackBot
56
58
  return if id.blank?
57
59
  return if data.blank?
58
60
 
59
- @data = data.merge(payload)
61
+ if @data[:payload].is_a?(Hash)
62
+ @data[:payload] = @data[:payload].merge(payload)
63
+ else
64
+ @data[:payload] = payload
65
+ end
66
+
60
67
  save
61
68
  end
62
69
 
@@ -73,6 +80,23 @@ module SlackBot
73
80
  end
74
81
  end
75
82
 
83
+ def user=(user)
84
+ @user = user
85
+ @data[:user_id] = user&.id
86
+ end
87
+
88
+ def class_name=(class_name)
89
+ @data[:class_name] = class_name
90
+ end
91
+
92
+ def channel_id=(channel_id)
93
+ @data[:channel_id] = channel_id
94
+ end
95
+
96
+ def payload=(payload)
97
+ @data[:payload] = payload
98
+ end
99
+
76
100
  def handler_class
77
101
  return if class_name.blank?
78
102
 
@@ -81,6 +105,7 @@ module SlackBot
81
105
 
82
106
  def method_missing(method_name, *args, &block)
83
107
  return data[method_name.to_sym] if data.key?(method_name.to_sym)
108
+ return data[:payload][method_name.to_s] if data[:payload].is_a?(Hash) && data[:payload].key?(method_name.to_s)
84
109
 
85
110
  super
86
111
  end
@@ -92,7 +117,7 @@ module SlackBot
92
117
  end
93
118
 
94
119
  def serialize_args
95
- data[:args] = args.to_s
120
+ @data[:args] = args.to_s
96
121
  end
97
122
 
98
123
  def generate_id
@@ -103,8 +128,9 @@ module SlackBot
103
128
  config.callback_storage_instance.read("#{CALLBACK_CACHE_KEY}:#{id}")
104
129
  end
105
130
 
106
- def write_data(data)
107
- config.callback_storage_instance.write("#{CALLBACK_CACHE_KEY}:#{id}", data, expires_in: 1.hour)
131
+ def write_data(data, expires_in: nil)
132
+ expires_in ||= CALLBACK_RECORD_EXPIRES_IN
133
+ config.callback_storage_instance.write("#{CALLBACK_CACHE_KEY}:#{id}", data, expires_in: expires_in)
108
134
  end
109
135
 
110
136
  def delete_data
@@ -48,7 +48,7 @@ module SlackBot
48
48
 
49
49
  private
50
50
 
51
- def open_modal(view_name, method_name: nil, context: nil)
51
+ def open_modal(view_name, context: nil)
52
52
  view = self.class.view_klass.new(
53
53
  args: args,
54
54
  current_user: @current_user,
@@ -61,7 +61,6 @@ module SlackBot
61
61
  trigger_id: params[:trigger_id],
62
62
  channel_id: params[:channel_id],
63
63
  class_name: self.class.name,
64
- method_name: method_name,
65
64
  user: @current_user,
66
65
  payload: payload,
67
66
  config: config
@@ -88,7 +88,7 @@ module SlackBot
88
88
  callback = SlackBot::Callback.find(callback_id, config: config)
89
89
  raise SlackBot::Errors::CallbackNotFound.new if callback.blank?
90
90
 
91
- SlackBot::DevConsole.log_check "SlackApi::Interactions##{__method__}: #{callback.id} #{callback.extra} #{callback.user_id} #{user&.id}"
91
+ SlackBot::DevConsole.log_check "SlackApi::Interactions##{__method__}: #{callback.id} #{callback.payload} #{callback.user_id} #{user&.id}"
92
92
 
93
93
  if callback.user_id != user.id
94
94
  raise "Callback user is not equal to action user"
@@ -12,7 +12,6 @@ module SlackBot
12
12
  trigger_id:,
13
13
  payload:,
14
14
  class_name:,
15
- method_name:,
16
15
  user:,
17
16
  channel_id:,
18
17
  config: nil
@@ -20,7 +19,6 @@ module SlackBot
20
19
  callback =
21
20
  Callback.create(
22
21
  class_name: class_name,
23
- method_name: method_name,
24
22
  user: user,
25
23
  channel_id: channel_id,
26
24
  config: config
@@ -45,7 +43,6 @@ module SlackBot
45
43
  view_id:,
46
44
  payload:,
47
45
  class_name: nil,
48
- method_name: nil,
49
46
  user: nil,
50
47
  channel_id: nil,
51
48
  config: nil
@@ -54,7 +51,6 @@ module SlackBot
54
51
  callback ||=
55
52
  Callback.create(
56
53
  class_name: class_name,
57
- method_name: method_name,
58
54
  user: user,
59
55
  channel_id: channel_id,
60
56
  config: config
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.3'.freeze
25
+ VERSION = '1.5.0'.freeze
26
26
  end
@@ -4,10 +4,9 @@ describe SlackBot::Callback do
4
4
  subject(:callback) {
5
5
  described_class.new(
6
6
  class_name: "Test",
7
- method_name: "test",
8
7
  user: user,
9
8
  channel_id: "test_channel_id",
10
- extra: { test: "test" },
9
+ payload: { test: "test" },
11
10
  config: config
12
11
  )
13
12
  }
@@ -34,7 +33,7 @@ describe SlackBot::Callback do
34
33
  end
35
34
 
36
35
  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" }, args: "" } }
36
+ let(:data) { { class_name: "Test", user_id: 1, channel_id: "test_channel_id", payload: { test: "test" }, args: "" } }
38
37
 
39
38
  it "returns callback" do
40
39
  expect(find).to be_a(described_class)
@@ -43,8 +42,7 @@ describe SlackBot::Callback do
43
42
  expect(find.user).to eq(user)
44
43
  expect(find.user_id).to eq(1)
45
44
  expect(find.channel_id).to eq("test_channel_id")
46
- expect(find.method_name).to eq("test")
47
- expect(find.extra).to eq({ test: "test" })
45
+ expect(find.payload).to eq({ test: "test" })
48
46
  end
49
47
  end
50
48
 
@@ -58,21 +56,20 @@ describe SlackBot::Callback do
58
56
  end
59
57
 
60
58
  describe ".create" do
61
- subject(:create) { described_class.create(class_name: "Test", method_name: "test", user: user, channel_id: "test_channel_id", config: config) }
59
+ subject(:create) { described_class.create(class_name: "Test", user: user, channel_id: "test_channel_id", config: config) }
62
60
 
63
61
  before do
64
62
  allow_any_instance_of(described_class).to receive(:generate_id).and_return("test_callback_id")
65
63
  allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
66
64
  args: "",
67
65
  class_name: "Test",
68
- method_name: "test",
69
66
  user_id: 1,
70
67
  channel_id: "test_channel_id",
71
- extra: nil
72
- }, expires_in: 1.hour)
68
+ payload: nil
69
+ }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
73
70
  end
74
71
 
75
- let(:data) { { class_name: "Test", method_name: "test", user_id: 1, channel_id: "test_channel_id", extra: nil } }
72
+ let(:data) { { class_name: "Test", user_id: 1, channel_id: "test_channel_id", payload: nil } }
76
73
 
77
74
  it "returns callback" do
78
75
  expect(create).to be_a(described_class)
@@ -81,8 +78,7 @@ describe SlackBot::Callback do
81
78
  expect(create.user).to eq(user)
82
79
  expect(create.user_id).to eq(1)
83
80
  expect(create.channel_id).to eq("test_channel_id")
84
- expect(create.method_name).to eq("test")
85
- expect(create.extra).to eq(nil)
81
+ expect(create.payload).to eq(nil)
86
82
  end
87
83
  end
88
84
 
@@ -94,10 +90,9 @@ describe SlackBot::Callback do
94
90
  let(:data) {
95
91
  {
96
92
  class_name: "Test",
97
- method_name: "test",
98
93
  user_id: 1,
99
94
  channel_id: "test_channel_id",
100
- extra: { test: "test" },
95
+ payload: { test: "test payload" },
101
96
  args: ""
102
97
  }
103
98
  }
@@ -113,8 +108,7 @@ describe SlackBot::Callback do
113
108
  expect(reload.user).to eq(user)
114
109
  expect(reload.user_id).to eq(1)
115
110
  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" })
111
+ expect(reload.payload).to eq({ test: "test payload" })
118
112
  end
119
113
 
120
114
  context "when callback is not found" do
@@ -131,10 +125,9 @@ describe SlackBot::Callback do
131
125
  let(:callback) {
132
126
  described_class.new(
133
127
  class_name: "Test",
134
- method_name: "test",
135
128
  user: user,
136
129
  channel_id: "test_channel_id",
137
- extra: { test: "test" },
130
+ payload: { test: "test payload" },
138
131
  config: config
139
132
  )
140
133
  }
@@ -144,15 +137,14 @@ describe SlackBot::Callback do
144
137
  allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
145
138
  args: "",
146
139
  class_name: "Test",
147
- method_name: "test",
148
140
  user_id: 1,
149
141
  channel_id: "test_channel_id",
150
- extra: { test: "test" }
151
- }, expires_in: 1.hour)
142
+ payload: { test: "test payload" }
143
+ }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
152
144
  end
153
145
 
154
146
  it "returns callback" do
155
- expect { save }.not_to raise_error
147
+ save
156
148
  expect(callback).to be_a(described_class)
157
149
  expect(callback.id).to eq("test_callback_id")
158
150
  expect(callback.args).to be_a(SlackBot::Args)
@@ -165,10 +157,9 @@ describe SlackBot::Callback do
165
157
  described_class.new(
166
158
  id: "test_callback_id",
167
159
  class_name: "Test",
168
- method_name: "test",
169
160
  user: user,
170
161
  channel_id: "test_channel_id",
171
- extra: { test: "test" },
162
+ payload: { test: "test payload" },
172
163
  config: config
173
164
  )
174
165
  }
@@ -177,18 +168,16 @@ describe SlackBot::Callback do
177
168
  allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
178
169
  args: "",
179
170
  class_name: "Test",
180
- method_name: "test",
181
171
  user_id: 1,
182
172
  channel_id: "test_channel_id",
183
- extra: { test: "test" },
184
- test: "test"
185
- }, expires_in: 1.hour)
173
+ payload: { test: "test payload 2" }
174
+ }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
186
175
  end
187
176
 
188
- let(:payload) { { test: "test" } }
177
+ let(:payload) { { test: "test payload 2" } }
189
178
 
190
179
  it "returns callback" do
191
- expect { update }.not_to raise_error
180
+ update
192
181
  expect(callback).to be_a(described_class)
193
182
  expect(callback.id).to eq("test_callback_id")
194
183
  expect(callback.args).to be_a(SlackBot::Args)
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.3
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov