slack-ruby-bot 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop_todo.yml +3 -9
  4. data/.travis.yml +6 -10
  5. data/CHANGELOG.md +9 -0
  6. data/DEPLOYMENT.md +3 -5
  7. data/Dangerfile +2 -0
  8. data/Gemfile +1 -4
  9. data/MIGRATION.md +13 -0
  10. data/README.md +50 -61
  11. data/UPGRADING.md +14 -0
  12. data/lib/slack-ruby-bot/bot.rb +1 -1
  13. data/lib/slack-ruby-bot/client.rb +22 -23
  14. data/lib/slack-ruby-bot/commands/about.rb +1 -1
  15. data/lib/slack-ruby-bot/commands/base.rb +0 -19
  16. data/lib/slack-ruby-bot/commands/help.rb +1 -1
  17. data/lib/slack-ruby-bot/commands/hi.rb +1 -1
  18. data/lib/slack-ruby-bot/commands/unknown.rb +1 -1
  19. data/lib/slack-ruby-bot/config.rb +1 -8
  20. data/lib/slack-ruby-bot/hooks/hook_support.rb +0 -6
  21. data/lib/slack-ruby-bot/hooks/message.rb +6 -17
  22. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb +0 -2
  23. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +0 -2
  24. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +0 -2
  25. data/lib/slack-ruby-bot/server.rb +3 -4
  26. data/lib/slack-ruby-bot/version.rb +1 -1
  27. data/spec/slack-ruby-bot/client_spec.rb +101 -28
  28. data/spec/slack-ruby-bot/config_spec.rb +0 -57
  29. data/spec/slack-ruby-bot/hooks/hook_support_spec.rb +0 -5
  30. data/spec/slack-ruby-bot/hooks/message_spec.rb +6 -53
  31. data/spec/slack-ruby-bot/server_spec.rb +4 -16
  32. data/spec/spec_helper.rb +0 -6
  33. metadata +9 -23
  34. data/lib/initializers/giphy.rb +0 -10
  35. data/lib/initializers/giphy_client.rb +0 -41
  36. data/spec/slack-ruby-bot/commands/send_gif_spec.rb +0 -27
  37. data/spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb +0 -40
  38. data/spec/slack-ruby-bot/initializers/giphy_client_spec.rb +0 -30
  39. data/spec/slack-ruby-bot/initializers/giphy_spec.rb +0 -20
  40. data/spec/support/fixtures/slack/giphy_burrito.yml +0 -69
  41. data/spec/support/fixtures/slack/giphy_client_burrito.yml +0 -71
  42. data/spec/support/fixtures/slack/giphy_client_burrito_rated_pg.yml +0 -80
@@ -17,7 +17,7 @@ module SlackRubyBot
17
17
  general_text
18
18
  end
19
19
 
20
- client.say(channel: data.channel, text: text, gif: 'help')
20
+ client.say(channel: data.channel, text: text)
21
21
  end
22
22
 
23
23
  class << self
@@ -9,7 +9,7 @@ module SlackRubyBot
9
9
  end
10
10
 
11
11
  def self.call(client, data, _match)
12
- client.say(channel: data.channel, text: "Hi <@#{data.user}>!", gif: 'hi')
12
+ client.say(channel: data.channel, text: "Hi <@#{data.user}>!")
13
13
  end
14
14
  end
15
15
  end
@@ -6,7 +6,7 @@ module SlackRubyBot
6
6
  match(/^(?<bot>\S*)[\s]*(?<expression>.*)$/)
7
7
 
8
8
  def self.call(client, data, _match)
9
- client.say(channel: data.channel, text: "Sorry <@#{data.user}>, I don't understand that command!", gif: 'understand')
9
+ client.say(channel: data.channel, text: "Sorry <@#{data.user}>, I don't understand that command!")
10
10
  end
11
11
  end
12
12
  end
@@ -4,7 +4,7 @@ module SlackRubyBot
4
4
  module Config
5
5
  extend self
6
6
 
7
- ATTRS = %i[token url aliases user user_id team team_id allow_bot_messages allow_message_loops send_gifs logger].freeze
7
+ ATTRS = %i[token url aliases user user_id team team_id allow_bot_messages allow_message_loops logger].freeze
8
8
  attr_accessor(*ATTRS)
