slack-ruby-bot 0.11.2 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.gitignore +3 -2
  4. data/.rubocop.yml +14 -1
  5. data/.rubocop_todo.yml +14 -42
  6. data/.travis.yml +11 -13
  7. data/CHANGELOG.md +39 -6
  8. data/CONTRIBUTING.md +5 -1
  9. data/DEPLOYMENT.md +63 -9
  10. data/Dangerfile +4 -0
  11. data/Gemfile +11 -7
  12. data/LICENSE.md +1 -1
  13. data/MIGRATION.md +13 -0
  14. data/README.md +133 -65
  15. data/Rakefile +2 -0
  16. data/TUTORIAL.md +23 -0
  17. data/UPGRADING.md +50 -0
  18. data/examples/inventory/Gemfile +2 -0
  19. data/examples/inventory/inventorybot.rb +5 -1
  20. data/examples/market/Gemfile +4 -3
  21. data/examples/market/marketbot.rb +22 -15
  22. data/examples/minimal/Gemfile +3 -1
  23. data/examples/minimal/pongbot.rb +2 -0
  24. data/examples/weather/Gemfile +2 -0
  25. data/examples/weather/weatherbot.rb +2 -0
  26. data/lib/config/application.rb +4 -2
  27. data/lib/config/boot.rb +2 -0
  28. data/lib/config/environment.rb +3 -1
  29. data/lib/slack-ruby-bot.rb +3 -1
  30. data/lib/slack-ruby-bot/about.rb +3 -1
  31. data/lib/slack-ruby-bot/app.rb +18 -12
  32. data/lib/slack-ruby-bot/bot.rb +3 -1
  33. data/lib/slack-ruby-bot/client.rb +25 -23
  34. data/lib/slack-ruby-bot/commands.rb +2 -0
  35. data/lib/slack-ruby-bot/commands/about.rb +3 -1
  36. data/lib/slack-ruby-bot/commands/base.rb +13 -21
  37. data/lib/slack-ruby-bot/commands/help.rb +10 -8
  38. data/lib/slack-ruby-bot/commands/hi.rb +3 -1
  39. data/lib/slack-ruby-bot/commands/support/attrs.rb +2 -0
  40. data/lib/slack-ruby-bot/commands/support/help.rb +4 -0
  41. data/lib/slack-ruby-bot/commands/support/match.rb +4 -3
  42. data/lib/slack-ruby-bot/commands/unknown.rb +3 -1
  43. data/lib/slack-ruby-bot/config.rb +7 -7
  44. data/lib/slack-ruby-bot/hooks.rb +2 -0
  45. data/lib/slack-ruby-bot/hooks/hello.rb +22 -3
  46. data/lib/slack-ruby-bot/hooks/hook_support.rb +2 -6
  47. data/lib/slack-ruby-bot/hooks/message.rb +11 -10
  48. data/lib/slack-ruby-bot/hooks/set.rb +3 -1
  49. data/lib/slack-ruby-bot/mvc.rb +2 -0
  50. data/lib/slack-ruby-bot/mvc/controller/base.rb +4 -3
  51. data/lib/slack-ruby-bot/mvc/model/base.rb +2 -0
  52. data/lib/slack-ruby-bot/mvc/mvc.rb +2 -0
  53. data/lib/slack-ruby-bot/mvc/view/base.rb +2 -0
  54. data/lib/slack-ruby-bot/rspec.rb +3 -1
  55. data/lib/slack-ruby-bot/rspec/support/bots_for_tests.rb +2 -0
  56. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/it_behaves_like_a_slack_bot.rb +4 -1
  57. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/not_respond.rb +3 -5
  58. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_error.rb +3 -7
  59. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_message.rb +15 -14
  60. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb +26 -19
  61. data/lib/slack-ruby-bot/rspec/support/slack-ruby-bot/start_typing.rb +32 -0
  62. data/lib/slack-ruby-bot/rspec/support/slack_api_key.rb +3 -1
  63. data/lib/slack-ruby-bot/rspec/support/slack_ruby_bot_configure.rb +2 -0
  64. data/lib/slack-ruby-bot/rspec/support/spec_helpers.rb +2 -0
  65. data/lib/slack-ruby-bot/server.rb +5 -28
  66. data/lib/slack-ruby-bot/support/loggable.rb +2 -0
  67. data/lib/slack-ruby-bot/version.rb +3 -1
  68. data/lib/slack_ruby_bot.rb +2 -0
  69. data/screenshots/create-classic-app.png +0 -0
  70. data/slack-ruby-bot.gemspec +6 -3
  71. data/spec/slack-ruby-bot/app_spec.rb +64 -1
  72. data/spec/slack-ruby-bot/client_spec.rb +103 -28
  73. data/spec/slack-ruby-bot/commands/about_spec.rb +2 -0
  74. data/spec/slack-ruby-bot/commands/aliases_spec.rb +2 -0
  75. data/spec/slack-ruby-bot/commands/attachment_spec.rb +2 -0
  76. data/spec/slack-ruby-bot/commands/bot_message_spec.rb +2 -0
  77. data/spec/slack-ruby-bot/commands/bot_messages_spec.rb +26 -0
  78. data/spec/slack-ruby-bot/commands/bot_spec.rb +2 -0
  79. data/spec/slack-ruby-bot/commands/commands_command_classes_spec.rb +2 -0
  80. data/spec/slack-ruby-bot/commands/commands_permitted_spec.rb +2 -0
  81. data/spec/slack-ruby-bot/commands/commands_precedence_spec.rb +2 -0
  82. data/spec/slack-ruby-bot/commands/commands_regexp_escape_spec.rb +2 -0
  83. data/spec/slack-ruby-bot/commands/commands_regexp_spec.rb +2 -0
  84. data/spec/slack-ruby-bot/commands/commands_spaces_spec.rb +2 -0
  85. data/spec/slack-ruby-bot/commands/commands_spec.rb +2 -0
  86. data/spec/slack-ruby-bot/commands/commands_with_block_spec.rb +2 -0
  87. data/spec/slack-ruby-bot/commands/commands_with_expression_spec.rb +2 -0
  88. data/spec/slack-ruby-bot/commands/direct_messages_spec.rb +2 -0
  89. data/spec/slack-ruby-bot/commands/empty_text_spec.rb +2 -0
  90. data/spec/slack-ruby-bot/commands/help_spec.rb +15 -13
  91. data/spec/slack-ruby-bot/commands/hi_spec.rb +2 -0
  92. data/spec/slack-ruby-bot/commands/match_spec.rb +2 -0
  93. data/spec/slack-ruby-bot/commands/message_loop_spec.rb +2 -0
  94. data/spec/slack-ruby-bot/commands/nil_message_spec.rb +2 -0
  95. data/spec/slack-ruby-bot/commands/non_breaking_space_spec.rb +29 -0
  96. data/spec/slack-ruby-bot/commands/not_implemented_spec.rb +2 -0
  97. data/spec/slack-ruby-bot/commands/operators_spec.rb +2 -0
  98. data/spec/slack-ruby-bot/commands/operators_with_block_spec.rb +2 -0
  99. data/spec/slack-ruby-bot/commands/scan_spec.rb +2 -0
  100. data/spec/slack-ruby-bot/commands/send_message_spec.rb +2 -0
  101. data/spec/slack-ruby-bot/commands/support/attrs_spec.rb +2 -0
  102. data/spec/slack-ruby-bot/commands/support/help_spec.rb +2 -0
  103. data/spec/slack-ruby-bot/commands/support/match_spec.rb +2 -0
  104. data/spec/slack-ruby-bot/commands/unknown_spec.rb +2 -0
  105. data/spec/slack-ruby-bot/config_spec.rb +2 -57
  106. data/spec/slack-ruby-bot/hooks/hello_spec.rb +51 -0
  107. data/spec/slack-ruby-bot/hooks/hook_support_spec.rb +2 -5
  108. data/spec/slack-ruby-bot/hooks/message_spec.rb +8 -35
  109. data/spec/slack-ruby-bot/hooks/set_spec.rb +2 -0
  110. data/spec/slack-ruby-bot/mvc/controller/controller_to_command_spec.rb +2 -0
  111. data/spec/slack-ruby-bot/rspec/respond_with_error_spec.rb +2 -0
  112. data/spec/slack-ruby-bot/rspec/respond_with_slack_message_spec.rb +20 -0
  113. data/spec/slack-ruby-bot/rspec/respond_with_slack_messages_spec.rb +36 -0
  114. data/spec/slack-ruby-bot/rspec/start_typing_spec.rb +36 -0
  115. data/spec/slack-ruby-bot/server_spec.rb +6 -33
  116. data/spec/slack-ruby-bot/support/loggable_spec.rb +2 -0
  117. data/spec/slack-ruby-bot/version_spec.rb +2 -0
  118. data/spec/spec_helper.rb +3 -6
  119. data/{lib/slack-ruby-bot/rspec → spec}/support/fixtures/slack/migration_in_progress.yml +0 -0
  120. data/{lib/slack-ruby-bot/rspec → spec}/support/vcr.rb +2 -0
  121. metadata +28 -29
  122. data/lib/initializers/giphy.rb +0 -8
  123. data/lib/initializers/giphy_client.rb +0 -39
  124. data/lib/slack-ruby-bot/rspec/support/fixtures/slack/giphy_burrito.yml +0 -69
  125. data/lib/slack-ruby-bot/rspec/support/fixtures/slack/giphy_client_burrito.yml +0 -71
  126. data/lib/slack-ruby-bot/rspec/support/fixtures/slack/giphy_client_burrito_rated_y.yml +0 -71
  127. data/screenshots/register-bot.png +0 -0
  128. data/spec/slack-ruby-bot/commands/send_gif_spec.rb +0 -25
  129. data/spec/slack-ruby-bot/commands/send_message_with_gif_spec.rb +0 -38
  130. data/spec/slack-ruby-bot/initializers/giphy_client_spec.rb +0 -28
  131. data/spec/slack-ruby-bot/initializers/giphy_spec.rb +0 -18
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Commands
3
5
  class Help < Base
