Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.

Commit c77c178

Browse files
committed
Merge pull request #1 from ID25/refactoring
Refactoring
2 parents 376cad5 + 138c511 commit c77c178

8 files changed

+67
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
require 'simple_slack_bot/command'
32
require 'simple_slack_bot/config'
4-
require 'simple_slack_bot/client'
3+
require 'simple_slack_bot/client'

lib/simple_slack_bot/client.rb

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
21
require 'slack-ruby-client'
32

43
module SlackBot
5-
64
class Client
75
attr_accessor :slack_web_client, :slack_realtime_client
86
attr_accessor :commands
@@ -15,9 +13,7 @@ def initialize
1513
def add_command(regex)
1614
command = Command.new(self)
1715
command.regex = regex
18-
command.action = lambda { |data|
19-
yield(data)
20-
}
16+
command.action = -> (data) { yield(data) }
2117
@commands << command
2218
end
2319

@@ -27,8 +23,8 @@ def start!
2723
return
2824
end
2925

30-
self.slack_init
31-
self.message_event_init
26+
slack_init
27+
message_event_init
3228

3329
EM.run do
3430
@slack_realtime_client.start!
@@ -73,16 +69,14 @@ def message_event_init
7369
puts data if config.debug.eql?(true)
7470

7571
case data['subtype']
76-
when 'channel_join' then
77-
client.message channel: data['channel'], text: config.join_message
72+
when 'channel_join' then
73+
client.message channel: data['channel'], text: config.join_message
7874
end
7975

8076
@commands.each do |command|
81-
if command.match?(data['text'])
82-
command.execute(data)
83-
end
77+
command.execute(data) if command.match?(data['text'])
8478
end
8579
end
8680
end
8781
end
88-
end
82+
end

lib/simple_slack_bot/command.rb

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
21
module SlackBot
32
class Command
4-
attr_accessor :bot_client
5-
attr_accessor :regex
6-
attr_accessor :help_message
7-
attr_accessor :action
3+
attr_accessor :bot_client, :regex, :help_message, :action
84

95
def initialize(bot_client)
106
@bot_client = bot_client
117
end
128

139
def match?(str)
14-
return false if @regex.nil? || @regex.match(str).nil?
15-
true
10+
(@regex.nil? || @regex.match(str).nil?) ? false : true
1611
end
1712

1813
def execute(data)
1914
return if @action.nil?
2015
@action.call(data)
2116
end
2217
end
23-
end
18+
end

lib/simple_slack_bot/config.rb

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
21
module SlackBot
32
module Config
43
extend self
54

65
ATTRIBUTES = [
7-
:debug,
8-
:join_message,
9-
:token
6+
:debug,
7+
:join_message,
8+
:token
109
]
1110

1211
attr_accessor(*Config::ATTRIBUTES)
@@ -16,13 +15,13 @@ module Config
1615
self.token = 'TOKEN'
1716
end
1817

19-
class << self
20-
def configure
21-
block_given? ? yield(Config) : Config
22-
end
18+
module_function
19+
20+
def configure
21+
block_given? ? yield(Config) : Config
22+
end
2323

24-
def config
25-
Config
26-
end
24+
def config
25+
Config
2726
end
28-
end
27+
end

spec/client_spec.rb

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1+
require 'spec_helper'
12

2-
# TODO
3+
describe SlackBot::Client do
4+
describe '#initialize' do
5+
it 'have an empty commands array' do
6+
expect(SlackBot::Client.new.commands).to eq []
7+
end
8+
end
9+
10+
describe '.add_command' do
11+
bot = SlackBot::Client.new
12+
13+
it 'add command to commands array' do
14+
result = bot.add_command('Hello')
15+
expect(result).to eq bot.commands
16+
end
17+
end
18+
end

spec/command_spec.rb

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1+
require 'spec_helper'
12

2-
# TODO
3+
describe SlackBot::Command do
4+
describe '#initialize' do
5+
bot = SlackBot::Client.new
6+
7+
it 'have a bot_client' do
8+
command = SlackBot::Command.new(bot)
9+
expect(command.bot_client).to eq bot
10+
end
11+
end
12+
13+
describe '.match?' do
14+
bot = SlackBot::Client.new
15+
bot.add_command('Hello')
16+
17+
it 'iterate commands' do
18+
bot.commands.each do |command|
19+
expect(command.regex).to eq 'Hello'
20+
end
21+
end
22+
end
23+
end

spec/config_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
describe SlackBot::Config do
44
describe '#initialize' do
55
it 'sets config' do
6-
expect(SlackBot::config.join_message).to eq 'Hello!'
7-
expect(SlackBot::config.debug).to eq false
8-
expect(SlackBot::config.token).to eq 'TOKEN'
6+
expect(SlackBot.config.join_message).to eq 'Hello!'
7+
expect(SlackBot.config.debug).to eq false
8+
expect(SlackBot.config.token).to eq 'TOKEN'
99
end
1010
end
1111

@@ -24,4 +24,4 @@
2424
expect(SlackBot.config.token).to eq 'Invalid Token'
2525
end
2626
end
27-
end
27+
end

spec/spec_helper.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'bundler/setup'
22
Bundler.setup
33

4-
require 'simple-slack-bot'
4+
require 'simple_slack_bot'
55

66
RSpec.configure do |config|
7-
end
7+
end

0 commit comments

Comments
 (0)