9
9
 
10
10
  def allow_bot_messages?
@@ -15,13 +15,6 @@ module SlackRubyBot
15
15
  !!allow_message_loops
16
16
  end
17
17
 
18
- def send_gifs?
19
- return false unless defined?(Giphy)
20
-
21
- v = boolean_from_env('SLACK_RUBY_BOT_SEND_GIFS')
22
- send_gifs.nil? ? (v.nil? || v) : send_gifs
23
- end
24
-
25
18
  def reset!
26
19
  ATTRS.each { |attr| send("#{attr}=", nil) }
27
20
  end
@@ -18,12 +18,6 @@ module SlackRubyBot
18
18
  end
19
19
  end
20
20
 
21
- # Instance stuff
22
- def hooks
23
- warn Kernel.caller.first + ' [DEPRECATION] `hooks` method is deprecated. Please use `server.on` instead to register a hook.'
24
- _hooks
25
- end
26
-
27
21
  def on(event_name, handler)
28
22
  _hooks.add(event_name, handler)
29
23
  end
@@ -4,10 +4,11 @@ module SlackRubyBot
4
4
  module Hooks
5
5
  class Message
6
6
  def call(client, data)
7
- return if message_to_self_not_allowed? && message_to_self?(client, data)
8
- return if bot_message_not_allowed? && bot_message?(client, data)
7
+ return if !client.allow_message_loops? && client.message_to_self?(data)
8
+ return if !client.allow_bot_messages? && client.bot_message?(data)
9
+
10
+ prepare!(data)
9
11
 
10
- data.text = data.text.strip if data.text
11
12
  result = child_command_classes.detect { |d| d.invoke(client, data) }
12
13
  result ||= built_in_command_classes.detect { |d| d.invoke(client, data) }
13
14
  result ||= SlackRubyBot::Commands::Unknown.tap { |d| d.invoke(client, data) }
@@ -16,20 +17,8 @@ module SlackRubyBot
16
17
 
17
18
  private
18
19
 
19
- def message_to_self_not_allowed?
20
- !SlackRubyBot::Config.allow_message_loops?
21
- end
22
-
23
- def message_to_self?(client, data)
24
- client.self && client.self.id == data.user
25
- end
26
-
27
- def bot_message_not_allowed?
28
- !SlackRubyBot::Config.allow_bot_messages?
29
- end
30
-
31
- def bot_message?(_client, data)
32
- data.subtype == 'bot_message'
20
+ def prepare!(data)
21
+ data.text = data.text.strip if data.text
33
22
  end
34
23
 
35
24
  #
@@ -9,8 +9,6 @@ RSpec::Matchers.define :respond_with_error do |error, error_message|
9
9
  message_command = SlackRubyBot::Hooks::Message.new
10
10
  channel, user, message, attachments = parse(actual)
11
11
 
12
- allow(Giphy).to receive(:random) if defined?(Giphy)
13
-
14
12
  begin
15
13
  expect do
16
14
  message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user, attachments: attachments))
@@ -11,8 +11,6 @@ RSpec::Matchers.define :respond_with_slack_message do |expected|
11
11
  message_command = SlackRubyBot::Hooks::Message.new
12
12
  channel, user, message, attachments = parse(actual)
13
13
 
14
- allow(Giphy).to receive(:random) if defined?(Giphy)
15
-
16
14
  allow(client).to receive(:message) do |options|
17
15
  @messages ||= []
18
16
  @messages.push options
@@ -13,8 +13,6 @@ RSpec::Matchers.define :respond_with_slack_messages do |expected|
13
13
  message_command = SlackRubyBot::Hooks::Message.new
14
14
  channel, user, message, attachments = parse(actual)
15
15
 
16
- allow(Giphy).to receive(:random) if defined?(Giphy)
17
-
18
16
  @messages ||= []
19
17
  allow(client).to receive(:message) do |options|
20
18
  @messages.push options
@@ -4,7 +4,7 @@ module SlackRubyBot
4
4
  class Server
5
5
  include Loggable