@@ -15,7 +17,7 @@ module SlackRubyBot
15
17
  general_text
16
18
  end
17
19
 
18
- client.say(channel: data.channel, text: text, gif: 'help')
20
+ client.say(channel: data.channel, text: text)
19
21
  end
20
22
 
21
23
  class << self
@@ -24,16 +26,16 @@ module SlackRubyBot
24
26
  def general_text
25
27
  bot_desc = Support::Help.instance.bot_desc_and_commands
26
28
  other_commands_descs = Support::Help.instance.other_commands_descs
27
- <<TEXT
28
- #{bot_desc.join("\n")}
29
+ <<~TEXT
30
+ #{bot_desc.join("\n")}
29
31
 
30
- *Other commands:*
31
- #{other_commands_descs.join("\n")}
32
+ *Other commands:*
33
+ #{other_commands_descs.join("\n")}
32
34
 
33
- For getting description of the command use: *help <command>*
35
+ For getting description of the command use: *help <command>*
34
36
 
35
- For more information see https://github.com/slack-ruby/slack-ruby-bot, please.
36
- TEXT
37
+ For more information see https://github.com/slack-ruby/slack-ruby-bot, please.
38
+ TEXT
37
39
  end
38
40
  end
39
41
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Commands
3
5
  class Hi < Base
