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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c56465750cafb460b35d7e84402b23590ea03d6f
4
- data.tar.gz: c3c163fe199748c4f45ae8caf74f941d3ba7abfc
3
+ metadata.gz: b61dbf5e2d3f2467a36ade7b6341532e8a8c0a8b
4
+ data.tar.gz: 5a3aa8f9e41fca2487f805f7ff4e61c6d6c1e4a4
5
5
  SHA512:
6
- metadata.gz: 5cc958e1a1ae1774962297d3d5303302f9467605165e8831bafa9e50a29606439ad7dc1e6f1b439bb179b6b1adf5fe640192f71872415e367d7cbd5fa750d6d3
7
- data.tar.gz: 0a18aa9fb81d7186fb5647f7f07d28ef8641341539f839f8a52da0e982c77249e15cc3c81f041aaac56683ebf41434d0323f07be7f8e1f6b3c4bd9d5db81a3d9
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 task HH:MM <task>
17
- @ruboty delete task <id>
18
- @ruboty list tasks <id>
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 task 07:00 Hi,kaihara!
25
- Task 270 is created.
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
- NAMESPACE = 'reminder'
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 task (?<hh>\d{2}):(?<mm>\d{2}) (?<task>.+)/, name: 'add', description: 'Add a task'
7
- on /delete task (?<id>.+)/, name: 'delete', description: 'Delete a task'
8
- on /list tasks\z/, name: 'list', description: 'Show all reminders'
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
- hour = message[:hh].to_i
17
- min = message[:mm].to_i
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
- # Validate
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[:task],
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("Task #{task.hash[:id]} is created.")
56
+ message.reply("Reminder #{task.hash[:id]} is created.")
35
57
 
36
- # Insert to the brain
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 tasks.delete(message[:id].to_i)
42
- message.reply("Task #{message[:id]} is deleted.")
62
+ if reminders.delete(message[:id].to_i)
63
+ message.reply("Reminder #{message[:id]} is deleted.")
43
64
  else
44
- message.reply("Task #{message[:id]} is not found.")
65
+ message.reply("Reminder #{message[:id]} is not found.")
45
66
  end
46
67
  end
47
68
 
48
69
  def list(message)
49
- if tasks.empty?
50
- message.reply("The task doesn't exist.")
70
+ if reminders.empty?
71
+ message.reply("The reminder doesn't exist.")
51
72
  else
52
- task_list = tasks.map do |id, hash|
53
- "#{id}: #{'%02d' % hash[:hour]}:#{'%02d' % hash[:min]} -> #{hash[:body]}"
54
- end.join("\n")
55
- message.reply(task_list, code: true)
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
- tasks.each do |id, hash|
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 tasks
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 tasks.has_key?(id)
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
@@ -1,3 +1,9 @@
1
1
  require 'ruboty'
2
2
  require 'ruboty/reminder/task'
3
3
  require 'ruboty/handlers/reminder'
4
+
5
+ module Ruboty
6
+ module Reminder
7
+ NAMESPACE = 'reminder'
8
+ end
9
+ end
@@ -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
- current_time = Time.new.to_i
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)
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Reminder
3
- VERSION = '0.0.3'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
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.3
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-19 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty