grape-slack-bot 1.3.0 → 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: 669dbf492365849f4628ed362f0a047e70d15cb80a62a236260be26d76fbaacc
4
- data.tar.gz: e17819f3afa4096b5623c4a8d19f6443522c2010d528ac6a14fefc17fb3c3309
3
+ metadata.gz: 0f8fc51cbef0f57cb132347ce71c02fba8ca5130749cca04ed3ffdbefb482ce1
4
+ data.tar.gz: a1a96768a308005a8bf8aa709c3c3a29f927c31a624c4ad9bfc6d7e60b9b01ad
5
5
  SHA512:
6
- metadata.gz: 05de3c891473d52722c368bd587e7a90b7d8e9eaa96c4f6bd59bd4bbb5f867c265e2fab1b2e95076abd8b75c0dd9460a51ea460909afd538e9c0c0962fe8250c
7
- data.tar.gz: c80d3a362202a526df44c32dad70270109ef344180d7d83d27cca9a24c9f308d422c7f8f91215a0a31b357ef2e86d14771b048f344051a7bc48ca27f80fa73d1
6
+ metadata.gz: 2d372b748e6242a8766799b762afbfb205023491cb5c1fb6f453cc2454d389d89eecfb0e3f7257ef92b545b3215e579103c325dcc5ba0a40bb5a48f6ab4451d3
7
+ data.tar.gz: 2c72725ce9911a95c74ae015e6f24aa77eae793afd97b7c2d5c934598c3c9195c61bd386bdd40661b2ce3d89bd875e426ed926c3ec711f709090ed2e591ffaf8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
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
+
1
9
  # 1.3.0
2
10
 
3
11
  * Clean up callback arguments, remove unused `method_name`
@@ -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,31 +15,33 @@ module SlackBot
14
15
  nil
15
16
  end
16
17
 
17
- def self.create(class_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, 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, 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
30
  user_id: user&.id,
30
31
  channel_id: channel_id,
31
- extra: extra
32
+ payload: payload
32
33
  }
33
34
  @args = SlackBot::Args.new
34
35
  @config = config || SlackBot::Config.current_instance
36
+ @expires_in = expires_in || CALLBACK_RECORD_EXPIRES_IN
35
37
  end
36
38
 
37
39
  def reload
38
- @data = read_data
39
- SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{data}")
40
- 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?
41
43
 
44
+ @data = cached_data
42
45
  parse_args
43
46
  self
44
47
  end
@@ -55,7 +58,12 @@ module SlackBot
55
58
  return if id.blank?
56
59
  return if data.blank?
57
60
 
58
- @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
+
59
67
  save
60
68
  end
61
69
 
@@ -72,6 +80,23 @@ module SlackBot
72
80
  end
73
81
  end
74
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
+
75
100
  def handler_class
76
101
  return if class_name.blank?
77
102
 
@@ -80,6 +105,7 @@ module SlackBot
80
105
 
81
106
  def method_missing(method_name, *args, &block)
82
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)
83
109
 
84
110
  super
85
111
  end
@@ -91,7 +117,7 @@ module SlackBot
91
117
  end
92
118
 
93
119
  def serialize_args
94
- data[:args] = args.to_s
120
+ @data[:args] = args.to_s
95
121
  end
96
122
 
97
123
  def generate_id
@@ -102,8 +128,9 @@ module SlackBot
102
128
  config.callback_storage_instance.read("#{CALLBACK_CACHE_KEY}:#{id}")
103
129
  end
104
130
 
105
- def write_data(data)
106
- 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)
107
134
  end
108
135
 
109
136
  def delete_data
@@ -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"
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.3.0'.freeze
25
+ VERSION = '1.5.0'.freeze
26
26
  end
@@ -6,7 +6,7 @@ describe SlackBot::Callback do
6
6
  class_name: "Test",
7
7
  user: user,
8
8
  channel_id: "test_channel_id",
9
- extra: { test: "test" },
9
+ payload: { test: "test" },
10
10
  config: config
11
11
  )
12
12
  }
@@ -33,7 +33,7 @@ describe SlackBot::Callback do
33
33
  end
34
34
 
35
35
  context "when callback is found" do
36
- let(:data) { { class_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: "" } }
37
37
 
