grape-slack-bot 1.2.0 → 1.2.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: f84c93d79a89045d58deda55e64382c8ef74daf4975e0f47732b480c08619020
4
- data.tar.gz: 6f3e952d9a8f85e48de8f045aab1c35f0d69e9ac39b8328c240f38c77c578ad0
3
+ metadata.gz: 99c07a4830556a1a896dea28c85f7fade663c8b49b6e649d084e2bf115a8eacd
4
+ data.tar.gz: 6dcce44e9edaf18c97f62dbf65fa248d6b309caad9e9ed6dcf7cdf327a16f063
5
5
  SHA512:
6
- metadata.gz: 458e378569349642ecf2433fd573d3ad40ef69def84a6253206ff5bcef2b9d983c24b0d1e53a30887de7a8203dcf733ba868bbbbc9b4e215ded1f71487c3dd12
7
- data.tar.gz: dd5ef3106d1a1255cb788bef313d905ca986133bc5b3b3e53166c70b9727f8d3b996cce595f724e80b9d08ec4338ca91fbedcf135945878c01a61d32872375ef
6
+ metadata.gz: 64c24157366d85a69449488dde1aa029f562afde69e0b3c210241d2e4778675842ed9003012f9f19193e0ef04d5a45f48f0acd70bfae9a55cc831667d4c0ee25
7
+ data.tar.gz: a399297e42b45b7be8bd05587abeb9150fba0276953c1771a46c05bbaee7b29361181cb16fc5b672d58bd68fcfe869b706166dd664261b61ac86e45bd633b3a7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 1.2.2
2
+
3
+ * `SlackBot::Callback.find` method will raise `SlackBot::Errors::CallbackNotFound` if callback is not resolved or has wrong data
4
+
5
+ # 1.2.1
6
+
7
+ * Extract `SlackBot::Logger` to separate file
8
+
1
9
  # 1.2.0
2
10
 
3
11
  * Remove `Rails.logger` dependency, make logger configurable
@@ -6,8 +6,12 @@ module SlackBot
6
6
  CALLBACK_CACHE_KEY = "slack-bot-callback".freeze
7
7
 
8
8
  def self.find(id, config: nil)
9
+ return if id.blank?
10
+
9
11
  callback = new(id: id, config: config)
10
12
  callback.reload
13
+ rescue SlackBot::Errors::CallbackNotFound
14
+ nil
11
15
  end
12
16
 
13
17
  def self.create(class_name:, method_name:, user:, channel_id: nil, config: nil)
@@ -34,6 +38,7 @@ module SlackBot
34
38
  def reload
35
39
  @data = read_data
36
40
  SlackBot::DevConsole.log_check("SlackBot::Callback#read_data: #{id} | #{data}")
41
+ raise SlackBot::Errors::CallbackNotFound if data.nil?
37
42
 
38
43
  parse_args
39
44
  self
@@ -83,7 +88,7 @@ module SlackBot
83
88
  private
84
89
 
85
90
  def parse_args
86
- args.raw_args = data&.dig(:args)
91
+ args.raw_args = data.fetch(:args)
87
92
  end
88
93
 
89
94
  def serialize_args
@@ -1,9 +1,4 @@
1
1
  module SlackBot
2
- class Logger
3
- def info(*args, **kwargs)
4
- puts args, kwargs
5
- end
6
- end
7
2
  class DevConsole
8
3
  def self.enabled=(value)
9
4
  @enabled = value
@@ -18,6 +18,9 @@ module SlackBot
18
18
  class MenuOptionsNotImplemented < SlackBot::Error
19
19
  end
20
20
 
21
+ class CallbackNotFound < SlackBot::Error
22
+ end
23
+
21
24
  class SlackResponseError < SlackBot::Error
22
25
  attr_reader :error, :data, :payload
23
26
  def initialize(error, data: nil, payload: nil)
