diff --git a/CHANGELOG.md b/CHANGELOG.md index 97eda56..bb93843 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### 0.12.1 (Next) +* [#244](https://github.com/slack-ruby/slack-ruby-bot/pull/244): Change log message when the bot is reconnected - [@wasabigeek](https://github.com/wasabigeek). * [#209](https://github.com/slack-ruby/slack-ruby-bot/pull/209): Allow `respond_to_slack_message` and `respond_to_slack_messages` without arguments - [@dblock](https://github.com/dblock). * [#216](https://github.com/slack-ruby/slack-ruby-bot/pull/216): Added `start_typing` RSpec matcher - [@dblock](https://github.com/dblock). * [#214](https://github.com/slack-ruby/slack-ruby-bot/pull/214): Add passenger deployment documentation - [@cybercrediators](https://github.com/cybercrediators). diff --git a/lib/slack-ruby-bot/hooks/hello.rb b/lib/slack-ruby-bot/hooks/hello.rb index fee93fb..322af4a 100644 --- a/lib/slack-ruby-bot/hooks/hello.rb +++ b/lib/slack-ruby-bot/hooks/hello.rb @@ -1,7 +1,7 @@ module SlackRubyBot module Hooks class Hello - attr_accessor :logger + attr_accessor :logger, :connected_at def initialize(logger) self.logger = logger @@ -9,7 +9,23 @@ def initialize(logger) def call(client, _data) return unless client && client.team - logger.info "Successfully connected team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com." + new_connected_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + log = [ + 'Successfully', + connected_at ? 'reconnected' : 'connected', + "team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com", + connected_at ? "after #{last_connection_till(new_connected_at)}s" : nil + ].compact.join(' ') + '.' + + logger.info log + + self.connected_at = new_connected_at + end + + private + + def last_connection_till(time) + (time - connected_at).round(2) end end end diff --git a/spec/slack-ruby-bot/hooks/hello_spec.rb b/spec/slack-ruby-bot/hooks/hello_spec.rb new file mode 100644 index 0000000..74d5c1b --- /dev/null +++ b/spec/slack-ruby-bot/hooks/hello_spec.rb @@ -0,0 +1,49 @@ +describe SlackRubyBot::Hooks::Hello do + let(:logger) { double(:logger, info: nil) } + let(:hello_hook) { described_class.new(logger) } + + describe '#call' do + let(:team_name) { 'Example Team' } + let(:team_id) { SecureRandom.uuid } + let(:team_domain) { 'example' } + let(:team) { double(:team, name: team_name, id: team_id, domain: team_domain) } + let(:client) { instance_double(SlackRubyBot::Client, team: team) } + + def receive_hello + hello_hook.call(client, double) + end + + it 'logs the connection' do + expect(logger).to receive(:info).with("Successfully connected team #{team_name} (#{team_id}) to https://#{team_domain}.slack.com.") + receive_hello + end + + context 'with no client' do + let(:client) { nil } + it 'does nothing' do + expect(logger).not_to receive(:info) + receive_hello + end + end + + context 'when client has no team' do + let(:team) { nil } + it 'does nothing' do + expect(logger).not_to receive(:info) + receive_hello + end + end + + context 'when hook is received multiple times' do + before do + receive_hello + end + + it 'logs the reconnections' do + expect(logger).to receive(:info).with(/^Successfully reconnected .+ after \S+s\.$/).twice + receive_hello + receive_hello + end + end + end +end