sleep 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +66 -0
  3. data/lib/sleep.rb +69 -0
  4. metadata +58 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Bryan Goines
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ Sleep
2
+ =====
3
+
4
+ A little Ruby library to calculate 6 sleep-cycle times for good night's sleep.
5
+
6
+
7
+ ## Install
8
+
9
+
10
+ **Gemfile**
11
+
12
+ gem 'sleep'
13
+
14
+ **RubyGem**
15
+
16
+ gem install sleep
17
+
18
+
19
+ ## Usage
20
+
21
+ require 'sleep'
22
+
23
+ sleep = Sleep.new
24
+
25
+
26
+ #### Optionals
27
+
28
+ sleep.delay = true
29
+ sleep.sleep_delay = 14 # minutes
30
+
31
+
32
+ #### Sleep right now
33
+
34
+ p sleep.now
35
+
36
+
37
+ #### Next wake up time
38
+
39
+ p sleep.next(Time.now)
40
+
41
+
42
+ #### Previous wake up time
43
+
44
+ p sleep.previous(Time.now)
45
+
46
+ #### Wake up at
47
+
48
+ p sleep.wakeup_at("6:30am")
49
+
50
+ p sleep.wakeup_at("in 2 hours")
51
+
52
+ #### Sleep at
53
+
54
+ p sleep.sleep_at("10:30pm"")
55
+
56
+ p sleep.sleep_at("in 15 minutes")
57
+
58
+
59
+ ## Credits
60
+
61
+
62
+ [Chronic](https://github.com/mojombo/chronic) for amazing natural language date/time parser
63
+
64
+ ## License
65
+
66
+ Sleep is licensed under the MIT license.
@@ -0,0 +1,69 @@
1
+ require 'chronic'
2
+ require 'ostruct'
3
+
4
+ class Sleep
5
+
6
+ attr_accessor :delay, :sleep_delay, :sleep_cycle
7
+
8
+ def initialize(opts = {}, &blk)
9
+ options = OpenStruct.new(opts)
10
+ @delay = (options.delay || false)
11
+ @sleep_delay = (options.sleep_delay || 14) # average sleep delay minutes
12
+ @sleep_cycle = (options.sleep_cycle || 90) # sleep cycle minutes
13
+ instance_eval(&blk) if block_given?
14
+ end
15
+
16
+ def now
17
+ sleep_cycles(Time.now)
18
+ end
19
+
20
+ def sleep_at(time)
21
+ time = Chronic.parse(time)
22
+ sleep_cycles(time)
23
+ end
24
+
25
+ def wakeup_at(time)
26
+ time = Chronic.parse(time)
27
+ sleep_cycles(time, false)
28
+ end
29
+
30
+ def next(time)
31
+ time = time.is_a?(String) ? Chronic.parse(time) : time
32
+ next_cycle(time).strftime("%l:%M %p")
33
+ end
34
+
35
+ def previous(time)
36
+ time = time.is_a?(String) ? Chronic.parse(time) : time
37
+ prev_cycle(time).strftime("%l:%M %p")
38
+ end
39
+ alias :prev :previous
40
+
41
+ def next_cycle(time = Time.now)
42
+ cycle = @delay ? @sleep_cycle + @sleep_delay : @sleep_cycle
43
+ Chronic.parse("in #{cycle} minutes", now: time)
44
+ end
45
+
46
+ def prev_cycle(time = Time.now)
47
+ cycle = @delay ? @sleep_cycle + @sleep_delay : @sleep_cycle
48
+ Chronic.parse("#{cycle} minutes ago", now: time)
49
+ end
50
+
51
+ private
52
+
53
+ def time_distance(from_time, to_time)
54
+ minutes = (to_time - from_time).abs/60
55
+ (minutes / 60).round(1)
56
+ end
57
+
58
+ def sleep_cycles(time, forward = true)
59
+ cycles = []
60
+ cycle_time = time
61
+ 6.times do |i|
62
+ cycle_time = forward ? next_cycle(cycle_time) : prev_cycle(cycle_time)
63
+ distance = forward ? time_distance(time, cycle_time) : time_distance(cycle_time, time)
64
+ cycles << cycle_time.strftime("%l:%M %p (#{distance}h)").gsub(/^\s+/, '')
65
+ end
66
+ cycles
67
+ end
68
+
69
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sleep
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryan Goines
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chronic
16
+ requirement: &70129312462580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70129312462580
25
+ description:
26
+ email: bryann83@gmail.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - README.md
32
+ - LICENSE
33
+ - lib/sleep.rb
34
+ homepage: https://github.com/bry4n/sleep
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.15
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A little Ruby library to calculate 6 sleep-cycle times for good night's sleep
58
+ test_files: []