@@ -0,0 +1,8 @@
1
+ module SlackBot
2
+ class Logger
3
+ def info(*args, **kwargs)
4
+ puts args.inspect if args.any?
5
+ puts kwargs.inspect if kwargs.any?
6
+ end
7
+ end
8
+ end
data/lib/slack_bot.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'slack_bot/logger'
1
2
  require 'slack_bot/dev_console'
2
3
 
3
4
  require 'slack_bot/config'
@@ -21,5 +22,5 @@ require 'slack_bot/pager'
21
22
  require 'slack_bot/grape_extension'
22
23
 
23
24
  module SlackBot
24
- VERSION = '1.2.0'.freeze
25
+ VERSION = '1.2.2'.freeze
25
26
  end
@@ -34,7 +34,7 @@ describe SlackBot::Callback do
34
34
  end
35
35
 
36
36
  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" } } }
37
+ let(:data) { { class_name: "Test", method_name: "test", user_id: 1, channel_id: "test_channel_id", extra: { test: "test" }, args: "" } }
38
38
 
39
39
  it "returns callback" do
40
40
  expect(find).to be_a(described_class)
@@ -47,6 +47,14 @@ describe SlackBot::Callback do
47
47
  expect(find.extra).to eq({ test: "test" })
48
48
  end
49
49
  end
50
+
51
+ context "when callback is not found" do
52
+ let(:data) { nil }
53
+
54
+ it "returns nil" do
55
+ expect(find).to eq(nil)
56
+ end
57
+ end
50
58
  end
51
59
 
52
60
  describe ".create" do
@@ -79,15 +87,112 @@ describe SlackBot::Callback do
79
87
  end
80
88
 
81
89
  describe "#reload" do
90
+ subject(:reload) { callback.reload }
91
+ let(:callback) {
92
+ described_class.new(id: "test_callback_id", config: config)
93
+ }
94
+ let(:data) {
95
+ {
96
+ class_name: "Test",
97
+ method_name: "test",
98
+ user_id: 1,
99
+ channel_id: "test_channel_id",
100
+ extra: { test: "test" },
101
+ args: ""
102
+ }
103
+ }
104
+
105
+ before do
106
+ allow(callback_storage_instance).to receive(:read).with("slack-bot-callback:test_callback_id").and_return(data)
107
+ end
82
108
 
109
+ it "returns callback" do
110
+ expect(reload).to be_a(described_class)
111
+ expect(reload.id).to eq("test_callback_id")
112
+ expect(reload.class_name).to eq("Test")
113
+ expect(reload.user).to eq(user)
114
+ expect(reload.user_id).to eq(1)
115
+ 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" })
118
+ end
119
+
120
+ context "when callback is not found" do
121
+ let(:data) { nil }
122
+
123
+ it "raises error" do
124
+ expect { reload }.to raise_error(SlackBot::Errors::CallbackNotFound)
125
+ end
126
+ end
83
127
  end
84
128
 
85
129
  describe "#save" do
130
+ subject(:save) { callback.save }
131
+ let(:callback) {
132
+ described_class.new(
133
+ class_name: "Test",
134
+ method_name: "test",
135
+ user: user,
136
+ channel_id: "test_channel_id",
137
+ extra: { test: "test" },
138
+ config: config
139
+ )
140
+ }
141
+
142
+ before do
143
+ allow_any_instance_of(described_class).to receive(:generate_id).and_return("test_callback_id")
144
+ allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
145
+ args: "",
146
+ class_name: "Test",
147
+ method_name: "test",
148
+ user_id: 1,
149
+ channel_id: "test_channel_id",
150
+ extra: { test: "test" }
151
+ }, expires_in: 1.hour)
152
+ end
86
153
 
154
+ it "returns callback" do
155
+ expect { save }.not_to raise_error
156
+ expect(callback).to be_a(described_class)
157
+ expect(callback.id).to eq("test_callback_id")
158
+ expect(callback.args).to be_a(SlackBot::Args)
159
+ end
87
160
  end
88
161
 
89
162
  describe "#update" do
