grape-slack-bot 1.5.8 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/slack_bot/command.rb +12 -10
- data/lib/slack_bot/concerns/interaction_klass.rb +17 -0
- data/lib/slack_bot/concerns/pager_klass.rb +17 -0
- data/lib/slack_bot/concerns/view_klass.rb +17 -0
- data/lib/slack_bot/errors.rb +14 -0
- data/lib/slack_bot/event.rb +17 -11
- data/lib/slack_bot/interaction.rb +28 -15
- data/lib/slack_bot/view.rb +2 -7
- data/lib/slack_bot.rb +1 -1
- data/spec/slack_bot/event_spec.rb +69 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4cd56999a5a9b67729fe61e5f95a79f6a0ab89aeafaa43f7537b4501fe6b81a
|
4
|
+
data.tar.gz: 867a61f2c1f28c82f1c0d3e5bef97ecf6602176c7d89fe221d57c8161048b628
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bed9b092db2eaa13610c7e892663d2d5313321d49dc5e097b7278a1d12f9e47d812e101f153892b8b3d7a71cb5141a2fc189fd5689f54cbcd36ae747e7510f3
|
7
|
+
data.tar.gz: bce0459566f928181c2bef05062109d11f2633717c06e60841b2001c003bf0d317b92f234ed1eb7b0fdba0424120fd7fe6886f7753cedc3a2f784c3cd03a78c1
|
data/lib/slack_bot/command.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
+
require 'slack_bot/concerns/interaction_klass'
|
2
|
+
require 'slack_bot/concerns/view_klass'
|
3
|
+
|
1
4
|
module SlackBot
|
2
5
|
class Command
|
3
|
-
|
4
|
-
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.view(klass)
|
8
|
-
define_singleton_method(:view_klass) { klass }
|
9
|
-
end
|
6
|
+
include SlackBot::Concerns::InteractionKlass
|
7
|
+
include SlackBot::Concerns::ViewKlass
|
10
8
|
|
11
9
|
attr_reader :current_user, :params, :args, :config
|
12
10
|
def initialize(current_user:, params:, args:, config: nil)
|
@@ -48,7 +46,7 @@ module SlackBot
|
|
48
46
|
|
49
47
|
private
|
50
48
|
|
51
|
-
def
|
49
|
+
def render_view(view_name, context: nil)
|
52
50
|
view = self.class.view_klass.new(
|
53
51
|
args: args,
|
54
52
|
current_user: @current_user,
|
@@ -56,13 +54,17 @@ module SlackBot
|
|
56
54
|
context: context,
|
57
55
|
config: config
|
58
56
|
)
|
59
|
-
|
57
|
+
view.send(view_name)
|
58
|
+
end
|
59
|
+
|
60
|
+
def open_modal(view_name, context: nil)
|
61
|
+
view_payload = render_view(view_name, context: context)
|
60
62
|
self.class.interaction_klass.open_modal(
|
61
63
|
trigger_id: params[:trigger_id],
|
62
64
|
channel_id: params[:channel_id],
|
63
65
|
class_name: self.class.name,
|
64
66
|
user: @current_user,
|
65
|
-
payload:
|
67
|
+
payload: view_payload,
|
66
68
|
config: config
|
67
69
|
)
|
68
70
|
render_response
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SlackBot::Concerns
|
2
|
+
module InteractionKlass
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def interaction_klass
|
9
|
+
raise SlackBot::Errors::InteractionClassNotImplemented.new(name)
|
10
|
+
end
|
11
|
+
|
12
|
+
def interaction(klass)
|
13
|
+
define_singleton_method(:interaction_klass) { klass }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SlackBot::Concerns
|
2
|
+
module PagerKlass
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def pager_klass
|
9
|
+
SlackBot::Pager
|
10
|
+
end
|
11
|
+
|
12
|
+
def pager(klass)
|
13
|
+
define_singleton_method(:pager_klass) { klass }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SlackBot::Concerns
|
2
|
+
module ViewKlass
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def view_klass
|
9
|
+
raise SlackBot::Errors::ViewClassNotImplemented.new(name)
|
10
|
+
end
|
11
|
+
|
12
|
+
def view(klass)
|
13
|
+
define_singleton_method(:view_klass) { klass }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/slack_bot/errors.rb
CHANGED
@@ -31,6 +31,20 @@ module SlackBot
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
class InteractionClassNotImplemented < SlackBot::Error
|
35
|
+
attr_reader :class_name
|
36
|
+
def initialize(class_name)
|
37
|
+
@class_name = class_name
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class ViewClassNotImplemented < SlackBot::Error
|
42
|
+
attr_reader :class_name
|
43
|
+
def initialize(class_name)
|
44
|
+
@class_name = class_name
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
34
48
|
class SlackResponseError < SlackBot::Error
|
35
49
|
attr_reader :error, :data, :payload
|
36
50
|
def initialize(error, data: nil, payload: nil)
|
data/lib/slack_bot/event.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
+
require 'slack_bot/concerns/interaction_klass'
|
2
|
+
require 'slack_bot/concerns/view_klass'
|
3
|
+
|
1
4
|
module SlackBot
|
2
5
|
class Event
|
3
|
-
|
4
|
-
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.view(klass)
|
8
|
-
define_singleton_method(:view_klass) { klass }
|
9
|
-
end
|
6
|
+
include SlackBot::Concerns::InteractionKlass
|
7
|
+
include SlackBot::Concerns::ViewKlass
|
10
8
|
|
11
9
|
attr_reader :current_user, :params, :config, :callback, :metadata
|
12
10
|
def initialize(current_user: nil, params: nil, callback: nil, config: nil)
|
@@ -34,12 +32,20 @@ module SlackBot
|
|
34
32
|
params["event"]["type"]
|
35
33
|
end
|
36
34
|
|
35
|
+
def render_view(view_name, context: nil)
|
36
|
+
view = self.class.view_klass.new(
|
37
|
+
args: callback&.args,
|
38
|
+
current_user: current_user,
|
39
|
+
params: params,
|
40
|
+
context: context,
|
41
|
+
config: config
|
42
|
+
)
|
43
|
+
view.send(view_name)
|
44
|
+
end
|
45
|
+
|
37
46
|
def publish_view(view_method_name, context: nil)
|
38
47
|
user_id = params["event"]["user"]
|
39
|
-
view =
|
40
|
-
self.class.view_klass
|
41
|
-
.new(current_user: current_user, params: params, context: context)
|
42
|
-
.send(view_method_name)
|
48
|
+
view = render_view(view_method_name, context: context)
|
43
49
|
view = view.merge(callback_id: callback.id) if callback.present?
|
44
50
|
view = view.merge(private_metadata: metadata) if metadata.present?
|
45
51
|
response =
|
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'active_support/core_ext/object'
|
2
2
|
|
3
|
+
require 'slack_bot/concerns/view_klass'
|
4
|
+
|
3
5
|
module SlackBot
|
4
6
|
class Interaction
|
5
|
-
SlackViewsReply =
|
7
|
+
SlackViewsReply = Struct.new(:callback_id, :view_id)
|
6
8
|
|
7
|
-
|
8
|
-
define_singleton_method(:view_klass) { klass }
|
9
|
-
end
|
9
|
+
include SlackBot::Concerns::ViewKlass
|
10
10
|
|
11
11
|
def self.open_modal(
|
12
12
|
trigger_id:,
|
13
13
|
payload:,
|
14
14
|
class_name:,
|
15
15
|
user:,
|
16
|
-
channel_id
|
16
|
+
channel_id: nil,
|
17
17
|
config: nil
|
18
18
|
)
|
19
19
|
callback =
|
@@ -91,20 +91,33 @@ module SlackBot
|
|
91
91
|
payload["actions"]
|
92
92
|
end
|
93
93
|
|
94
|
+
def render_view(view_name, context: nil)
|
95
|
+
view = self.class.view_klass.new(
|
96
|
+
args: callback&.args,
|
97
|
+
current_user: current_user,
|
98
|
+
params: params,
|
99
|
+
context: context,
|
100
|
+
config: config
|
101
|
+
)
|
102
|
+
view.send(view_name)
|
103
|
+
end
|
104
|
+
|
105
|
+
def open_modal(view_name, context: nil)
|
106
|
+
view_payload = render_view(view_name, context: context)
|
107
|
+
self.class.open_modal(
|
108
|
+
trigger_id: payload["trigger_id"],
|
109
|
+
payload: view_payload,
|
110
|
+
class_name: self.class.name,
|
111
|
+
user: current_user,
|
112
|
+
config: config
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
94
116
|
def update_modal(view_name, context: nil)
|
95
117
|
return if callback.blank?
|
96
118
|
|
97
119
|
view_id = payload["view"]["id"]
|
98
|
-
|
99
|
-
view =
|
100
|
-
self.class.view_klass.new(
|
101
|
-
args: args,
|
102
|
-
current_user: current_user,
|
103
|
-
params: params,
|
104
|
-
context: context,
|
105
|
-
config: config
|
106
|
-
)
|
107
|
-
payload = view.send(view_name)
|
120
|
+
payload = render_view(view_name, context: context)
|
108
121
|
|
109
122
|
self.class.update_modal(
|
110
123
|
view_id: view_id,
|
data/lib/slack_bot/view.rb
CHANGED
@@ -1,14 +1,9 @@
|
|
1
1
|
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
+
require 'slack_bot/concerns/pager_klass'
|
2
3
|
|
3
4
|
module SlackBot
|
4
5
|
class View
|
5
|
-
|
6
|
-
SlackBot::Pager
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.pager(klass)
|
10
|
-
define_singleton_method(:pager_klass) { klass }
|
11
|
-
end
|
6
|
+
include SlackBot::Concerns::PagerKlass
|
12
7
|
|
13
8
|
attr_reader :args, :current_user, :params, :context, :config
|
14
9
|
def initialize(current_user:, params:, args: nil, context: nil, config: nil)
|
data/lib/slack_bot.rb
CHANGED
@@ -1,5 +1,74 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SlackBot::Event do
|
4
|
+
let(:current_user) { double(:current_user) }
|
5
|
+
let(:params) { double(:params) }
|
6
|
+
let(:callback) { double(:callback) }
|
7
|
+
let(:config) { double(:config) }
|
4
8
|
|
9
|
+
subject do
|
10
|
+
described_class.new(
|
11
|
+
current_user: current_user,
|
12
|
+
params: params,
|
13
|
+
callback: callback,
|
14
|
+
config: config
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.view_klass' do
|
19
|
+
it 'raises exception' do
|
20
|
+
expect { subject.class.view_klass }.to raise_error(SlackBot::Errors::ViewClassNotImplemented)
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when view is called" do
|
24
|
+
before do
|
25
|
+
subject.class.view :view_name
|
26
|
+
end
|
27
|
+
it 'returns view_name' do
|
28
|
+
expect(subject.class.view_klass).to eq(:view_name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.interaction_klass' do
|
34
|
+
it 'raises exception' do
|
35
|
+
expect { subject.class.interaction_klass }.to raise_error(SlackBot::Errors::InteractionClassNotImplemented)
|
36
|
+
end
|
37
|
+
context "when interaction is called" do
|
38
|
+
before do
|
39
|
+
subject.class.interaction :interaction_name
|
40
|
+
end
|
41
|
+
it 'returns interaction_name' do
|
42
|
+
expect(subject.class.interaction_klass).to eq(:interaction_name)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#initialize' do
|
48
|
+
it 'sets current_user' do
|
49
|
+
expect(subject.current_user).to eq(current_user)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'sets params' do
|
53
|
+
expect(subject.params).to eq(params)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'sets callback' do
|
57
|
+
expect(subject.callback).to eq(callback)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'sets config' do
|
61
|
+
expect(subject.config).to eq(config)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#call' do
|
66
|
+
it 'returns nil' do
|
67
|
+
expect(subject.call).to eq(nil)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#publish_view' do
|
72
|
+
|
73
|
+
end
|
5
74
|
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.
|
4
|
+
version: 1.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Makarov
|
@@ -167,6 +167,9 @@ files:
|
|
167
167
|
- lib/slack_bot/callback.rb
|
168
168
|
- lib/slack_bot/callback_storage.rb
|
169
169
|
- lib/slack_bot/command.rb
|
170
|
+
- lib/slack_bot/concerns/interaction_klass.rb
|
171
|
+
- lib/slack_bot/concerns/pager_klass.rb
|
172
|
+
- lib/slack_bot/concerns/view_klass.rb
|
170
173
|
- lib/slack_bot/config.rb
|
171
174
|
- lib/slack_bot/dev_console.rb
|
172
175
|
- lib/slack_bot/error.rb
|