ruboty-reminder 0.1.0 → 0.2.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: b61dbf5e2d3f2467a36ade7b6341532e8a8c0a8b
4
- data.tar.gz: 5a3aa8f9e41fca2487f805f7ff4e61c6d6c1e4a4
3
+ metadata.gz: da80f209dcff681fda0091e2be6025fdd58ea5b2
4
+ data.tar.gz: 0c5b965d4791226ca2ce14a99b941fe17811e7bc
5
5
  SHA512:
6
- metadata.gz: 6ef089c0cb2f1b10f60593c4aa19bcd869cac0d151ef0b4b287d57f56f87ef64a24271f40d5ebddb22c6251c40a50b175a81e83beee6d99c0dc0a799da127876
7
- data.tar.gz: 6b8340491b685bc134aa8ab3ab8e69ecea29b3f91d821a8d38828c9c4d80592cbbb8af8fafc24fe5db54cdd5407c6a31768e39f838337618a09f6f8cae7f0545
6
+ metadata.gz: a01a00f58699c9ea3b6a561d8564f845f284c059c8c2e789a548ed7c4dc85a1f47968d548f4b93d8807ee1633cb307542d81aa5ed2088715ba6f82fd12bb0ae1
7
+ data.tar.gz: 223f2cbf7cd3eaade988047e2d2ecf25dcc10afe6164f897cbf2a9e863ec833b0d2860d94344946ed7e736e61c9e36523839e8df72eb39178e14303f24b92ac0
@@ -28,8 +28,8 @@ module Ruboty
28
28
  hour = message[:hour].to_i
29
29
  min = message[:min].to_i
30
30
 
31
- unless validate_time_format(year, month, day, hour, min)
32
- message.reply('Invalid time format.')
31
+ unless valid_time?(year, month, day, hour, min)
32
+ message.reply('Invalid time.')
33
33
  return
34
34
  end
35
35
 
@@ -55,11 +55,13 @@ module Ruboty
55
55
  task.start(robot)
56
56
  message.reply("Reminder #{task.hash[:id]} is created.")
57
57
 
58
- reminders[task.hash[:id]] = task.hash
58
+ reminders[task.hash[:id]] = task
59
59
  end
60
60
 
61
61
  def delete(message)
62
- if reminders.delete(message[:id].to_i)
62
+ if task = reminders[message[:id].to_i]
63
+ task.thread.kill
64
+ reminders.delete(message[:id].to_i)
63
65
  message.reply("Reminder #{message[:id]} is deleted.")
64
66
  else
65
67
  message.reply("Reminder #{message[:id]} is not found.")
@@ -70,19 +72,21 @@ module Ruboty
70
72
  if reminders.empty?
71
73
  message.reply("The reminder doesn't exist.")
72
74
  else
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]}"
75
+ sorted_reminders = reminders.sort_by {|_id, task| task.hash[:unixtime]}
76
+
77
+ reminder_list = sorted_reminders.map do |id, task|
78
+ date = "#{task.hash[:year]}/#{'%02d' % task.hash[:month]}/#{'%02d' % task.hash[:day]}"
79
+ time = "#{'%02d' % task.hash[:hour]}:#{'%02d' % task.hash[:min]}"
80
+ "#{id}: #{date} #{time} -> #{task.hash[:body]}"
77
81
  end
78
82
  message.reply(reminder_list.join("\n"), code: true)
79
83
  end
80
84
  end
81
85
 
82
86
  def restart
83
- reminders.each do |id, hash|
84
- task = Ruboty::Reminder::Task.new(hash)
85
- task.start(robot)
87
+ reminders.each do |id, task|
88
+ new_task = Ruboty::Reminder::Task.new(task.hash)
89
+ new_task.start(robot)
86
90
  end
87
91
  end
88
92
 
@@ -97,7 +101,7 @@ module Ruboty
97
101
  end
98
102
  end
99
103
 
100
- def validate_time_format(year, month, day, hour, min)
104
+ def valid_time?(year, month, day, hour, min)
101
105
  YEAR_RANGE.include?(year) &&\
102
106
  MONTH_RANGE.include?(month) &&\
103
107
  DAY_RANGE.include?(day) &&\
@@ -1,19 +1,20 @@
1
1
  module Ruboty
2
2
  module Reminder
3
3
  class Task
4
- attr_reader :hash
4
+ attr_reader :hash, :thread
5
5
 
6
6
  def initialize(hash)
7
7
  @hash = hash
8
+ @thread = nil
8
9
  end
9
10
 
10
11
  def start(robot)
11
12
  wait_time = hash[:unixtime] - Time.now.to_i
12
13
 
13
- Thread.start do
14
+ @thread = Thread.start do
14
15
  sleep(wait_time)
15
16
  Message.new(
16
- hash.except(:id, :body, :hour, :min).merge(robot: robot)
17
+ hash.except(:id, :body, :year, :month, :day, :hour, :min, :unixtime).merge(robot: robot)
17
18
  ).reply(hash[:body])
18
19
  robot.brain.data[NAMESPACE].delete(hash[:id])
19
20
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Reminder
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -20,6 +20,4 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler'
22
22
  spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'pry'
24
- spec.add_development_dependency 'awesome_print'
25
23
  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.1.0
4
+ version: 0.2.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-12-17 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty
@@ -52,34 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: pry
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: awesome_print
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
55
  description: Ruboty handler to remind a task.
84
56
  email:
85
57
  - kaihar4@gmail.com