163
+ subject(:update) { callback.update(payload) }
164
+ let(:callback) {
165
+ described_class.new(
166
+ id: "test_callback_id",
167
+ class_name: "Test",
168
+ method_name: "test",
169
+ user: user,
170
+ channel_id: "test_channel_id",
171
+ extra: { test: "test" },
172
+ config: config
173
+ )
174
+ }
90
175
 
176
+ before do
177
+ allow(callback_storage_instance).to receive(:write).with("slack-bot-callback:test_callback_id", {
178
+ args: "",
179
+ class_name: "Test",
180
+ method_name: "test",
181
+ user_id: 1,
182
+ channel_id: "test_channel_id",
183
+ extra: { test: "test" },
184
+ test: "test"
185
+ }, expires_in: 1.hour)
186
+ end
187
+
188
+ let(:payload) { { test: "test" } }
189
+
190
+ it "returns callback" do
191
+ expect { update }.not_to raise_error
192
+ expect(callback).to be_a(described_class)
193
+ expect(callback.id).to eq("test_callback_id")
194
+ expect(callback.args).to be_a(SlackBot::Args)
195
+ end
91
196
  end
92
197
 
93
198
  describe "#destroy" do
@@ -1,50 +1,3 @@
1
- module SlackBot
2
- class Logger
3
- def info(*args, **kwargs)
4
- puts args, kwargs
5
- end
6
- end
7
- class DevConsole
8
- def self.enabled=(value)
9
- @enabled = value
10
- end
11
-
12
- def self.enabled?
13
- @enabled
14
- end
15
-
16
- def self.logger=(value)
17
- @logger = value
18
- end
19
-
20
- def self.logger
21
- @logger ||= Logger.new
22
- end
23
-
24
- def self.log(message = nil, &block)
25
- return unless enabled?
26
-
27
- message = yield if block_given?
28
- logger.info(message)
29
- end
30
-
31
- def self.log_input(message = nil, &block)
32
- message = yield if block_given?
33
- log(">>> #{message}")
34
- end
35
-
36
- def self.log_output(message = nil, &block)
37
- message = yield if block_given?
38
- log("<<< #{message}")
39
- end
40
-
41
- def self.log_check(message = nil, &block)
42
- message = yield if block_given?
43
- log("!!! #{message}")
44
- end
45
- end
46
- end
47
-
48
1
  require 'spec_helper'
49
2
 
50
3
  describe SlackBot::DevConsole do
@@ -85,4 +38,58 @@ describe SlackBot::DevConsole do
85
38
  end
86
39
  end
87
40
  end
41
+
42
+ describe '.log_input' do
43
+ context 'when enabled' do
44
+ before { described_class.enabled = true }
45
+ it 'logs the message' do
46
+ expect(described_class.logger).to receive(:info).with('>>> test')
47
+ described_class.log_input('test')
48
+ end
49
+ end
50
+
51
+ context 'when disabled' do
52
+ before { described_class.enabled = false }
53
+ it 'does not log the message' do
54
+ expect(described_class.logger).not_to receive(:info)
55
+ described_class.log_input('test')
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '.log_output' do
61
+ context 'when enabled' do
62
+ before { described_class.enabled = true }
63
+ it 'logs the message' do
64
+ expect(described_class.logger).to receive(:info).with('<<< test')
65
+ described_class.log_output('test')
66
+ end
67
+ end
68
+
69
+ context 'when disabled' do
70
+ before { described_class.enabled = false }
71
+ it 'does not log the message' do
72
+ expect(described_class.logger).not_to receive(:info)
73
+ described_class.log_output('test')
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '.log_check' do
79
+ context 'when enabled' do
80
+ before { described_class.enabled = true }
81
+ it 'logs the message' do
82
+ expect(described_class.logger).to receive(:info).with('!!! test')
83
+ described_class.log_check('test')
84
+ end
85
+ end
86
+
87
+ context 'when disabled' do
88
+ before { described_class.enabled = false }
89
+ it 'does not log the message' do
90
+ expect(described_class.logger).not_to receive(:info)
91
+ described_class.log_check('test')
92
+ end
93
+ end
94
+ end
88
95
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe SlackBot::Logger do
4
+ describe '#info' do
5
+ it 'prints the args' do
6
+ expect { subject.info('test') }.to output(%(["test"]\n)).to_stdout
7
+ end
8
+
9
+ it 'prints the kwargs' do
10
+ expect { subject.info(test: 'test') }.to output(%({:test=>"test"}\n)).to_stdout
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,51 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SlackBot::Pager do
4
+ let(:args) do
5
+ instance_double(SlackBot::Args)
6
+ end
7
+ before do
8
+ allow(args).to receive(:[]).with(:page).and_return(1)
9
+ allow(args).to receive(:[]).with(:per_page).and_return(10)
10
+ end
4
11
 
