telegram_meetup_bot 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae8241da7158681a6a6be60b126372aa00f3b895
4
- data.tar.gz: fda9664f80b483f967a41e33a7be66cbb9cf8a66
3
+ metadata.gz: 16ad2ffb6a84e883548fba023e8d84fa21a2dc63
4
+ data.tar.gz: 57ec211d5bc0b5913172250058270d8393a4d89b
5
5
  SHA512:
6
- metadata.gz: 52101c58e82e849dbd27f7af9512f65fca7b55c616d8c0fe1271ffa069cc3101fa8d7bee5632c25e451a687740cb3c60f237bcaa2094b2ee1ac0b6111784629e
7
- data.tar.gz: e3ea5104324d11e57d24137cb3881f434ab8652cdf754de2c36731d963737d843fddbd2716f99dc8b32db8d867bf7914eb52d62dfb5815b809d642289270733b
6
+ metadata.gz: afc655b8e9ab2ea0556578b0b620cbb4c74fa79d3a737c5cd74459e9e742b69bbb11d0fd2e59d482670f4e15f0448e7fca88a377cdbb576e719409905aa7972f
7
+ data.tar.gz: b753c521d5a1d6e72b0dc2596e3ab73cbc14ed851d2ec6f7fab6ab4327e30e37ffd38d59bac536800976eff3d259b043096a6ee52093ae813b466ec200d6944d
@@ -18,4 +18,6 @@ help: |
18
18
  /cancel - remove yourself from the today list;
19
19
  /cancel 23.04.16 - remove yourself from the list on 23.04.16;
20
20
  /cal 1 - display reserved days in January (it uses current month by default)
21
+ /user - display your reserved days
22
+ /user username - display reserved days by username
21
23
  There are 3 formats for date available: 14 - day, 14.09 - day and month, 14.09.15 - day, month and year;
