telegram_meetup_bot 0.1.5 → 0.1.6

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: 2cb81754b8ece1419aa6058ecabd7e8a4462d88a
4
- data.tar.gz: 24f686b8db1e4768a1361f8ac52551166d44b4fe
3
+ metadata.gz: ae8241da7158681a6a6be60b126372aa00f3b895
4
+ data.tar.gz: fda9664f80b483f967a41e33a7be66cbb9cf8a66
5
5
  SHA512:
6
- metadata.gz: e93de5d8f13463d74a26c3084af467c9973e168a4616a60e1e9ed49f921efb0e875fe216f0b279192ab9f44a0eb042f1d8497998313f8a821ddcbbd6ed9eb94c
7
- data.tar.gz: 2d06f6fe0d08cd2ea9b87f7009636ea71bac05d0e83452706507b699c9dd70a14f524df6132c02ee824a612a91e81daa1ccec0099cc6e5bb1b2a4759cf74503b
6
+ metadata.gz: 52101c58e82e849dbd27f7af9512f65fca7b55c616d8c0fe1271ffa069cc3101fa8d7bee5632c25e451a687740cb3c60f237bcaa2094b2ee1ac0b6111784629e
7
+ data.tar.gz: e3ea5104324d11e57d24137cb3881f434ab8652cdf754de2c36731d963737d843fddbd2716f99dc8b32db8d867bf7914eb52d62dfb5815b809d642289270733b
data/README.md CHANGED
@@ -4,9 +4,9 @@
4
4
 
5
5
  Telegram bot for meetups organisation. It suits really well for small communities, where people want to attend a place together, but not on regular basis.
6
6
 
