time_window 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 28095f55f17892cabbc31bf44bd8e114968f720f3f2466152cab4fa85b7d4789
4
+ data.tar.gz: 987aa9ea2313c274ba9e0a35f21407120c657fa96a06eece12059936cccaf7a6
5
+ SHA512:
6
+ metadata.gz: 2a4f98c5754fac92d582959a9c497e3f1d6c9481fefae91fad158b00f5f302ac0d829d417fe5ce37023f6e5a3eb29c67593c36859b99c0e64d46a4d5945d2d1d
7
+ data.tar.gz: a8ff425e5797c2cff6e5753a623010a59eb81ee1cce7ee4dcd05c11146508f414f2c6534af5695fbdc2624a4963d4aa92e97eba8f3a0e76add9efa616c5b9904
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+ notifications:
5
+ email: false
6
+ rvm:
7
+ - 2.5.3
8
+ before_script:
9
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
10
+ - chmod +x ./cc-test-reporter
11
+ - "./cc-test-reporter before-build"
12
+ after_script:
13
+ - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
14
+ env:
15
+ global:
16
+ secure: oanQQ2DXrqCbMuw7oDS+GvN2HekLKqcPFkT5cIxUW+SyRyf7WAVPY9x8BjTVrejx4L6GrT68E+vcUJNZf3UTo8RADGrbA5WxKUKhDO7i85GvbStRXE+5AOO7c1S8DPiT4KRISxzbUtE69UpwlOqZW3L6Pd9PneMPlsiB8O6KLGIP06LMxljlEcfpD21v2bovDUaHf2gd9i+CD2SojXhdgdVeR0LFOHYG9x7rDqvEApG01uKRsI7QizlC9WWMEmh6QxyWJ/DpLwooeOtBQV6h/dtBKBkh0mW4qFovCmm+HuioEPq3sx/ApoqrQoLFZf1gZ6Fxzqw9dzDIA2gIyB4nCbtuPCJSmuXgXmmGVLfGFozHF4byuHxFp20QuNaOcGZUAr08ntFeFqvZ5BLB0EGj0Q2FpB42SWRKnURar1rAfrDKiHooLJgiwpKpvN0AVXdD0gHk2A6IKx1umvKPLL/3AU3vmtbXlHRRT8zerLwAn27JZRZsjYHxN/3jwGPDIAV56z7ozuLTvLLbQxHBhlU6Tn+/SzS0wia5l8w+z6D0Wt1h0m9ytr1JnYvs5kk3inKWMHEMeXkwN2oOS5axX8jzP+LnBR32Gb4Gedc0Qrra2HCmmD5NYgjYAEuipNBUeffcoLX9Mvi+J/NZRamE6oAxD4A0eu5uH/ZaCLE3pMZb2Xk=
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at me@fnando.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in time_window.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ time_window (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ awesome_print (1.8.0)
10
+ byebug (10.0.2)
11
+ coderay (1.1.2)
12
+ docile (1.3.1)
13
+ json (2.1.0)
14
+ method_source (0.9.2)
15
+ minitest (5.11.3)
16
+ minitest-utils (0.4.4)
17
+ minitest
18
+ pry (0.12.2)
19
+ coderay (~> 1.1.0)
20
+ method_source (~> 0.9.0)
21
+ pry-byebug (3.6.0)
22
+ byebug (~> 10.0)
23
+ pry (~> 0.10)
24
+ pry-meta (0.0.10)
25
+ awesome_print
26
+ pry
27
+ pry-byebug
28
+ pry-remote
29
+ pry-remote (0.1.8)
30
+ pry (~> 0.9)
31
+ slop (~> 3.0)
32
+ rake (12.3.2)
33
+ simplecov (0.16.1)
34
+ docile (~> 1.1)
35
+ json (>= 1.8, < 3)
36
+ simplecov-html (~> 0.10.0)
37
+ simplecov-html (0.10.2)
38
+ slop (3.6.0)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler
45
+ minitest
46
+ minitest-utils
47
+ pry-meta
48
+ rake
49
+ simplecov
50
+ time_window!
51
+
52
+ BUNDLED WITH
53
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nando Vieira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # TimeWindow
2
+
3
+ [![Travis-CI](https://travis-ci.org/fnando/time_window.svg)](https://travis-ci.org/fnando/time_window)
4
+ [![CodeClimate](https://codeclimate.com/github/fnando/time_window.svg)](https://codeclimate.com/github/fnando/time_window)
5
+ [![Test Coverage](https://codeclimate.com/github/fnando/time_window/badges/coverage.svg)](https://codeclimate.com/github/fnando/time_window/coverage)
6
+ [![Gem](https://img.shields.io/gem/v/time_window.svg)](https://rubygems.org/gems/time_window)
7
+ [![Gem](https://img.shields.io/gem/dt/time_window.svg)](https://rubygems.org/gems/time_window)
8
+
9
+ Retrieve time objects by time windows (e.g. TimeWindow.call('5m') will round minutes to multiples of 5).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'time_window'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install time_window
26
+
27
+ ## Usage
28
+
29
+ You can get a time window for any given time object. By default, it uses `Time.now`.
30
+
31
+ ```ruby
32
+ require "time_window"
33
+
34
+ Time.now
35
+ #=> 2018-12-10 07:07:14 -0800
36
+
37
+ TimeWindow.call("5m")
38
+ #=> 2018-12-10 07:05:00 -0800
39
+ ```
40
+
41
+ You can use the following units as time windows:
42
+
43
+ - `s`: second
44
+ - `m`: minute
45
+ - `h`: hour
46
+
47
+ To specify a custom time object, just pass a second argument.
48
+
49
+ ```ruby
50
+ TimeWindow.call "15m", Time.parse("2032-11-23T14:55:26")
51
+ #=> 2032-11-23 14:45:00 -0800
52
+ ```
53
+
54
+ By default, `Time.now` is used as the default time. You can override the generator by setting `TimeWindow.now_proc`.
55
+
56
+ ```ruby
57
+ # https://api.rubyonrails.org/classes/Time.html#method-c-current
58
+ TimeWindow.now_proc = -> { Time.current }
59
+ ```
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/time_window. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
74
+
75
+ ## Code of Conduct
76
+
77
+ Everyone interacting in the TimeWindow project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fnando/time_window/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "time_window"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,66 @@
1
+ module TimeWindow
2
+ require "time_window/version"
3
+
4
+ InvalidUnit = Class.new(StandardError)
5
+ InvalidWindow = Class.new(StandardError)
6
+
7
+ ALIASES = {
8
+ "s" => "second",
9
+ "m" => "minute",
10
+ "h" => "hour"
11
+ }.freeze
12
+
13
+ class << self
14
+ attr_accessor :now_proc
15
+ end
16
+
17
+ self.now_proc = -> { Time.now }
18
+
19
+ def self.now
20
+ now_proc.call
21
+ end
22
+
23
+ # Retrieve the formatted date time window.
24
+ # The `time` param defaults to the current time.
25
+ # Returns a `Time` object.
26
+ #
27
+ # Accept units are: s (second), m (minute), h (hour).
28
+ #
29
+ # TimeWindow.call("5m")
30
+ # #=> 2018-12-10 05:40:00 -0800
31
+ #
32
+ def self.call(window, time = now)
33
+ _, window, unit = *window.match(/\A(\d+)(.)\z/)
34
+
35
+ window = Integer(window)
36
+ unit = ALIASES.fetch(unit) { raise InvalidUnit, "#{unit.inspect} is not a valid unit" }
37
+
38
+ hour, minute, second = public_send("process_#{unit}_window", time, window)
39
+
40
+ Time.new(time.year, time.month, time.day, hour, minute, second)
41
+ end
42
+
43
+ def self.process_second_window(time, window)
44
+ raise InvalidWindow, "#{window.inspect} must be covered by 1..59" unless (1..59).cover?(window)
45
+
46
+ second = time.sec - (time.sec % window)
47
+
48
+ [time.hour, time.min, second]
49
+ end
50
+
51
+ def self.process_minute_window(time, window)
52
+ raise InvalidWindow, "#{window.inspect} must be covered by 1..59" unless (1..59).cover?(window)
53
+
54
+ minute = time.min - (time.min % window)
55
+
56
+ [time.hour, minute, 0]
57
+ end
58
+
59
+ def self.process_hour_window(time, window)
60
+ raise InvalidWindow, "#{window.inspect} must be covered by 1..23" unless (1..23).cover?(window)
61
+
62
+ hour = time.hour - (time.hour % window)
63
+
64
+ [hour, 0, 0]
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module TimeWindow
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require "./lib/time_window/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "time_window"
5
+ spec.version = TimeWindow::VERSION
6
+ spec.authors = ["Nando Vieira"]
7
+ spec.email = ["me@fnando.com"]
8
+
9
+ spec.summary = "Retrieve time objects by time windows (e.g. TimeWindow.call('5m') will round minutes to multiples of 5)."
10
+ spec.description = spec.summary
11
+ spec.homepage = "https://github.com/fnando/time_window"
12
+ spec.license = "MIT"
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "minitest-utils"
26
+ spec.add_development_dependency "pry-meta"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "simplecov"
29
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_window
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-utils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-meta
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: rake
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
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Retrieve time objects by time windows (e.g. TimeWindow.call('5m') will
98
+ round minutes to multiples of 5).
99
+ email:
100
+ - me@fnando.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/time_window.rb
116
+ - lib/time_window/version.rb
117
+ - time_window.gemspec
118
+ homepage: https://github.com/fnando/time_window
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.7.6
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Retrieve time objects by time windows (e.g. TimeWindow.call('5m') will round
142
+ minutes to multiples of 5).
143
+ test_files: []