tell_me_whenever 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/tell_me_whenever.rb +127 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89c6497168202140837d5f9ffa887e322ede7e34
4
+ data.tar.gz: b5da0ff1c6c8103cfcd41a243e434afdcb64dd21
5
+ SHA512:
6
+ metadata.gz: 4ae75cdcd599511e65bd102ce922b90ef29db1b638875381c1dd559fe741e5389e29a27227193605b7c40b486a731b25f58099cace22430b634d444e673c3a50
7
+ data.tar.gz: 720c1939b1c9891a8808a5f0f40a6f6fbc38ab5414a5609ae35bcf8b9b2c7005276d3518f1c23aaac9631fa493ee9bc679f4a205d5339827e046cfbc5a586aea
@@ -0,0 +1,127 @@
1
+ require 'date'
2
+ require 'active_support'
3
+ require 'active_support/core_ext/date'
4
+ require 'active_support/core_ext/numeric/time'
5
+ require 'active_support/core_ext/integer/time'
6
+
7
+ class TellMeWhenever
8
+ def self.parse(str)
9
+ str = str.downcase
10
+ target = nil
11
+
12
+ # figure out whether an exact date was used
13
+ # match against months
14
+ months = { "january" => 1,
15
+ "february" => 2,
16
+ "march" => 3,
17
+ "april" => 4,
18
+ "may" => 5,
19
+ "june" => 6,
20
+ "july" => 7,
21
+ "august" => 8,
22
+ "september" => 9,
23
+ "october" => 10,
24
+ "november" => 11,
25
+ "december" => 12}
26
+
27
+ targetMonth = 0
28
+ months.each do |key, value|
29
+ if str.include? key then
30
+ targetMonth = value
31
+ end
32
+ end
33
+
34
+ # match against weekdays
35
+ weekdays = { "monday" => 1,
36
+ "tuesday" => 2,
37
+ "wednesday" => 3,
38
+ "thursday" => 4,
39
+ "friday" => 5,
40
+ "saturday" => 6,
41
+ "sunday" => 7}
42
+ targetWeekday = 0
43
+ weekdays.each do |key, value|
44
+ if str.include? key then
45
+ targetWeekday = value
46
+ end
47
+ end
48
+
49
+ # match against date offset
50
+ units = ["year", "month", "week", "day", "hour"]
51
+ targetUnits = []
52
+ units.each do |unit|
53
+ if str.include? unit then
54
+ targetUnits.push unit
55
+ end
56
+ end
57
+
58
+ if targetMonth != 0 then
59
+ target = DateTime.now.beginning_of_day.change({ month: targetMonth, day: 1, hour: 9 })
60
+
61
+ # find out whether a specific day was specified
62
+ str.scan(/\d+/).map(&:to_i).sort.reverse.each do |day|
63
+ # is this date within this month?
64
+ if target.end_of_month.day >= day
65
+ target = target.change({day: day})
66
+ break
67
+ end
68
+ end
69
+ elsif targetWeekday != 0 then
70
+ target = DateTime.now.beginning_of_day.change({ hour: 9 })
71
+
72
+ # falls within this week or next week?
73
+ if target.monday.next_day(targetWeekday - 1) < target then
74
+ target = target.next_week.next_day(targetWeekday - 1).change({hour: 9})
75
+ else
76
+ target = target.monday.next_day(targetWeekday - 1).change({hour: 9})
77
+ end
78
+ elsif targetUnits.length > 0 then
79
+ target = DateTime.now
80
+
81
+ if(!targetUnits.include?("hour") && !targetUnits.include?("day")) then
82
+ target = target.change({hour: 9})
83
+ end
84
+
85
+ # try to find all applicable quantifiers in the str string
86
+ targetUnits.each do |unit|
87
+ magnitude = 1
88
+ first = str.slice(0, str.index(unit)).scan(/\d+/).map(&:to_i).reverse.first
89
+ if first != nil then
90
+ magnitude = first
91
+ end
92
+
93
+ # TODO flag special cases on magnitude limitations
94
+ if unit == "year" then
95
+ if magnitude > 100 then
96
+ magnitude = 100
97
+ end
98
+ target = target + magnitude.years
99
+ elsif unit == "month" then
100
+ if magnitude > 1200 then
101
+ magnitude = 1200
102
+ end
103
+ target = target + magnitude.months
104
+ elsif unit == "week" then
105
+ if magnitude > 63600 then
106
+ magnitude = 63600
107
+ end
108
+ target = target + magnitude.weeks
109
+ elsif unit == "day" then
110
+ if magnitude > 100000 then
111
+ magnitude = 100000
112
+ end
113
+ target = target + magnitude.days
114
+ elsif unit == "hour" then
115
+ if magnitude > 100000 then
116
+ magnitude = 100000
117
+ end
118
+ target = target + magnitude.hours
119
+ end
120
+ end
121
+ elsif str.include?("tomorrow") then
122
+ target = DateTime.now.beginning_of_day.change({ hour: 9 }) + 1.days
123
+ end
124
+
125
+ return target
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tell_me_whenever
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pepijn Schoen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ description: Parses a string to a time offset
28
+ email: pepijn@eliandor.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/tell_me_whenever.rb
34
+ homepage: http://rubygems.org/gems/tell_me_whenever
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.2.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Tell Me When
58
+ test_files: []