6
6
 
7
- attr_accessor :token, :aliases, :send_gifs
7
+ attr_accessor :token, :aliases
8
8
 
9
9
  include SlackRubyBot::Hooks::HookSupport
10
10
 
@@ -13,7 +13,6 @@ module SlackRubyBot
13
13
  def initialize(options = {})
14
14
  @token = options[:token]
15
15
  @aliases = options[:aliases]
16
- @send_gifs = options[:send_gifs]
17
16
 
18
17
  # Hook Handling
19
18
  flush_hook_blocks
@@ -57,7 +56,7 @@ module SlackRubyBot
57
56
  else
58
57
  raise e
59
58
  end
60
- rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed, Faraday::Error::SSLError => e
59
+ rescue Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::SSLError => e
61
60
  logger.error e
62
61
  sleep 1 # ignore, try again
63
62
  rescue StandardError => e
@@ -79,7 +78,7 @@ module SlackRubyBot
79
78
 
80
79
  def client
81
80
  @client ||= begin
82
- client = SlackRubyBot::Client.new(aliases: aliases, send_gifs: send_gifs, token: token)
81
+ client = SlackRubyBot::Client.new(aliases: aliases, token: token)
83
82
  _hooks.client = client
84
83
 
85
84
  client
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SlackRubyBot
4
- VERSION = '0.15.0'
4
+ VERSION = '0.16.0'
5
5
  end
@@ -1,41 +1,114 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe SlackRubyBot::Client do
4
- describe '#send_gifs?' do
5
- context 'without giphy is false', unless: WithGiphy.env? do
6
- it 'by default' do
7
- expect(subject.send_gifs?).to be false
4
+ describe '#allow_message_loops?' do
5
+ context 'with global allow_message_loops set to true' do
6
+ before do
7
+ SlackRubyBot::Config.allow_message_loops = true
8
8
  end
9
-
10
- it 'when set to true' do
11
- subject.send_gifs = true
12
- expect(subject.send_gifs?).to be false
9
+ it do
10
+ expect(subject.allow_message_loops?).to be true
13
11
  end
14
-
15
- it 'when set to true via config' do
16
- SlackRubyBot::Config.send_gifs = true
17
- expect(SlackRubyBot::Config.send_gifs?).to be false
12
+ context 'overridden locally' do
13
+ subject do
14
+ SlackRubyBot::Client.new(allow_message_loops: false)
15
+ end
16
+ it do
17
+ expect(subject.allow_message_loops?).to be false
18
+ end
18
19
  end
19
20
  end
20
-
21
- context 'with giphy', if: WithGiphy.env? do
22
- it 'default is true' do
23
- expect(subject.send_gifs?).to be true
21
+ context 'with global allow_message_loops set to false' do
22
+ before do
23
+ SlackRubyBot::Config.allow_message_loops = false
24
24
  end
25
-
26
- it 'defaults to SlackRubyBot::Config.send_gifs? if set' do
27
- SlackRubyBot::Config.send_gifs = false
28
- expect(subject.send_gifs?).to be false
25
+ it do
26
+ expect(subject.allow_message_loops?).to be false
29
27
  end
30
-
31
- it 'client setting takes precedence' do
32
- SlackRubyBot::Config.send_gifs = true
33
- subject.send_gifs = false
34
- expect(subject.send_gifs?).to be false
35
- SlackRubyBot::Config.send_gifs = false
36
- subject.send_gifs = true
37
- expect(subject.send_gifs?).to be true
28
+ context 'overridden locally' do
29
+ subject do
30
+ SlackRubyBot::Client.new(allow_message_loops: true)
31
+ end
32
+ it do
33
+ expect(subject.allow_message_loops?).to be true
34
+ end
35
+ end
36
+ end
37
+ context 'overridden locally' do
38
+ subject do
39
+ SlackRubyBot::Client.new(allow_message_loops: true)
40
+ end
41
+ it do
42
+ expect(subject.allow_message_loops?).to be true
43
+ end
44
+ end
45
+ end
46
+ describe '#allow_bot_messages?' do
47
+ context 'with global allow_bot_messages set to true' do
48
+ before do
49
+ SlackRubyBot::Config.allow_bot_messages = true
50
+ end
51
+ it do
52
+ expect(subject.allow_bot_messages?).to be true
53
+ end
54
+ context 'overridden locally' do
55
+ subject do
56
+ SlackRubyBot::Client.new(allow_bot_messages: false)
57
+ end
58
+ it do
59
+ expect(subject.allow_bot_messages?).to be false
60
+ end
61
+ end
62
+ end
63
+ context 'with global allow_bot_messages set to false' do
64
+ before do
65
+ SlackRubyBot::Config.allow_bot_messages = false
38
66
  end