38
38
  it "returns callback" do
39
39
  expect(find).to be_a(described_class)
@@ -42,7 +42,7 @@ describe SlackBot::Callback do
42
42
  expect(find.user).to eq(user)
43
43
  expect(find.user_id).to eq(1)
44
44
  expect(find.channel_id).to eq("test_channel_id")
45
- expect(find.extra).to eq({ test: "test" })
45
+ expect(find.payload).to eq({ test: "test" })
46
46
  end
47
47
  end
48
48
 
@@ -65,11 +65,11 @@ describe SlackBot::Callback do
65
65
  class_name: "Test",
66
66
  user_id: 1,
67
67
  channel_id: "test_channel_id",
68
- extra: nil
69
- }, expires_in: 1.hour)
68
+ payload: nil
69
+ }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
70
70
  end
71
71
 
72
- let(:data) { { class_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 } }
73
73
 
74
74
  it "returns callback" do
75
75
  expect(create).to be_a(described_class)
@@ -78,7 +78,7 @@ describe SlackBot::Callback do
78
78
  expect(create.user).to eq(user)
79
79
  expect(create.user_id).to eq(1)
80
80
  expect(create.channel_id).to eq("test_channel_id")
81
- expect(create.extra).to eq(nil)
81
+ expect(create.payload).to eq(nil)
82
82
  end
83
83
  end
84
84
 
@@ -92,7 +92,7 @@ describe SlackBot::Callback do
92
92
  class_name: "Test",
93
93
  user_id: 1,
94
94
  channel_id: "test_channel_id",
95
- extra: { test: "test" },
95
+ payload: { test: "test payload" },
96
96
  args: ""
97
97
  }
98
98
  }
@@ -108,7 +108,7 @@ describe SlackBot::Callback do
108
108
  expect(reload.user).to eq(user)
109
109
  expect(reload.user_id).to eq(1)
110
110
  expect(reload.channel_id).to eq("test_channel_id")
111
- expect(reload.extra).to eq({ test: "test" })
111
+ expect(reload.payload).to eq({ test: "test payload" })
112
112
  end
113
113
 
114
114
  context "when callback is not found" do
@@ -127,7 +127,7 @@ describe SlackBot::Callback do
127
127
  class_name: "Test",
128
128
  user: user,
129
129
  channel_id: "test_channel_id",
130
- extra: { test: "test" },
130
+ payload: { test: "test payload" },
131
131
  config: config
132
132
  )
133
133
  }
@@ -139,12 +139,12 @@ describe SlackBot::Callback do
139
139
  class_name: "Test",
140
140
  user_id: 1,
141
141
  channel_id: "test_channel_id",
142
- extra: { test: "test" }
143
- }, expires_in: 1.hour)
142
+ payload: { test: "test payload" }
143
+ }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
144
144
  end
145
145
 
146
146
  it "returns callback" do
147
- expect { save }.not_to raise_error
147
+ save
148
148
  expect(callback).to be_a(described_class)
149
149
  expect(callback.id).to eq("test_callback_id")
150
150
  expect(callback.args).to be_a(SlackBot::Args)
@@ -159,7 +159,7 @@ describe SlackBot::Callback do
159
159
  class_name: "Test",
160
160
  user: user,
161
161
  channel_id: "test_channel_id",
162
- extra: { test: "test" },
162
+ payload: { test: "test payload" },
163
163
  config: config
164
164
  )
165
165
  }
@@ -170,15 +170,14 @@ describe SlackBot::Callback do
170
170
  class_name: "Test",
171
171
  user_id: 1,
172
172
  channel_id: "test_channel_id",
173
- extra: { test: "test" },
174
- test: "test"
175
- }, expires_in: 1.hour)
173
+ payload: { test: "test payload 2" }
174
+ }, expires_in: SlackBot::Callback::CALLBACK_RECORD_EXPIRES_IN)
176
175
  end
177
176
 
178
- let(:payload) { { test: "test" } }
177
+ let(:payload) { { test: "test payload 2" } }
179
178
 
180
179
  it "returns callback" do
181
- expect { update }.not_to raise_error
180
+ update
182
181
  expect(callback).to be_a(described_class)
183
182
  expect(callback.id).to eq("test_callback_id")
184
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.3.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov