reminders_txt 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7833e42aea7e1c22ce822a700b512ce739bda483
4
- data.tar.gz: 47908e11f918d4d85f429d24e58f3bd8ca3cb05e
3
+ metadata.gz: f5f2a37608ad9d46e3adcc3a01d5008cf8e77231
4
+ data.tar.gz: 75a75d8bcedea94551146b8ea767c27c1abe1237
5
5
  SHA512:
6
- metadata.gz: ced8f17dd7096cfe9b7d92a1925ad3c9257f3c5f4a707242d1d7efbf0926abd0faf471ee30288104bc953ff994c267b2587896f8397fd55caba909958b680e0e
7
- data.tar.gz: 329aba39a779adfa91c9eb0e98e9192023b9ec0452e73bfbfa5e6a2f2e3f555a7ce9368046a99b6d76c1180fa736b6ccb498104fedc6a5e1f98ace2e41f54c24
6
+ metadata.gz: 46744e545870d608e9867f8eafd29e3525e8e987773a1db0f7a3beffd3e959fc5e6560ac78f98269727704935fe6537f2b9f949d8096aa2fe0d2f2946bf69c04
7
+ data.tar.gz: 291ce8551dd33f96a87de068e9024ca6f12675db65645fecada3cc0bc10f7de95d486371a69d2a49af788779629207e1f7508d1bb3ae53584dad343cd0106a4d
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -5,6 +5,7 @@
5
5
 
6
6
  require 'dynarex'
7
7
  require 'app-routes'
8
+ require 'digest/md5'
8
9
  require 'chronic_cron'
9
10
 
10
11
 
@@ -26,25 +27,26 @@ class RemindersTxt
26
27
 
27
28
  attr_reader :expressions
28
29
 
29
- def initialize(s, now: Time.now, dxfilepath: 'reminders.xml')
30
+ def initialize(filename='reminders.txt', now: Time.now, dxfilepath: 'reminders.xml')
30
31
 
31
- @raw_input = s
32
+
33
+ s = File.read filename
34
+ @file_contents, @filename = s, filename
32
35
  @now = now
33
36
 
34
- Dir.chdir File.dirname(dxfilepath) if dxfilepath
37
+ Dir.chdir File.dirname(filename)
35
38
 
36
39
  @dxfilepath = dxfilepath
37
40
 
38
41
  super()
39
- @params = {input: s}
42
+ @params = {}
40
43
  expressions(@params)
41
- buffer = s[2..-1]
44
+ buffer = s.lines[2..-1]
42
45
 
43
46
  @reminders = buffer.inject([]) do |r, x|
44
47
  if (x.length > 1) then
45
48
  @params[:input] = x.strip
46
49
  rx = find_expression(x)
47
- rx.input = x.strip
48
50
  r << rx
49
51
  end
50
52
  r
@@ -53,7 +55,7 @@ class RemindersTxt
53
55
  update()
54
56
  end
55
57
 
56
- def create_dx()
58
+ def save_dx()
57
59
 
58
60
  dx = Dynarex.new('reminders/reminder(input, title, recurring, date, end_date)')
59
61
  @reminders.each {|x| dx.create x.to_h}
@@ -67,36 +69,50 @@ class RemindersTxt
67
69
  x.date = x.date.is_a?(Time) ? x.date : Chronic.parse(x.date)
68
70
  x
69
71
  end
70
-
72
+
71
73
  # synchronise with the XML file
72
74
  # if XML file doesn't exist, create it
73
75
 
74
76
  if File.exists? @dxfilepath then
75
-
77
+
76
78
  dx = Dynarex.new @dxfilepath
77
79
 
78
80
  @reminders.map do |reminder|
79
81
 
80
82
  r = dx.find_by_input reminder.input
81
- reminder.date = Date.parse r.date
83
+ reminder.date = r ? Date.parse(r.date) : reminder.date.to_date
82
84
 
83
85
  reminder
84
86
  end
85
-
86
- else
87
- create_dx()
87
+
88
88
  end
89
-
90
89
 
91
90
  # delete expired non-recurring reminders
92
91
  @reminders.reject! {|x| x.date.to_time < @now }
93
92
 
94
93
  @reminders.sort_by!(&:date)
95
94
 
95
+
96
+ # did the reminders change?
97
+
98
+ h1 = (Digest::MD5.new << self.to_s).to_s
99
+ h2 = (Digest::MD5.new << @file_contents).to_s
100
+
101
+ b = h1 != h2
102
+
103
+ if b then
104
+
105
+ save_dx()
106
+ File.write @filename, self.to_s
107
+
108
+ end
109
+
110
+ [:refresh, b]
111
+
96
112
  end
97
113
 
98
114
  def to_s()
99
- @raw_input[0..2].join + @reminders.map(&:input).join("\n")
115
+ @file_contents.lines[0..2].join + @reminders.map(&:input).join("\n")
100
116
  end
101
117
 
102
118
  alias update refresh
@@ -122,10 +138,15 @@ class RemindersTxt
122
138
 
123
139
  if recurring =~ /day|week/ then
124
140
 
125
- earlier_date = Chronic.parse(date)
126
- new_date = CronFormat.new(ChronicCron.new(recurring).to_expression, earlier_date).to_time
127
- input.gsub!(date, new_date.strftime("#{new_date.day.ordinal} %b %Y"))
128
- date = new_date
141
+ start_date = Chronic.parse(date)
142
+
143
+ if start_date <= @now then
144
+
145
+ new_date = CronFormat.new(ChronicCron.new(recurring).to_expression, start_date).to_time
146
+ input.gsub!(date, new_date.strftime("#{new_date.day.ordinal} %b %Y"))
147
+ date = new_date
148
+
149
+ end
129
150
  end
130
151
 
131
152
 
@@ -137,21 +158,21 @@ class RemindersTxt
137
158
  # some meeting First thursday of the month at 7:30pm
138
159
  get /(.*)\s+(\w+ \w+day of (?:the|every) month at .*)/ do |title, recurring|
139
160
 
140
- OpenStruct.new input: '', title: title, recurring: recurring
161
+ OpenStruct.new input: params[:input], title: title, recurring: recurring
141
162
  #[1, title, recurring].inspect
142
163
  end
143
164
 
144
165
  # some important day 24th Mar
145
166
  get /(.*)\s+(\d+.*)/ do |title, date|
146
167
 
147
- OpenStruct.new input: '', title: title, date: date
168
+ OpenStruct.new input: params[:input], title: title, date: date
148
169
  #[2, title, date].inspect
149
170
  end
150
171
 
151
172
  # 27-Mar@1436 some important day
152
173
  get /(\d[^\s]+)\s+(.*)/ do |date, title|
153
174
 
154
- OpenStruct.new input: '', title: title, date: date
175
+ OpenStruct.new input: params[:input], title: title, date: date
155
176
  #[3, title, date].inspect
156
177
  end
157
178
 
@@ -166,4 +187,4 @@ class RemindersTxt
166
187
 
167
188
  alias find_expression run_route
168
189
 
169
- end
190
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reminders_txt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file