@@ -0,0 +1,70 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class Base
4
+ attr_reader :message
5
+
6
+ def initialize(message)
7
+ @message = message
8
+ end
9
+
10
+ def exec
11
+ fail NotImplementedError, 'This method should be overriden'
12
+ end
13
+
14
+ def command
15
+ @command ||= if COMMANDS.include?(message.command)
16
+ message.command
17
+ else
18
+ DEFAULT_COMMAND
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def author
25
+ message.author
26
+ end
27
+
28
+ def params
29
+ message.params
30
+ end
31
+
32
+ def handle_date(date, &block)
33
+ if date_has_error?(date)
34
+ @error
35
+ else
36
+ yield
37
+ end
38
+ end
39
+
40
+ def date_has_error?(date)
41
+ if date.nil?
42
+ @error = build_response(key: 'wrong_date_format')
43
+ elsif date < Date.today
44
+ @error = build_response(key: 'old_date')
45
+ end
46
+ end
47
+
48
+ def list_response(args)
49
+ empty_key = args.fetch(:empty_key) { 'nobody' }
50
+
51
+ if args.fetch(:list).empty?
52
+ build_response(args.merge(key: empty_key))
53
+ else
54
+ build_response(args) { |response| "#{response}\n#{args.fetch(:list)}" }
55
+ end
56
+ end
57
+
58
+ def build_response(args = {})
59
+ response_key = args.fetch(:key) { command }
60
+ response = Initializers::ResponsesLoader.responses[response_key].dup
61
+ response.gsub!('%first_name%', author.first_name)
62
+ response.gsub!('%date%', args[:date].strftime('%d %h %Y')) if args[:date]
63
+ response.gsub!('%date%', args[:month].strftime('%h %Y')) if args[:month]
64
+ response.gsub!('%username%', args[:username]) if args[:username]
65
+
66
+ block_given? ? yield(response) : response
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,24 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class CalCommand < Base
4
+ def exec
5
+ dates = Calendar.submited_days_of_month(month)
6
+ list_response(list: dates, month: month_with_year)
7
+ end
8
+
9
+ private
10
+
11
+ def month
12
+ @month ||= ParamsParser.new(params.first).parse_month if params.any?
13
+ @month ||= Date.today.month
14
+ @month
15
+ end
16
+
17
+ def month_with_year
18
+ today = Date.today
19
+ year = month < today.month ? today.next_year.year : today.year
20
+ Date.new(year, month)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class CancelCommand < Base
4
+ def exec
5
+ handle_date(date) do
6
+ calendar = Calendar.new(date: date, user: author)
7
+ deleted_user = calendar.delete_user_from_date
8
+ args = deleted_user ? {} : {key: 'not_subscribed', date: date}
9
+ build_response(args)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def date
16
+ @parsed_date ||= ParamsParser.new(params.first).parse_date
17
+ @parsed_date ||= Date.today if params.empty?
18
+ @parsed_date
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class DateCommand < Base
4
+ def exec
5
+ handle_date(date) do
6
+ Calendar.new(date: date, user: author, time: time).add_user_to_date
7
+ build_response(date: date)
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def date
14
+ @parsed_date ||= ParamsParser.new(params.first).parse_date
15
+ @parsed_date ||= Date.today if params.empty? || only_time_passed?
16
+ @parsed_date
17
+ end
18
+
19
+ def only_time_passed?
20
+ time && params.size == 1
21
+ end
22
+
23
+ def time
24
+ @time ||= ParamsParser.new(params[1] || params[0]).parse_time
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class Factory
4
+ class << self
5
+ def build(message)
6
+ return if BLACK_LIST.include?(message.command)
7
+
8
+ if no_username?(message)
9
+ TelegramMeetupBot::Commands::NilUsername.new(message)
10
+ else
11
+ klass(message.command).new(message)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def klass(command)
18
+ command = whitelisted_command(command).capitalize
19
+ Object.const_get "TelegramMeetupBot::Commands::#{command}Command"
20
+ end
21
+
22
+ def whitelisted_command(command)
23
+ COMMANDS.include?(command) ? command : DEFAULT_COMMAND
24
+ end
25
+
26
+ def no_username?(message)
27
+ message.author.username.nil?
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class HelpCommand < Base
4
+ def exec
5
+ build_response
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class ListCommand < Base
4
+ def exec
5
+ handle_date(date) do
6
+ users = Calendar.formated_users_for_date(date)
7
+ list_response(list: users, date: date)
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def date
14
+ @parsed_date ||= ParamsParser.new(params.first).parse_date
15
+ @parsed_date ||= Date.today if params.empty?
16
+ @parsed_date
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class NilUsername < Base
4
+ def exec
5
+ build_response(key: 'no_username')
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module TelegramMeetupBot
2
+ module Commands
3
+ class UserCommand < Base
4
+ def exec
5
+ dates = Calendar.submited_days_by_user(username)
6
+ list_response(list: dates, month: Date.today, username: username,
7
+ empty_key: 'user_without_reservation')
8
+ end
9
+
10
+ private
11
+
12
+ def username
13
+ params.first.sub('@', '') || author.username
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'telegram_meetup_bot/commands/factory'
2
+ require 'telegram_meetup_bot/commands/base'
3
+ require 'telegram_meetup_bot/commands/nil_username'
4
+ require 'telegram_meetup_bot/commands/cal_command'
5
+ require 'telegram_meetup_bot/commands/cancel_command'
6
+ require 'telegram_meetup_bot/commands/date_command'
7
+ require 'telegram_meetup_bot/commands/help_command'
8
+ require 'telegram_meetup_bot/commands/list_command'
9
+ require 'telegram_meetup_bot/commands/user_command'
10
+
11
+ COMMANDS = %w(date list cancel help cal user)
12
+ BLACK_LIST = %w(me)
13
+ DEFAULT_COMMAND = 'help'
@@ -1,76 +1,19 @@
1
1
  module TelegramMeetupBot
2
2
  class CommandsHandler
3
- COMMANDS = %w(date list cancel help cal user)
4
- BLACK_LIST = %w(me)
5
- attr_reader :command, :params, :helper, :botan
3
+ attr_reader :command, :botan, :messenger
6
4
 
7
5
  def initialize(args)
8
6
  parser = MessageParser.new(args.fetch(:message))
9
- @command = commands.include?(parser.command) ? parser.command : 'help'
10
- @params = parser.params
11
- @helper = HandlerHelper.new(
12
- author: parser.author,
13
- command: @command,
14
- messenger: args.fetch(:messenger)
15
- )
7
+ @command = TelegramMeetupBot::Commands::Factory.build(parser)
8
+ @messenger = args.fetch(:messenger)
16
9
  @botan = args[:botan]
17
10
  end
18
11
 
19
12
  def process