67
+ it do
68
+ expect(subject.allow_bot_messages?).to be false
69
+ end
70
+ context 'overridden locally' do
71
+ subject do
72
+ SlackRubyBot::Client.new(allow_bot_messages: true)
73
+ end
74
+ it do
75
+ expect(subject.allow_bot_messages?).to be true
76
+ end
77
+ end
78
+ end
79
+ context 'overridden locally' do
80
+ subject do
81
+ SlackRubyBot::Client.new(allow_bot_messages: true)
82
+ end
83
+ it do
84
+ expect(subject.allow_bot_messages?).to be true
85
+ end
86
+ end
87
+ end
88
+ describe '#message_to_self?' do
89
+ before do
90
+ allow(subject).to receive(:self).and_return(Hashie::Mash.new({ 'id' => 'U0K8CKKT1' }))
91
+ end
92
+ context 'with message to self' do
93
+ it do
94
+ expect(subject.message_to_self?(Hashie::Mash.new(user: 'U0K8CKKT1'))).to be true
95
+ end
96
+ end
97
+ context 'with message to another user' do
98
+ it do
99
+ expect(subject.message_to_self?(Hashie::Mash.new(user: 'U0K8CKKT2'))).to be false
100
+ end
101
+ end
102
+ end
103
+ describe '#bot_message?' do
104
+ it 'bot message' do
105
+ expect(subject.bot_message?(Hashie::Mash.new(subtype: 'bot_message'))).to be true
106
+ end
107
+ it 'not bot message' do
108
+ expect(subject.bot_message?(Hashie::Mash.new(subtype: 'other'))).to be false
109
+ end
110
+ it 'no subtype' do
111
+ expect(subject.bot_message?(Hashie::Mash.new)).to be false
39
112
  end
40
113
  end
41
114
  end
@@ -1,63 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe SlackRubyBot::Config do
4
- describe '.send_gifs?' do
5
- after { ENV.delete 'SLACK_RUBY_BOT_SEND_GIFS' }
6
-
7
- context 'without giphy is false', unless: WithGiphy.env? do
8
- it 'by default' do
9
- expect(SlackRubyBot::Config.send_gifs?).to be false
10
- end
11
- it 'when set to true' do
12
- SlackRubyBot::Config.send_gifs = true
13
- expect(SlackRubyBot::Config.send_gifs?).to be false
14
- end
15
- it 'when set to true via SLACK_RUBY_BOT_SEND_GIFS' do
16
- ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'true'
17
- expect(SlackRubyBot::Config.send_gifs?).to be false
18
- end
19
- end
20
- context 'with giphy', if: WithGiphy.env? do
21
- it 'default is true' do
22
- expect(SlackRubyBot::Config.send_gifs?).to be true
23
- end
24
- context 'set to false' do
25
- it 'is false' do
26
- SlackRubyBot::Config.send_gifs = false
27
- expect(SlackRubyBot::Config.send_gifs?).to be false
28
- end
29
- end
30
- context 'set to false via SLACK_RUBY_BOT_SEND_GIFS' do
31
- it 'is false' do
32
- ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'false'
33
- expect(SlackRubyBot::Config.send_gifs?).to be false
34
- end
35
- end
36
- context 'set to true' do
37
- it 'is true' do
38
- SlackRubyBot::Config.send_gifs = true
39
- expect(SlackRubyBot::Config.send_gifs?).to be true
40
- end
41
- end
42
- context 'set to true via SLACK_RUBY_BOT_SEND_GIFS' do
43
- it 'is true' do
44
- ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'true'
45
- expect(SlackRubyBot::Config.send_gifs?).to be true
46
- end
47
- end
48
- context 'when using both methods' do
49
- it 'config setting takes precedence' do
50
- ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'true'
51
- SlackRubyBot::Config.send_gifs = false
52
- expect(SlackRubyBot::Config.send_gifs?).to be false
53
- ENV['SLACK_RUBY_BOT_SEND_GIFS'] = 'false'
54
- SlackRubyBot::Config.send_gifs = true
55
- expect(SlackRubyBot::Config.send_gifs?).to be true
56
- end
57
- end
58
- end
59
- end
60
-
61
4
  describe '#reset!' do