@@ -7,7 +9,7 @@ module SlackRubyBot
7
9
  end
8
10
 
9
11
  def self.call(client, data, _match)
10
- client.say(channel: data.channel, text: "Hi <@#{data.user}>!", gif: 'hi')
12
+ client.say(channel: data.channel, text: "Hi <@#{data.user}>!")
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Commands
3
5
  module Support
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
  require_relative 'attrs'
3
5
 
@@ -34,12 +36,14 @@ module SlackRubyBot
34
36
  help_attrs = find_command_help_attrs(unescaped_name)
35
37
  return "There's no command *#{unescaped_name}*" unless help_attrs
36
38
  return "There's no description for command *#{unescaped_name}*" if help_attrs.command_long_desc.blank?
39
+
37
40
  "#{command_name_and_desc(help_attrs)}\n\n#{help_attrs.command_long_desc}"
38
41
  end
39
42
 
40
43
  def find_command_help_attrs(name)
41
44
  help_attrs = commands_help_attrs.find { |k| k.command_name == name }
42
45
  return help_attrs if help_attrs
46
+
43
47
  commands_help_attrs.each { |k| k.commands.each { |c| return c if c.command_name == name } }
44
48
  nil
45
49
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Commands
3
5
  module Support
@@ -9,9 +11,8 @@ module SlackRubyBot
9
11
  attr_reader :attachment, :attachment_field