12
+ describe '#total_count' do
13
+ it 'returns the count of the source cursor' do
14
+ source_cursor = double(count: 10)
15
+ pager = described_class.new(source_cursor, args: args)
16
+ expect(pager.total_count).to eq(10)
17
+ end
18
+ end
19
+
20
+ describe '#pages_count' do
21
+ it 'returns the count of the source cursor divided by the limit' do
22
+ source_cursor = double(count: 10)
23
+ pager = described_class.new(source_cursor, args: args, limit: 5)
24
+ expect(pager.pages_count).to eq(2)
25
+ end
26
+
27
+ it 'returns the count of the source cursor divided by the limit' do
28
+ source_cursor = double(count: 11)
29
+ pager = described_class.new(source_cursor, args: args, limit: 5)
30
+ expect(pager.pages_count).to eq(3)
31
+ end
32
+ end
33
+
34
+ describe '#offset' do
35
+ it 'returns the offset based on the page and limit' do
36
+ source_cursor = double(count: 10)
37
+ pager = described_class.new(source_cursor, args: args, limit: 5, page: 2)
38
+ expect(pager.offset).to eq(5)
39
+ end
40
+ end
41
+
42
+ describe '#cursor' do
43
+ it 'returns the cursor with the limit and offset' do
44
+ source_cursor = double(count: 10)
45
+ expect(source_cursor).to receive(:limit).with(5).and_return(source_cursor)
46
+ expect(source_cursor).to receive(:offset).with(5).and_return(source_cursor)
47
+ pager = described_class.new(source_cursor, args: args, limit: 5, page: 2)
48
+ expect(pager.cursor).to eq(source_cursor)
49
+ end
50
+ end
5
51
  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.2.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov
@@ -174,6 +174,7 @@ files:
174
174
  - lib/slack_bot/event.rb
175
175
  - lib/slack_bot/grape_extension.rb
176
176
  - lib/slack_bot/interaction.rb
177
+ - lib/slack_bot/logger.rb
177
178
  - lib/slack_bot/menu_options.rb
178
179
  - lib/slack_bot/pager.rb
179
180
  - lib/slack_bot/view.rb
@@ -186,6 +187,7 @@ files:
186
187
  - spec/slack_bot/event_spec.rb
187
188
  - spec/slack_bot/grape_extension_spec.rb
188
189
  - spec/slack_bot/interaction_spec.rb
190
+ - spec/slack_bot/logger_spec.rb
189
191
  - spec/slack_bot/menu_options_spec.rb
190
192
  - spec/slack_bot/pager_spec.rb
191
193
  - spec/slack_bot/view_spec.rb
@@ -228,6 +230,7 @@ test_files:
228
230
  - spec/slack_bot/event_spec.rb
229
231
  - spec/slack_bot/grape_extension_spec.rb
230
232
  - spec/slack_bot/interaction_spec.rb
233
+ - spec/slack_bot/logger_spec.rb
231
234
  - spec/slack_bot/menu_options_spec.rb
232
235
  - spec/slack_bot/pager_spec.rb
233
236
  - spec/slack_bot/view_spec.rb