7
- ![chat example](https://cloud.githubusercontent.com/assets/854386/10962172/1461e79a-83a7-11e5-94b4-78c6d53f648c.png)
7
+ ![chat example](https://cloud.githubusercontent.com/assets/854386/11559969/0081da72-99ce-11e5-8950-7375a12e2faf.png)
8
8
 
9
- Available commands: ```/date, /list, /cancel, /cal, /help```
9
+ Available commands: ```/date, /list, /cancel, /cal, /help, /user```
10
10
 
11
11
  ## Installation
12
12
 
@@ -7,6 +7,8 @@ nobody: "No one planned to come on %date%."
7
7
  not_subscribed: "You wasn't subscribed on %date%."
8
8
  no_username: "%first_name%, please set a username in the Telegram settings."
9
9
  cal: "Reserved days (number of people) in %date%:"
10
+ user: "@%username% has reserved these days in %date%:"
11
+ user_without_reservation: "@%username% hasn't reserved any days in %date%."
10
12
  help: |
11
13
  Hi, %first_name%. Use this commands list:
12
14
  /date 14:00 - add yourself to the today list (time is optional);
@@ -41,7 +41,7 @@ module TelegramMeetupBot
41
41
  def self.submited_days_of_month(month)
42
42
  storage = Initializers::ConfigLoader.storage
43
43
  dates = storage.get_all_available_dates
44
- min, max = build_date_window(month)
44
+ min, max = build_dates_window(month)
45
45
 
46
46
  dates = dates.keep_if { |date, _| date >= min && date <= max }.sort
47
47
  dates.map do |date, users_yml|
@@ -49,6 +49,20 @@ module TelegramMeetupBot
49
49
  end.join(', ')
50
50
  end
51
51
 
52
+ def self.submited_days_by_user(username)
53
+ storage = Initializers::ConfigLoader.storage
54
+ dates = storage.get_all_available_dates
55
+ min, max = build_dates_window(Date.today.month)
56
+ username.downcase!
57
+
58
+ dates = dates.keep_if do |date, users_yml|
59
+ date >= min && date <= max &&
60
+ YAML.load(users_yml).any? { |u| u[:username].downcase == username }
61
+ end
62
+
63
+ dates.sort.map { |date, _| date.match(/\d\d$/) }.join(', ')
64
+ end
65
+
52
66
  private
53
67
 
54
68
  def process_user(&block)
@@ -74,7 +88,7 @@ module TelegramMeetupBot
74
88
  end
75
89
  end
76
90
 
77
- def self.build_date_window(month)
91
+ def self.build_dates_window(month)
78
92
  today = Date.today
79
93
  year = month < today.month ? today.next_year.year : today.year
80
94
  max = Date.new(year, month, -1).to_s
@@ -1,6 +1,6 @@
1
1
  module TelegramMeetupBot
2
2
  class CommandsHandler
3
- COMMANDS = %w(date list cancel help cal)
3
+ COMMANDS = %w(date list cancel help cal user)
4
4
  BLACK_LIST = %w(me)
5
5
  attr_reader :command, :params, :helper, :botan
6
6
 
@@ -65,6 +65,10 @@ module TelegramMeetupBot
65
65
  helper.handle_cal month
66
66
  end
67
67
 
68
+ def user(params)
69
+ helper.handle_user params.first
70
+ end
71
+
68
72
  def help
69
73
  helper.handle_default_command
70
74
  end
@@ -51,6 +51,15 @@ module TelegramMeetupBot
51
51
  messenger.send_text list_response(list: dates, month: month)
52
52
  end
53
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
+
54
63
  private
55
64
 
56
65
  def handle(date, &block)
@@ -70,8 +79,10 @@ module TelegramMeetupBot
70
79
  end
71
80
 
72
81
  def list_response(args)
82
+ empty_key = args.fetch(:empty_key) { 'nobody' }
83
+
73
84
  if args.fetch(:list).empty?
74
- build_response(args.merge(key: 'nobody'))
85
+ build_response(args.merge(key: empty_key))
75
86
  else
76
87
  build_response(args) { |response| "#{response}\n#{args.fetch(:list)}" }
77
88
  end
@@ -83,6 +94,7 @@ module TelegramMeetupBot
83
94
  response.gsub!('%first_name%', author.first_name)
84
95
  response.gsub!('%date%', args[:date].strftime('%d %h %Y')) if args[:date]
85
96
  response.gsub!('%date%', args[:month].strftime('%h %Y')) if args[:month]
97
+ response.gsub!('%username%', args[:username]) if args[:username]
86
98
 
87
99
  block_given? ? yield(response) : response
88
100
  end
@@ -3,7 +3,7 @@ module TelegramMeetupBot
3
3
  class ResponsesLoader < Base
4
4
  FILE_NAME = 'responses.yml'
5
5
  AVAILABLE_KEYS = %w(date list cancel wrong_date_format old_date nobody
6
- not_subscribed no_username help cal)
6
+ not_subscribed no_username help cal user user_without_reservation)
7
7
 
8
8
  def self.responses
9
9
  @configurations
@@ -17,7 +17,7 @@ module TelegramMeetupBot
17
17
  end
18
18
 
19
19
  def get_all_available_dates
20
- dates = redis.hgetall(key)
20
+ redis.hgetall(key)
21
21
  end
22
22
 
23
23
  def delete_date(date)
@@ -1,3 +1,3 @@
1
1
  module TelegramMeetupBot
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -121,4 +121,61 @@ RSpec.describe TelegramMeetupBot::Calendar do
121
121
  end
122
122
  end
123
123
  end
124
+
125
+ describe 'Calendar#submited_days_by_user' do
126
+ let(:storage) { double('storage') }
127
+ let(:month) { 7 }
128
+ let(:username) { 'timur' }
129
+ subject { described_class }
130
+ before { allow(storage).to receive(:get_all_available_dates).and_return(dates) }
131
+
132
+ context "user has reserved days in current month" do
133
+ let(:dates) {
134
+ {"2015-07-30"=>"---\n- :id: 65208698\n :username: #{username}\n :first_name: Timur\n",
135
+ "2015-07-15"=>"---\n- :id: 65208698\n :username: #{username}\n :first_name: Timur\n",
136
+ "2015-07-16"=>"---\n- :id: 65208698\n :username: #{username}\n :first_name: Timur\n :time: '10:10'\n",
137
+ "2015-06-14"=>"---\n- :id: 65208698\n :username: #{username}\n :first_name: Timur\n",
138
+ "2015-08-17"=>"---\n- :id: 65208698\n :username: #{username}\n :first_name: Timur\n"}
139
+ }
140
+
141
+ context "today is first day of month" do
142
+ it "works" do
143
+ Timecop.freeze(Date.new(2015, month, 1)) do
144
+ expect(subject.submited_days_by_user(username)).to eq("15, 16, 30")
145
+ end
146
+ end
147
+ end
148
+
149
+ context "today is end of month" do
150
+ it "works" do
151
+ Timecop.freeze(Date.new(2015, month, 20)) do
152
+ expect(subject.submited_days_by_user(username)).to eq("30")
153
+ end
154
+ end
155
+ end
156
+
157
+ context "case insensitive" do
158
+ it "works" do
159
+ Timecop.freeze(Date.new(2015, month, 1)) do
160
+ expect(subject.submited_days_by_user(username.upcase)).to eq("15, 16, 30")
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ context "user hasn't reserved any days in current month" do
167
+ let(:dates) {
168
+ {"2015-07-30"=>"---\n- :id: 65208692\n :username: asuka\n :first_name: Asuka\n",
169
+ "2015-07-15"=>"---\n- :id: 65208692\n :username: asuka\n :first_name: Asuka\n",
170
+ "2015-07-16"=>"---\n- :id: 65208692\n :username: asuka\n :first_name: Asuka\n :time: '10:10'\n",
171
+ "2015-06-14"=>"---\n- :id: 65208698\n :username: #{username}\n :first_name: Timur\n",
172
+ "2015-08-17"=>"---\n- :id: 65208698\n :username: #{username}\n :first_name: Timur\n"}
173
+ }
174
+ it "starts from today" do
175
+ Timecop.freeze(Date.new(2015, month, 1)) do
176
+ expect(subject.submited_days_by_user(username)).to eq("")
177
+ end
178
+ end
179
+ end
180
+ end
124
181
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram_meetup_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timur Yanberdin