telegram_meetup_bot 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/config_samples/responses.yml +8 -11
- data/lib/telegram_meetup_bot/calendar.rb +10 -2
- data/lib/telegram_meetup_bot/commands_handler.rb +23 -22
- data/lib/telegram_meetup_bot/initializers/responses_loader.rb +2 -3
- data/lib/telegram_meetup_bot/params_parser.rb +6 -4
- data/lib/telegram_meetup_bot/storage.rb +4 -0
- data/lib/telegram_meetup_bot/version.rb +1 -1
- data/spec/telegram_meetup_bot/calendar_spec.rb +18 -6
- data/spec/telegram_meetup_bot/commands_handler_spec.rb +3 -3
- data/spec/telegram_meetup_bot/params_parser_spec.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98d69941024a79b6b13a609d1043f7041625284f
|
4
|
+
data.tar.gz: 14411958ac147a0d5a7d01edc9f6e500f4afd085
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b09a53761a76d37699959a4f74e32a658672d4f49f5835043b232816b5d430aba78844ca796ff61310d8c8b6d9fd0ebd4a04f48a63e6134e29597eb948654fe7
|
7
|
+
data.tar.gz: 3f541bb8be59ea76fb2afac89b1719686acca0807310b74f16b22c964a45f471e2d4f6a50927f61087fdf9f07a3811a5819d6cdf44c66a09da460f2a158f85a0
|
@@ -1,10 +1,7 @@
|
|
1
|
-
today: "We are waiting for you today, %first_name%."
|
2
|
-
today_list: "Today visitors:"
|
3
|
-
today_cancel: "We are waiting for you next time, %first_name%."
|
4
1
|
date: "We are waiting for you on %date%, %first_name%."
|
5
|
-
|
6
|
-
|
7
|
-
wrong_date_format: "Error: wrong date format. Correct formats are dd, dd.mm or dd.mm.yy
|
2
|
+
list: "%date% visitors:"
|
3
|
+
cancel: "We are waiting for you next time, %first_name%."
|
4
|
+
wrong_date_format: "Error: wrong date format. Correct formats are dd, dd.mm or dd.mm.yy"
|
8
5
|
old_date: "This date is too old."
|
9
6
|
nobody: "No one planned to come on %date%."
|
10
7
|
not_subscribed: "You wasn't subscribed on %date%."
|
@@ -12,11 +9,11 @@ no_username: "%first_name%, please set a username in the Telegram settings."
|
|
12
9
|
cal: "Reserved days in %date%:"
|
13
10
|
help: |
|
14
11
|
Hi, %first_name%. Use this commands list:
|
15
|
-
/
|
16
|
-
/today_list - display the today list;
|
17
|
-
/today_cancel - remove yourself from the today list;
|
12
|
+
/date 14:00 - add yourself to the today list (time is optional);
|
18
13
|
/date 23 14:00 - add yourself to the list on 23rd of the current month (time is optional);
|
19
|
-
/
|
20
|
-
/
|
14
|
+
/list - display the today list;
|
15
|
+
/list 23.04 - display the list for April 23 of the current year;
|
16
|
+
/cancel - remove yourself from the today list;
|
17
|
+
/cancel 23.04.16 - remove yourself from the list on 23.04.16;
|
21
18
|
/cal 1 - display reserved days in January (it uses current month by default)
|
22
19
|
There are 3 formats for date available: 14 - day, 14.09 - day and month, 14.09.15 - day, month and year;
|
@@ -22,7 +22,7 @@ module TelegramMeetupBot
|
|
22
22
|
process_user do |users, saved_user|
|
23
23
|
if saved_user
|
24
24
|
users.delete(saved_user)
|
25
|
-
|
25
|
+
update_or_clean_date(users)
|
26
26
|
true
|
27
27
|
end
|
28
28
|
end
|
@@ -44,7 +44,7 @@ module TelegramMeetupBot
|
|
44
44
|
min, max = build_date_window(month)
|
45
45
|
|
46
46
|
dates.keep_if { |date| date >= min && date <= max }.sort.
|
47
|
-
map { |date| date.match(/\d\d$/) }.join(' ')
|
47
|
+
map { |date| date.match(/\d\d$/) }.join(', ')
|
48
48
|
end
|
49
49
|
|
50
50
|
private
|
@@ -64,6 +64,14 @@ module TelegramMeetupBot
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
def update_or_clean_date(users)
|
68
|
+
if users.any?
|
69
|
+
storage.set_users_to_date(users, date)
|
70
|
+
else
|
71
|
+
storage.delete_date(date)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
67
75
|
def self.build_date_window(month)
|
68
76
|
today = Date.today
|
69
77
|
year = month < today.month ? today.next_year.year : today.year
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module TelegramMeetupBot
|
2
2
|
class CommandsHandler
|
3
|
-
COMMANDS = %w(
|
4
|
-
|
3
|
+
COMMANDS = %w(date list cancel help cal)
|
4
|
+
BLACK_LIST = %w(me)
|
5
5
|
attr_reader :command, :params, :helper
|
6
6
|
|
7
7
|
def initialize(args)
|
8
8
|
parser = MessageParser.new(args.fetch(:message))
|
9
|
-
@command =
|
9
|
+
@command = commands.include?(parser.command) ? parser.command : 'help'
|
10
10
|
@params = parser.params
|
11
11
|
@helper = HandlerHelper.new(
|
12
12
|
author: parser.author,
|
@@ -16,43 +16,44 @@ module TelegramMeetupBot
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def process
|
19
|
-
if
|
20
|
-
send command
|
21
|
-
else
|
22
|
-
send command, params
|
23
|
-
end
|
19
|
+
return if BLACK_LIST.include?(command)
|
24
20
|
|
21
|
+
call_command(command)
|
25
22
|
helper.send_empty_username_notification unless helper.author_has_username?
|
26
23
|
end
|
27
24
|
|
28
25
|
private
|
29
26
|
|
30
|
-
def
|
31
|
-
|
32
|
-
helper.handle_date(Date.today, time)
|
27
|
+
def commands
|
28
|
+
COMMANDS + BLACK_LIST
|
33
29
|
end
|
34
30
|
|
31
|
+
def call_command(command)
|
32
|
+
if self.class.instance_method(command).arity == 0
|
33
|
+
send command
|
34
|
+
else
|
35
|
+
send command, params
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# bot commands
|
40
|
+
|
35
41
|
def date(params)
|
36
42
|
date = ParamsParser.new(params.first).parse_date
|
37
|
-
time = ParamsParser.new(params[1]
|
43
|
+
time = ParamsParser.new(params[1] || params[0]).parse_time
|
44
|
+
date ||= Date.today if params.empty? || (time && params.size == 1)
|
38
45
|
helper.handle_date(date, time)
|
39
46
|
end
|
40
47
|
|
41
|
-
def
|
42
|
-
helper.handle_date_list Date.today
|
43
|
-
end
|
44
|
-
|
45
|
-
def date_list(params)
|
48
|
+
def list(params)
|
46
49
|
date = ParamsParser.new(params.first).parse_date
|
50
|
+
date ||= Date.today if params.empty?
|
47
51
|
helper.handle_date_list date
|
48
52
|
end
|
49
53
|
|
50
|
-
def
|
51
|
-
helper.handle_date_cancel Date.today
|
52
|
-
end
|
53
|
-
|
54
|
-
def date_cancel(params)
|
54
|
+
def cancel(params)
|
55
55
|
date = ParamsParser.new(params.first).parse_date
|
56
|
+
date ||= Date.today if params.empty?
|
56
57
|
helper.handle_date_cancel date
|
57
58
|
end
|
58
59
|
|
@@ -2,9 +2,8 @@ module TelegramMeetupBot
|
|
2
2
|
module Initializers
|
3
3
|
class ResponsesLoader < Base
|
4
4
|
FILE_NAME = 'responses.yml'
|
5
|
-
AVAILABLE_KEYS = %w(
|
6
|
-
|
7
|
-
no_username help cal)
|
5
|
+
AVAILABLE_KEYS = %w(date list cancel wrong_date_format old_date nobody
|
6
|
+
not_subscribed no_username help cal)
|
8
7
|
|
9
8
|
def self.responses
|
10
9
|
@configurations
|
@@ -11,11 +11,11 @@ module TelegramMeetupBot
|
|
11
11
|
|
12
12
|
def parse_date
|
13
13
|
format = case arg
|
14
|
-
when
|
14
|
+
when /\A[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{2}\Z/
|
15
15
|
"%d.%m.%y"
|
16
|
-
when
|
16
|
+
when /\A[0-9]{1,2}\.[0-9]{1,2}\Z/
|
17
17
|
"%d.%m"
|
18
|
-
when
|
18
|
+
when /\A[0-9]{1,2}\Z/
|
19
19
|
"%d"
|
20
20
|
end
|
21
21
|
|
@@ -23,7 +23,9 @@ module TelegramMeetupBot
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def parse_time
|
26
|
-
|
26
|
+
if arg =~ /\A[0-2][0-9]:[0-5][0-9]\Z/
|
27
|
+
Time.parse(arg).strftime('%R') rescue nil
|
28
|
+
end
|
27
29
|
end
|
28
30
|
|
29
31
|
def parse_month
|
@@ -2,19 +2,31 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
RSpec.describe TelegramMeetupBot::Calendar do
|
4
4
|
let(:storage) { double('storage') }
|
5
|
-
let(:user) { TelegramMeetupBot::MessageParser::User.new(1, '
|
5
|
+
let(:user) { TelegramMeetupBot::MessageParser::User.new(1, 'Unit01', 'Shinji') }
|
6
|
+
let(:user2) { TelegramMeetupBot::MessageParser::User.new(2, 'Unit02', 'Asuka') }
|
6
7
|
before { allow(TelegramMeetupBot::Initializers::ConfigLoader).to receive(:storage).and_return(storage)}
|
7
8
|
subject { described_class.new(user: user, date: Date.today, time: '10:00') }
|
8
9
|
|
9
10
|
describe '#delete_user_from_date' do
|
10
11
|
before { allow(storage).to receive(:set_users_to_date) }
|
12
|
+
before { allow(storage).to receive(:delete_date) }
|
11
13
|
|
12
|
-
context "when
|
13
|
-
before { allow(storage).to receive(:get_users_for_date).and_return([user.to_h]) }
|
14
|
+
context "when more than one users exist in storage on selected date" do
|
15
|
+
before { allow(storage).to receive(:get_users_for_date).and_return([user.to_h, user2.to_h]) }
|
14
16
|
|
15
17
|
it { expect(subject.delete_user_from_date).to eq(true) }
|
16
18
|
it 'set array without user to storage' do
|
17
|
-
expect(storage).to receive(:set_users_to_date).with([], Date.today)
|
19
|
+
expect(storage).to receive(:set_users_to_date).with([user2.to_h], Date.today)
|
20
|
+
subject.delete_user_from_date
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when one user exists in storage on selected date" do
|
25
|
+
before { allow(storage).to receive(:get_users_for_date).and_return([user.to_h]) }
|
26
|
+
|
27
|
+
it { expect(subject.delete_user_from_date).to eq(true) }
|
28
|
+
it 'delete date key from storage' do
|
29
|
+
expect(storage).to receive(:delete_date).with(Date.today)
|
18
30
|
subject.delete_user_from_date
|
19
31
|
end
|
20
32
|
end
|
@@ -71,7 +83,7 @@ RSpec.describe TelegramMeetupBot::Calendar do
|
|
71
83
|
let(:dates) { %w(2015-07-30 2015-07-15 2015-07-16 2015-06-14 2015-08-17) }
|
72
84
|
it "works" do
|
73
85
|
Timecop.freeze(Date.new(2015, month - 1, 1)) do
|
74
|
-
expect(subject.submited_days_of_month(month)).to eq("15 16 30")
|
86
|
+
expect(subject.submited_days_of_month(month)).to eq("15, 16, 30")
|
75
87
|
end
|
76
88
|
end
|
77
89
|
end
|
@@ -80,7 +92,7 @@ RSpec.describe TelegramMeetupBot::Calendar do
|
|
80
92
|
let(:dates) { %w(2015-07-30 2015-07-15 2015-07-16 2015-06-14 2015-08-17) }
|
81
93
|
it "starts from today" do
|
82
94
|
Timecop.freeze(Date.new(2015, month, 16)) do
|
83
|
-
expect(subject.submited_days_of_month(month)).to eq("16 30")
|
95
|
+
expect(subject.submited_days_of_month(month)).to eq("16, 30")
|
84
96
|
end
|
85
97
|
end
|
86
98
|
end
|
@@ -19,11 +19,11 @@ RSpec.describe TelegramMeetupBot::CommandsHandler do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
context "when command without args" do
|
22
|
-
let(:command) { '/
|
22
|
+
let(:command) { '/help' }
|
23
23
|
|
24
24
|
it "works" do
|
25
|
-
allow_any_instance_of(TelegramMeetupBot::HandlerHelper).to receive(:
|
26
|
-
expect(subject).to receive(:
|
25
|
+
allow_any_instance_of(TelegramMeetupBot::HandlerHelper).to receive(:handle_default_command)
|
26
|
+
expect(subject).to receive(:help).with(no_args)
|
27
27
|
subject.process
|
28
28
|
end
|
29
29
|
end
|
@@ -35,7 +35,7 @@ RSpec.describe TelegramMeetupBot::ParamsParser do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
context 'wrong format' do
|
38
|
-
let(:dates) { %w(abc 3.02.2007 3.02.7 2007.02.03 3-02-07 32.03.07 32) }
|
38
|
+
let(:dates) { %w(abc 3.02.2007 3.02.7 2007.02.03 3-02-07 32.03.07 32 3..07 .02.07 3.02.) }
|
39
39
|
|
40
40
|
it 'returns nil' do
|
41
41
|
dates.each do |date|
|
@@ -47,20 +47,20 @@ RSpec.describe TelegramMeetupBot::ParamsParser do
|
|
47
47
|
|
48
48
|
describe '#parse_time' do
|
49
49
|
context 'correct' do
|
50
|
-
let(:
|
50
|
+
let(:time) { %w(01:05) }
|
51
51
|
|
52
52
|
it 'works' do
|
53
|
-
|
53
|
+
time.each do |date|
|
54
54
|
expect(described_class.new(date).parse_time).to eq('01:05')
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
context 'wrong_format' do
|
60
|
-
let(:
|
60
|
+
let(:time) { %w(abc 25:05 01:61 -3) }
|
61
61
|
|
62
62
|
it 'works' do
|
63
|
-
|
63
|
+
time.each do |date|
|
64
64
|
expect(described_class.new(date).parse_time).to eq(nil)
|
65
65
|
end
|
66
66
|
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.
|
4
|
+
version: 0.1.1
|
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-
|
11
|
+
date: 2015-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|