10
12
 
11
13
  def initialize(match_data, attachment = nil, attachment_field = nil)
12
- unless match_data.is_a? MatchData
13
- raise ArgumentError, 'match_data should be a type of MatchData'
14
- end
14
+ raise ArgumentError, 'match_data should be a type of MatchData' unless match_data.is_a? MatchData
15
+
15
16
  @match_data = match_data
16
17
  @attachment = attachment
17
18
  @attachment_field = attachment_field
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Commands
3
5
  class Unknown < Base
4
6
  match(/^(?<bot>\S*)[\s]*(?<expression>.*)$/)
5
7
 
6
8
  def self.call(client, data, _match)
7
- 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!")
8
10
  end
9
11
  end
10
12
  end
@@ -1,18 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Config
3
5
  extend self
4
6
 
5
- ATTRS = %i[token url aliases user user_id team team_id 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
6
8
  attr_accessor(*ATTRS)
7
9
 
8
- def allow_message_loops?
9
- allow_message_loops
10
+ def allow_bot_messages?
11
+ !!allow_bot_messages
10
12
  end
11
13
 
12
- def send_gifs?
13
- return false unless defined?(Giphy)
14
- v = boolean_from_env('SLACK_RUBY_BOT_SEND_GIFS')
15
- send_gifs.nil? ? (v.nil? || v) : send_gifs
14
+ def allow_message_loops?
15
+ !!allow_message_loops
16
16
  end
17
17
 
18
18
  def reset!
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'slack-ruby-bot/hooks/set'
2
4
  require 'slack-ruby-bot/hooks/hello'
3
5
  require 'slack-ruby-bot/hooks/message'
@@ -1,15 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Hooks
3
5
  class Hello
4
- attr_accessor :logger
6
+ attr_accessor :logger, :connected_at
5
7
 
6
8
  def initialize(logger)
7
9
  self.logger = logger
8
10
  end
9
11
 
10
12
  def call(client, _data)
11
- return unless client && client.team
12
- logger.info "Successfully connected team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com."
13
+ return unless client&.team
14
+
15
+ new_connected_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
16
+ log = [
17
+ 'Successfully',
18
+ connected_at ? 'reconnected' : 'connected',
19
+ "team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com",
20
+ connected_at ? "after #{last_connection_till(new_connected_at)}s" : nil
21
+ ].compact.join(' ') + '.'
22
+
23
+ logger.info log
24
+
25
+ self.connected_at = new_connected_at
26
+ end
27
+
28
+ private
29
+
30
+ def last_connection_till(time)
31
+ (time - connected_at).round(2)
13
32
  end
14
33
  end
15
34
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Hooks
3
5
  module HookSupport
@@ -16,12 +18,6 @@ module SlackRubyBot
16
18
  end
17
19
  end
18
20
 
19
- # Instance stuff
20
- def hooks
21
- warn Kernel.caller.first + ' [DEPRECATION] `hooks` method is deprecated. Please use `server.on` instead to register a hook.'
22
- _hooks
23
- end
24
-
25
21
  def on(event_name, handler)
26
22
  _hooks.add(event_name, handler)
27
23
  end
@@ -1,9 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Hooks
3
5
  class Message
4
6
  def call(client, data)
5
- return if message_to_self_not_allowed? && message_to_self?(client, data)
6
- data.text = data.text.strip if data.text
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)
11
+
7
12
  result = child_command_classes.detect { |d| d.invoke(client, data) }