20
- return if BLACK_LIST.include?(command)
21
-
22
- call_command(command)
23
- botan.track(command) if botan
24
- helper.send_empty_username_notification unless helper.author_has_username?
25
- end
26
-
27
- private
28
-
29
- def commands
30
- COMMANDS + BLACK_LIST
31
- end
32
-
33
- def call_command(command)
34
- if self.class.instance_method(command).arity == 0
35
- send command
36
- else
37
- send command, params
13
+ if command
14
+ messenger.send_text(command.exec)
15
+ botan.track(command.command) if botan
38
16
  end
39
17
  end
40
-
41
- # bot commands
42
-
43
- def date(params)
44
- date = ParamsParser.new(params.first).parse_date
45
- time = ParamsParser.new(params[1] || params[0]).parse_time
46
- date ||= Date.today if params.empty? || (time && params.size == 1)
47
- helper.handle_date(date, time)
48
- end
49
-
50
- def list(params)
51
- date = ParamsParser.new(params.first).parse_date
52
- date ||= Date.today if params.empty?
53
- helper.handle_date_list date
54
- end
55
-
56
- def cancel(params)
57
- date = ParamsParser.new(params.first).parse_date
58
- date ||= Date.today if params.empty?
59
- helper.handle_date_cancel date
60
- end
61
-
62
- def cal(params)
63
- month = ParamsParser.new(params.first).parse_month if params.any?
64
- month ||= Date.today.month
65
- helper.handle_cal month
66
- end
67
-
68
- def user(params)
69
- helper.handle_user params.first
70
- end
71
-
72
- def help
73
- helper.handle_default_command
74
- end
75
18
  end
76
19
  end
@@ -1,3 +1,3 @@
1
1
  module TelegramMeetupBot
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -11,8 +11,8 @@ require 'telegram_meetup_bot/storage'
11
11
  require 'telegram_meetup_bot/calendar'
12
12
  require 'telegram_meetup_bot/commands_handler'
13
13
  require 'telegram_meetup_bot/params_parser'
14
- require 'telegram_meetup_bot/handler_helper'
15
14
  require 'telegram_meetup_bot/botan'
15
+ require 'telegram_meetup_bot/commands'
16
16
 
17
17
  module TelegramMeetupBot
18
18
  def self.run(config_path)
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe TelegramMeetupBot::Commands::CalCommand do
4
+ before { allow(TelegramMeetupBot::Initializers::ConfigLoader).to receive(:storage).and_return(storage)}
5
+ let(:storage) { double('storage') }
6
+ let(:from) { instance_double('from', id: 1, username: 'Ikari01', first_name: 'Shinji') }
7
+ let(:message) { instance_double('message', text: command, from: from) }
8
+ let(:message_parser) { TelegramMeetupBot::MessageParser.new(message) }
9
+
10
+ describe '#exec' do
11
+ subject { described_class.new(message_parser) }
12
+ let(:command) { '/cal 5' }
13
+
14
+ context 'month is in this year' do
15
+ it 'works' do
16
+ allow(TelegramMeetupBot::Calendar).to receive(:submited_days_of_month)
17
+ allow(subject).to receive(:list_response).with({month: Date.new(2015, 5, 1), list: nil})
18
+ expect(TelegramMeetupBot::Calendar).to receive(:submited_days_of_month)
19
+ Timecop.freeze(Date.new(2015, 4, 1)) do
20
+ subject.exec
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'month is in next year' do
26
+ it 'works' do
27
+ allow(TelegramMeetupBot::Calendar).to receive(:submited_days_of_month)
28
+ allow(subject).to receive(:list_response).with({month: Date.new(2016, 5, 1), list: nil})
29
+ expect(TelegramMeetupBot::Calendar).to receive(:submited_days_of_month)
30
+ Timecop.freeze(Date.new(2015, 6, 1)) do
31
+ subject.exec
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe TelegramMeetupBot::Commands::CancelCommand do
4
+ before { allow(TelegramMeetupBot::Initializers::ConfigLoader).to receive(:storage).and_return(storage)}
5
+ let(:storage) { double('storage') }
6
+ let(:from) { instance_double('from', id: 1, username: 'Ikari01', first_name: 'Shinji') }
7
+ let(:message) { instance_double('message', text: command, from: from) }
8
+ let(:message_parser) { TelegramMeetupBot::MessageParser.new(message) }
9
+
10
+ describe '#exec' do
11
+ subject { described_class.new(message_parser) }
12
+
13
+ context 'valid date' do
14
+ let(:command) { '/cancel 21.01.16' }
15
+
16
+ it 'works' do
17
+ allow_any_instance_of(TelegramMeetupBot::Calendar).to receive(:delete_user_from_date)
18
+ allow(subject).to receive(:build_response).and_return(true)
19
+ expect_any_instance_of(TelegramMeetupBot::Calendar).to receive(:delete_user_from_date)
20
+ subject.exec
21
+ end
22
+ end
23
+
24
+ context 'invalid date' do
25
+ let(:command) { '/cancel 213.01.2015' }
26
+
27
+ it 'works' do
28
+ allow_any_instance_of(TelegramMeetupBot::Calendar).to receive(:delete_user_from_date)
29
+ allow(subject).to receive(:build_response).and_return(true)
30
+ expect_any_instance_of(TelegramMeetupBot::Calendar).not_to receive(:delete_user_from_date)
31
+ subject.exec
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe TelegramMeetupBot::Commands::DateCommand do
4
+ before { allow(TelegramMeetupBot::Initializers::ConfigLoader).to receive(:storage).and_return(storage)}
5
+ let(:storage) { double('storage') }
6
+ let(:from) { instance_double('from', id: 1, username: 'Ikari01', first_name: 'Shinji') }
7
+ let(:message) { instance_double('message', text: command, from: from) }
8
+ let(:message_parser) { TelegramMeetupBot::MessageParser.new(message) }
9
+
10
+ describe '#exec' do
11
+ subject { described_class.new(message_parser) }
12
+
13
+ context 'valid date' do
14
+ let(:command) { '/date 21.01.16' }
15
+
16
+ it 'works' do
17
+ allow_any_instance_of(TelegramMeetupBot::Calendar).to receive(:add_user_to_date)
18
+ allow(subject).to receive(:build_response).and_return(true)
19
+ expect_any_instance_of(TelegramMeetupBot::Calendar).to receive(:add_user_to_date)
20
+ subject.exec
21
+ end
22
+ end
23
+
24
+ context 'invalid date' do
25
+ let(:command) { '/date 213.01.2015' }
26
+
27
+ it 'works' do
28
+ allow_any_instance_of(TelegramMeetupBot::Calendar).to receive(:add_user_to_date)
29
+ allow(subject).to receive(:build_response).and_return(true)
30
+ expect_any_instance_of(TelegramMeetupBot::Calendar).not_to receive(:add_user_to_date)
31
+ subject.exec
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe TelegramMeetupBot::Commands::Factory do
4
+ let(:from) { instance_double('from', id: 1, username: 'Ikari01', first_name: 'Shinji') }
5
+ let(:message) { instance_double('message', text: command, from: from) }
6
+ let(:message_parser) { TelegramMeetupBot::MessageParser.new(message) }
7
+
8
+ describe 'self#build' do
9
+ subject { described_class.build(message_parser) }
10
+
11
+ context 'command in blacklist' do
12
+ let(:command) { '/me' }
13
+
14
+ it 'works' do
15
+ expect(subject).to eq(nil)
16
+ end
17
+ end
18
+
19
+ context 'available command' do
20
+ let(:command) { '/date' }
21
+
22
+ it 'works' do
23
+ expect(subject.class).to eq(TelegramMeetupBot::Commands::DateCommand)
24
+ end
25
+ end
26
+
27
+ context 'not available command' do
28
+ let(:command) { '/qwerty' }
29
+
30
+ it 'uses default command' do
31
+ expect(subject.class).to eq(TelegramMeetupBot::Commands::HelpCommand)
32
+ end
33
+ end
34
+
35
+ context 'user without username' do
36
+ let(:from) { instance_double('from', id: 1, username: nil, first_name: 'Shinji') }
37
+ let(:command) { '/date' }
38
+
39
+ it 'does something' do
40
+ expect(subject.class).to eq(TelegramMeetupBot::Commands::NilUsername)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe TelegramMeetupBot::Commands::ListCommand do
4
+ before { allow(TelegramMeetupBot::Initializers::ConfigLoader).to receive(:storage).and_return(storage)}
5
+ let(:storage) { double('storage') }
6
+ let(:from) { instance_double('from', id: 1, username: 'Ikari01', first_name: 'Shinji') }
7
+ let(:message) { instance_double('message', text: command, from: from) }
8
+ let(:message_parser) { TelegramMeetupBot::MessageParser.new(message) }
9
+
10
+ describe '#exec' do
11
+ subject { described_class.new(message_parser) }
12
+
13
+ context 'valid date' do
14
+ let(:command) { '/list 21.01.16' }
15
+
16
+ it 'works' do
17
+ allow(TelegramMeetupBot::Calendar).to receive(:formated_users_for_date)
18
+ allow(subject).to receive(:build_response).and_return(true)
19
+ allow(subject).to receive(:list_response).and_return(true)
20
+ expect(TelegramMeetupBot::Calendar).to receive(:formated_users_for_date)
21
+ subject.exec
22
+ end
23
+ end
24
+
25
+ context 'invalid date' do
26
+ let(:command) { '/list 213.01.2015' }
27
+
28
+ it 'works' do
29
+ allow(TelegramMeetupBot::Calendar).to receive(:formated_users_for_date)
30
+ allow(subject).to receive(:build_response).and_return(true)
31
+ allow(subject).to receive(:list_response).and_return(true)
32
+ expect(TelegramMeetupBot::Calendar).not_to receive(:formated_users_for_date)
33
+ subject.exec
34
+ end
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram_meetup_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timur Yanberdin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-03 00:00:00.000000000 Z
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,8 +129,17 @@ files:
129
129
  - lib/telegram_meetup_bot/botan.rb