62
5
  it 'sets all config attributes to nil' do
63
6
  SlackRubyBot::Config::ATTRS.each do |attr|
@@ -56,11 +56,6 @@ describe SlackRubyBot::Hooks::HookSupport do
56
56
  end
57
57
  end
58
58
 
59
- describe '#hooks' do
60
- subject { super().new }
61
- it { expect(subject.hooks).to eq subject.send(:_hooks) }
62
- end
63
-
64
59
  describe '#_hooks' do
65
60
  it 'returns a SlackRubyBot::Hooks::Set instance' do
66
61
  hooks_set = subject.new.send(:_hooks)
@@ -5,10 +5,12 @@ describe SlackRubyBot::Hooks::Message do
5
5
 
6
6
  describe '#call' do
7
7
  it 'doesn\'t raise an error when message is a frozen string' do
8
- message_hook.call(
9
- SlackRubyBot::Client.new,
10
- Hashie::Mash.new(text: 'message')
11
- )
8
+ expect do
9
+ message_hook.call(
10
+ SlackRubyBot::Client.new,
11
+ Hashie::Mash.new(text: 'message'.freeze) # rubocop:disable Style/RedundantFreeze
12
+ )
13
+ end.to_not raise_error
12
14
  end
13
15
  end
14
16
 
@@ -32,55 +34,6 @@ describe SlackRubyBot::Hooks::Message do
32
34
  expect(built_in_command_classes).to_not include SlackRubyBot::Commands::Unknown
33
35
  end
34
36
  end
35
- describe '#message_to_self_not_allowed?' do
36
- context 'with allow_message_loops set to true' do
37
- before do
38
- SlackRubyBot::Config.allow_message_loops = true
39
- end
40
- it do
41
- expect(message_hook.send(:message_to_self_not_allowed?)).to be false
42
- end
43
- end
44
- context 'with allow_message_loops set to false' do
45
- before do
46
- SlackRubyBot::Config.allow_message_loops = false
47
- end
48
- it do
49
- expect(message_hook.send(:message_to_self_not_allowed?)).to be true
50
- end
51
- end
52
- end
53
- describe '#bot_message_not_allowed?' do
54
- context 'with allow_bot_messages set to true' do
55
- before do
56
- SlackRubyBot::Config.allow_bot_messages = true
57
- end
58
- it do
59
- expect(message_hook.send(:bot_message_not_allowed?)).to be false
60
- end
61
- end
62
- context 'with allow_bot_messages set to false' do
63
- before do
64
- SlackRubyBot::Config.allow_bot_messages = false
65
- end
66
- it do
67
- expect(message_hook.send(:bot_message_not_allowed?)).to be true
68
- end
69
- end
70
- end
71
- describe '#message_to_self?' do
72
- let(:client) { Hashie::Mash.new(self: { 'id' => 'U0K8CKKT1' }) }
73
- context 'with message to self' do
74
- it do
75
- expect(message_hook.send(:message_to_self?, client, Hashie::Mash.new(user: 'U0K8CKKT1'))).to be true
76
- end
77
- end
78
- context 'with message to another user' do
79
- it do
80
- expect(message_hook.send(:message_to_self?, client, Hashie::Mash.new(user: 'U0K8CKKT2'))).to be false
81
- end
82
- end
83
- end
84
37
  describe '#message' do
85
38
  let(:client) { Hashie::Mash.new(self: { 'id' => 'U0K8CKKT1' }) }
86
39
  it 'invokes a command' do