8
13
  result ||= built_in_command_classes.detect { |d| d.invoke(client, data) }
9
14
  result ||= SlackRubyBot::Commands::Unknown.tap { |d| d.invoke(client, data) }
@@ -12,12 +17,8 @@ module SlackRubyBot
12
17
 
13
18
  private
14
19
 
15
- def message_to_self_not_allowed?
16
- !SlackRubyBot::Config.allow_message_loops?
17
- end
18
-
19
- def message_to_self?(client, data)
20
- client.self && client.self.id == data.user
20
+ def prepare!(data)
21
+ data.text = data.text.strip if data.text
21
22
  end
22
23
 
23
24
  #
@@ -36,7 +37,7 @@ module SlackRubyBot
36
37
  #
37
38
  def child_command_classes
38
39
  command_classes.reject do |k|
39
- k.name && k.name.starts_with?('SlackRubyBot::Commands::')
40
+ k.name&.starts_with?('SlackRubyBot::Commands::')
40
41
  end
41
42
  end
42
43
 
@@ -47,7 +48,7 @@ module SlackRubyBot
47
48
  #
48
49
  def built_in_command_classes
49
50
  command_classes.select do |k|
50
- k.name && k.name.starts_with?('SlackRubyBot::Commands::') && k != SlackRubyBot::Commands::Unknown
51
+ k.name&.starts_with?('SlackRubyBot::Commands::') && k != SlackRubyBot::Commands::Unknown
51
52
  end
52
53
  end
53
54
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module Hooks
3
5
  class Set
@@ -36,7 +38,7 @@ module SlackRubyBot
36
38
  end
37
39
 
38
40
  def flush_handlers
39
- handlers.keys.each { |hook| register_callback(hook) }
41
+ handlers.each_key { |hook| register_callback(hook) }
40
42
  end
41
43
  end
42
44
  end
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'slack-ruby-bot/mvc/mvc'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module MVC
3
5
  module Controller
@@ -45,9 +47,7 @@ module SlackRubyBot
45
47
  def inherited(klass) # :nodoc:
46
48
  # Define the abstract ivar on subclasses so that we don't get
47
49
  # uninitialized ivar warnings
48
- unless klass.instance_variable_defined?(:@abstract)
49
- klass.instance_variable_set(:@abstract, false)
50
- end
50
+ klass.instance_variable_set(:@abstract, false) unless klass.instance_variable_defined?(:@abstract)
51
51
  super
52
52
  end
53
53
 
@@ -64,6 +64,7 @@ module SlackRubyBot
64
64
 
65
65
  methods.each do |name|
66
66
  next if name[0] == '_'
67
+
67
68
  commands = lookup_command_name(name)
68
69
 
69
70
  # Generates a command for each controller method *and* its aliases
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module MVC
3
5
  module Model
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../mvc/controller/base'
2
4
 
3
5
  require_relative '../mvc/model/base'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SlackRubyBot
2
4
  module MVC
3
5
  module View
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
2
4
 
3
5
  require 'rubygems'
@@ -7,6 +9,6 @@ require 'rack/test'
7
9
  require 'config/environment'
8
10
  require 'slack-ruby-bot'