130
130
  - lib/telegram_meetup_bot/calendar.rb
131
131
  - lib/telegram_meetup_bot/client.rb
132
+ - lib/telegram_meetup_bot/commands.rb
133
+ - lib/telegram_meetup_bot/commands/base.rb
134
+ - lib/telegram_meetup_bot/commands/cal_command.rb
135
+ - lib/telegram_meetup_bot/commands/cancel_command.rb
136
+ - lib/telegram_meetup_bot/commands/date_command.rb
137
+ - lib/telegram_meetup_bot/commands/factory.rb
138
+ - lib/telegram_meetup_bot/commands/help_command.rb
139
+ - lib/telegram_meetup_bot/commands/list_command.rb
140
+ - lib/telegram_meetup_bot/commands/nil_username.rb
141
+ - lib/telegram_meetup_bot/commands/user_command.rb
132
142
  - lib/telegram_meetup_bot/commands_handler.rb
133
- - lib/telegram_meetup_bot/handler_helper.rb
134
143
  - lib/telegram_meetup_bot/initializers.rb
135
144
  - lib/telegram_meetup_bot/initializers/base.rb
136
145
  - lib/telegram_meetup_bot/initializers/config_loader.rb
@@ -142,7 +151,11 @@ files:
142
151
  - lib/telegram_meetup_bot/version.rb
143
152
  - spec/spec_helper.rb
144
153
  - spec/telegram_meetup_bot/calendar_spec.rb
145
- - spec/telegram_meetup_bot/commands_handler_spec.rb
154
+ - spec/telegram_meetup_bot/commands/cal_command_spec.rb
155
+ - spec/telegram_meetup_bot/commands/cancel_command_spec.rb
156
+ - spec/telegram_meetup_bot/commands/date_command_spec.rb
157
+ - spec/telegram_meetup_bot/commands/factory_spec.rb
158
+ - spec/telegram_meetup_bot/commands/list_command_spec.rb
146
159
  - spec/telegram_meetup_bot/message_parser_spec.rb
147
160
  - spec/telegram_meetup_bot/params_parser_spec.rb
148
161
  - telegram_meetup_bot.gemspec
@@ -173,6 +186,10 @@ summary: Telegram bot for meetups organisation
173
186
  test_files:
174
187
  - spec/spec_helper.rb
175
188
  - spec/telegram_meetup_bot/calendar_spec.rb
176
- - spec/telegram_meetup_bot/commands_handler_spec.rb
189
+ - spec/telegram_meetup_bot/commands/cal_command_spec.rb
190
+ - spec/telegram_meetup_bot/commands/cancel_command_spec.rb
191
+ - spec/telegram_meetup_bot/commands/date_command_spec.rb
192
+ - spec/telegram_meetup_bot/commands/factory_spec.rb
193
+ - spec/telegram_meetup_bot/commands/list_command_spec.rb
177
194
  - spec/telegram_meetup_bot/message_parser_spec.rb
178
195
  - spec/telegram_meetup_bot/params_parser_spec.rb
@@ -1,102 +0,0 @@
1
- module TelegramMeetupBot
2
- class HandlerHelper
3
- attr_reader :command, :author, :messenger, :error
4
-
5
- def initialize(args)
6
- @command = args.fetch(:command)
7
- @author = args.fetch(:author)
8
- @messenger = args.fetch(:messenger)
9
- end
10
-
11
- def handle_date(date, time)
12
- handle(date) do
13
- Calendar.new(date: date, user: author, time: time).add_user_to_date
14
- messenger.send_text build_response(date: date)
15
- end
16
- end
17
-
18
- def handle_date_list(date)
19
- handle(date) do
20
- users = Calendar.formated_users_for_date(date)
21
- messenger.send_text list_response(list: users, date: date)
22
- end
23
- end
24
-
25
- def handle_date_cancel(date)
26
- handle(date) do
27
- calendar = Calendar.new(date: date, user: author)
28
- deleted_user = calendar.delete_user_from_date
29
- args = deleted_user ? {} : {key: 'not_subscribed', date: date}
30
- messenger.send_text build_response(args)
31
- end
32
- end
33
-
34
- def handle_default_command
35
- messenger.send_text build_response
36
- end
37
-
38
- def send_empty_username_notification
39
- messenger.send_text build_response(key: 'no_username')
40
- end
41
-
42
- def author_has_username?
43
- author.username
44
- end
45
-
46
- def handle_cal(month)
47
- dates = Calendar.submited_days_of_month(month)
48
- today = Date.today
49
- year = month < today.month ? today.next_year.year : today.year
50
- month = Date.new(year, month)
51
- messenger.send_text list_response(list: dates, month: month)
52
- end
53
-
54
- def handle_user(username)
55
- username ||= author.username
56
- dates = Calendar.submited_days_by_user(username)
57
- month = Date.today
58
- response = list_response(list: dates, month: month, username: username,
59
- empty_key: 'user_without_reservation')
60
- messenger.send_text response
61
- end
62
-
63
- private
64
-
65
- def handle(date, &block)
66
- if date_has_error?(date)
67
- messenger.send_text error
68
- else
69
- yield
70
- end
71
- end
72
-
73
- def date_has_error?(date)
74
- if date.nil?
75
- @error = build_response(key: 'wrong_date_format')
76
- elsif date < Date.today
77
- @error = build_response(key: 'old_date')
78
- end
79
- end
80
-
81
- def list_response(args)
82
- empty_key = args.fetch(:empty_key) { 'nobody' }
83
-
84
- if args.fetch(:list).empty?
85
- build_response(args.merge(key: empty_key))
86
- else
87
- build_response(args) { |response| "#{response}\n#{args.fetch(:list)}" }
88
- end
89
- end
90
-
91
- def build_response(args = {})
92
- response_key = args.fetch(:key) { command }
93
- response = Initializers::ResponsesLoader.responses[response_key].dup
94
- response.gsub!('%first_name%', author.first_name)
95
- response.gsub!('%date%', args[:date].strftime('%d %h %Y')) if args[:date]
96
- response.gsub!('%date%', args[:month].strftime('%h %Y')) if args[:month]
97
- response.gsub!('%username%', args[:username]) if args[:username]
98
-
99
- block_given? ? yield(response) : response
100
- end
101
- end
102
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe TelegramMeetupBot::CommandsHandler do
4
- let(:messenger) { instance_double('message') }
5
- let(:from) { instance_double('from', id: 1, username: 'Ikari01', first_name: 'Shinji') }
6
- let(:message) { instance_double('message', text: command, from: from) }
7
-
8
- subject { described_class.new(message: message, messenger: messenger) }
9
-
10
- describe '#process' do
11
- context "when command isn't in whitelist" do
12
- let(:command) { '/unknow_command' }
13
-
14
- it "calls help command" do
15
- allow_any_instance_of(TelegramMeetupBot::HandlerHelper).to receive(:handle_default_command)
16
- expect(subject).to receive(:help).with(no_args)
17
- subject.process
18
- end
19
- end
20
-
21
- context "when command without args" do
22
- let(:command) { '/help' }
23
-
24
- it "works" do
25
- allow_any_instance_of(TelegramMeetupBot::HandlerHelper).to receive(:handle_default_command)
26
- expect(subject).to receive(:help).with(no_args)
27
- subject.process
28
- end
29
- end
30
-
31
- context "when command with args" do
32
- let(:command) { '/date 08.11.07' }
33
-
34
- it "works" do
35
- allow_any_instance_of(TelegramMeetupBot::HandlerHelper).to receive(:handle_date)
36
- expect(subject).to receive(:date).with(['08.11.07'])
37
- subject.process
38
- end
39
- end
40
- end
41
- end