ruboty-reminder 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/lib/ruboty/handlers/reminder.rb +61 -26
- data/lib/ruboty/reminder.rb +6 -0
- data/lib/ruboty/reminder/task.rb +1 -18
- data/lib/ruboty/reminder/version.rb +1 -1
- 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: b61dbf5e2d3f2467a36ade7b6341532e8a8c0a8b
|
4
|
+
data.tar.gz: 5a3aa8f9e41fca2487f805f7ff4e61c6d6c1e4a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ef089c0cb2f1b10f60593c4aa19bcd869cac0d151ef0b4b287d57f56f87ef64a24271f40d5ebddb22c6251c40a50b175a81e83beee6d99c0dc0a799da127876
|
7
|
+
data.tar.gz: 6b8340491b685bc134aa8ab3ab8e69ecea29b3f91d821a8d38828c9c4d80592cbbb8af8fafc24fe5db54cdd5407c6a31768e39f838337618a09f6f8cae7f0545
|
data/README.md
CHANGED
@@ -13,15 +13,18 @@ gem 'ruboty-reminder'
|
|
13
13
|
## Usage
|
14
14
|
|
15
15
|
```
|
16
|
-
@ruboty add
|
17
|
-
@ruboty delete
|
18
|
-
@ruboty list
|
16
|
+
@ruboty add reminder yyyy/MM/dd HH:mm <reminder>
|
17
|
+
@ruboty delete reminder <id>
|
18
|
+
@ruboty list reminders <id>
|
19
19
|
```
|
20
20
|
|
21
21
|
## Example
|
22
22
|
|
23
23
|
```
|
24
|
-
> @ruboty add
|
25
|
-
|
24
|
+
> @ruboty add reminder 07:00 Hi,kaihara!
|
25
|
+
Reminder 270 is created.
|
26
26
|
Hi, kaihara!
|
27
|
+
> @ruboty add reminder 2015/01/01 00:00 Happy New Year!
|
28
|
+
Reminder 300 is created.
|
29
|
+
Happy New Year!
|
27
30
|
```
|
@@ -1,11 +1,15 @@
|
|
1
1
|
module Ruboty
|
2
2
|
module Handlers
|
3
3
|
class Reminder < Base
|
4
|
-
|
4
|
+
YEAR_RANGE = (2000..2100)
|
5
|
+
MONTH_RANGE = (1..12)
|
6
|
+
DAY_RANGE = (1..31)
|
7
|
+
HOUR_RANGE = (0..23)
|
8
|
+
MIN_RANGE = (0..59)
|
5
9
|
|
6
|
-
on /add
|
7
|
-
on /delete
|
8
|
-
on /list
|
10
|
+
on /add reminder( (?<year>\d{4})\/(?<month>\d{2})\/(?<day>\d{2}))? (?<hour>\d{2}):(?<min>\d{2}) (?<reminder>.+)/, name: 'add', description: 'Add a reminder'
|
11
|
+
on /delete reminder (?<id>.+)/, name: 'delete', description: 'Delete a reminder'
|
12
|
+
on /list reminders\z/, name: 'list', description: 'Show all reminders'
|
9
13
|
|
10
14
|
def initialize(*args)
|
11
15
|
super
|
@@ -13,66 +17,97 @@ module Ruboty
|
|
13
17
|
end
|
14
18
|
|
15
19
|
def add(message)
|
16
|
-
|
17
|
-
|
20
|
+
year, month, day =
|
21
|
+
# [year, month, day] can be omitted.
|
22
|
+
if message[:year] && message[:month] && message[:day]
|
23
|
+
[message[:year].to_i, message[:month].to_i, message[:day].to_i]
|
24
|
+
else
|
25
|
+
now_time = Time.now
|
26
|
+
[now_time.year, now_time.month, now_time.day]
|
27
|
+
end
|
28
|
+
hour = message[:hour].to_i
|
29
|
+
min = message[:min].to_i
|
18
30
|
|
19
|
-
|
20
|
-
unless hour >= 0 && hour <= 23 && min >= 0 && min <= 59
|
31
|
+
unless validate_time_format(year, month, day, hour, min)
|
21
32
|
message.reply('Invalid time format.')
|
22
33
|
return
|
23
34
|
end
|
24
35
|
|
36
|
+
target_unixtime = Time.new(year, month, day, hour, min, 0).to_i
|
37
|
+
|
38
|
+
if past?(target_unixtime)
|
39
|
+
message.reply('Already past.')
|
40
|
+
return
|
41
|
+
end
|
42
|
+
|
25
43
|
task = Ruboty::Reminder::Task.new(
|
26
44
|
message.original.except(:robot).merge(
|
27
45
|
id: generate_id,
|
28
|
-
body: message[:
|
46
|
+
body: message[:reminder],
|
47
|
+
year: year,
|
48
|
+
month: month,
|
49
|
+
day: day,
|
29
50
|
hour: hour,
|
30
|
-
min: min
|
51
|
+
min: min,
|
52
|
+
unixtime: target_unixtime
|
31
53
|
)
|
32
54
|
)
|
33
55
|
task.start(robot)
|
34
|
-
message.reply("
|
56
|
+
message.reply("Reminder #{task.hash[:id]} is created.")
|
35
57
|
|
36
|
-
|
37
|
-
tasks[task.hash[:id]] = task.hash
|
58
|
+
reminders[task.hash[:id]] = task.hash
|
38
59
|
end
|
39
60
|
|
40
61
|
def delete(message)
|
41
|
-
if
|
42
|
-
message.reply("
|
62
|
+
if reminders.delete(message[:id].to_i)
|
63
|
+
message.reply("Reminder #{message[:id]} is deleted.")
|
43
64
|
else
|
44
|
-
message.reply("
|
65
|
+
message.reply("Reminder #{message[:id]} is not found.")
|
45
66
|
end
|
46
67
|
end
|
47
68
|
|
48
69
|
def list(message)
|
49
|
-
if
|
50
|
-
message.reply("The
|
70
|
+
if reminders.empty?
|
71
|
+
message.reply("The reminder doesn't exist.")
|
51
72
|
else
|
52
|
-
|
53
|
-
"#{
|
54
|
-
|
55
|
-
|
73
|
+
reminder_list = reminders.map do |id, hash|
|
74
|
+
date = "#{hash[:year]}/#{'%02d' % hash[:month]}/#{'%02d' % hash[:day]}"
|
75
|
+
time = "#{'%02d' % hash[:hour]}:#{'%02d' % hash[:min]}"
|
76
|
+
"#{id}: #{date} #{time} -> #{hash[:body]}"
|
77
|
+
end
|
78
|
+
message.reply(reminder_list.join("\n"), code: true)
|
56
79
|
end
|
57
80
|
end
|
58
81
|
|
59
82
|
def restart
|
60
|
-
|
83
|
+
reminders.each do |id, hash|
|
61
84
|
task = Ruboty::Reminder::Task.new(hash)
|
62
85
|
task.start(robot)
|
63
86
|
end
|
64
87
|
end
|
65
88
|
|
66
|
-
def
|
67
|
-
robot.brain.data[NAMESPACE] ||= {}
|
89
|
+
def reminders
|
90
|
+
robot.brain.data[Ruboty::Reminder::NAMESPACE] ||= {}
|
68
91
|
end
|
69
92
|
|
70
93
|
def generate_id
|
71
94
|
loop do
|
72
95
|
id = rand(1000)
|
73
|
-
return id unless
|
96
|
+
return id unless reminders.has_key?(id)
|
74
97
|
end
|
75
98
|
end
|
99
|
+
|
100
|
+
def validate_time_format(year, month, day, hour, min)
|
101
|
+
YEAR_RANGE.include?(year) &&\
|
102
|
+
MONTH_RANGE.include?(month) &&\
|
103
|
+
DAY_RANGE.include?(day) &&\
|
104
|
+
HOUR_RANGE.include?(hour) &&\
|
105
|
+
MIN_RANGE.include?(min)
|
106
|
+
end
|
107
|
+
|
108
|
+
def past?(unixtime)
|
109
|
+
(unixtime - Time.now.to_i) < 0
|
110
|
+
end
|
76
111
|
end
|
77
112
|
end
|
78
113
|
end
|
data/lib/ruboty/reminder.rb
CHANGED
data/lib/ruboty/reminder/task.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Ruboty
|
2
2
|
module Reminder
|
3
3
|
class Task
|
4
|
-
NAMESPACE = 'reminder'
|
5
|
-
|
6
4
|
attr_reader :hash
|
7
5
|
|
8
6
|
def initialize(hash)
|
@@ -10,22 +8,7 @@ module Ruboty
|
|
10
8
|
end
|
11
9
|
|
12
10
|
def start(robot)
|
13
|
-
|
14
|
-
target_time = Time.new(
|
15
|
-
Time.now.year,
|
16
|
-
Time.now.month,
|
17
|
-
Time.now.day,
|
18
|
-
hash[:hour],
|
19
|
-
hash[:min],
|
20
|
-
0
|
21
|
-
).to_i
|
22
|
-
|
23
|
-
wait_time =
|
24
|
-
if target_time - current_time < 0
|
25
|
-
(target_time + 24*60*60) - current_time
|
26
|
-
else
|
27
|
-
target_time - current_time
|
28
|
-
end
|
11
|
+
wait_time = hash[:unixtime] - Time.now.to_i
|
29
12
|
|
30
13
|
Thread.start do
|
31
14
|
sleep(wait_time)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruboty-reminder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaihar4
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruboty
|