9
11
 
10
- Dir[File.join(File.dirname(__FILE__), 'rspec/support', '**/*.rb')].each do |file|
12
+ Dir[File.join(File.dirname(__FILE__), 'rspec/support', '**/*.rb')].sort.each do |file|
11
13
  require file
12
14
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Testing
2
4
  class WeatherBot < SlackRubyBot::Bot
3
5
  help do
@@ -1,13 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  shared_examples 'a slack ruby bot' do
2
4
  context 'not configured' do
3
5
  before do
4
6
  @slack_api_token = ENV.delete('SLACK_API_TOKEN')
7
+ SlackRubyBot.configure { |config| config.token = nil }
5
8
  end
6
9
  after do
7
10
  ENV['SLACK_API_TOKEN'] = @slack_api_token
8
11
  end
9
12
  it 'requires SLACK_API_TOKEN' do
10
- expect { subject }.to raise_error RuntimeError, "Missing ENV['SLACK_API_TOKEN']."
13
+ expect { described_class.instance }.to raise_error RuntimeError, 'Missing Slack API Token.'
11
14
  end
12
15
  end
13
16
  end
@@ -1,12 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rspec/expectations'
2
4
 
3
5
  RSpec::Matchers.define :not_respond do
4
6
  match do |actual|
5
- client = if respond_to?(:client)
6
- send(:client)
7
- else
8
- SlackRubyBot::Client.new
9
- end
7
+ client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new
10
8
 
11
9
  message_command = SlackRubyBot::Hooks::Message.new
12
10
  channel, user, message, attachments = parse(actual)
@@ -1,18 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rspec/expectations'
2
4
 
3
5
  RSpec::Matchers.define :respond_with_error do |error, error_message|
4
6
  match do |actual|
5
- client = if respond_to?(:client)
6
- send(:client)
7
- else
8
- SlackRubyBot::Client.new
9
- end
7
+ client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new
10
8
 
11
9
  message_command = SlackRubyBot::Hooks::Message.new
12
10
  channel, user, message, attachments = parse(actual)
13
11
 
14
- allow(Giphy).to receive(:random) if defined?(Giphy)
15
-
16
12
  begin
17
13
  expect do
18
14
  message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user, attachments: attachments))
@@ -1,33 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rspec/expectations'
4
+
2
5
  RSpec::Matchers.define :respond_with_slack_message do |expected|
3
6
  include SlackRubyBot::SpecHelpers
7
+
4
8
  match do |actual|
5
9
  client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new
6
- def client.test_messages
7
- @test_received_messages
8
- end
9
-
10
- def client.say(options = {})
11
- super
12
- @test_received_messages = @test_received_messages.nil? ? [] : @test_received_messages
13
- @test_received_messages.push options
14
- end
15
10
 
16
11
  message_command = SlackRubyBot::Hooks::Message.new
17
12
  channel, user, message, attachments = parse(actual)
18
13
 
19
- allow(Giphy).to receive(:random) if defined?(Giphy)
14
+ allow(client).to receive(:message) do |options|
15
+ @messages ||= []
16
+ @messages.push options
17
+ end
20
18
 
21
- allow(client).to receive(:message)
22
19
  message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user, attachments: attachments))
23
- @messages = client.test_messages
24
- expect(client).to have_received(:message).with(hash_including(channel: channel, text: expected)).once
20
+
21
+ matcher = have_received(:message).once
22
+ matcher = matcher.with(hash_including(channel: channel, text: expected)) if channel && expected
23
+
24
+ expect(client).to matcher
25
+
25
26
  true
26
27
  end
27
28
 
28
29
  failure_message do |_actual|
29
30
  message = "expected to receive message with text: #{expected} once,\n received:"
30
- message += @messages.count.zero? ? 'No response messages received' : @messages.inspect
31
+ message += @messages&.any? ? @messages.inspect : 'none'
31
32
  message
32
33
  end
33
34
  end