date_range_rollout 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 73cde61347df868f2cbb1b74b5ec0e04dc3f831e10b62afa2999158f19d209c3
4
+ data.tar.gz: 5e4e2bf729d43a9b25c260988fb3412410af37642ad158466584cd39fbcc8182
5
+ SHA512:
6
+ metadata.gz: addd646c365bea6a36f89cf6aefdf5b1ab3759b3678da0e50f8b6b3a3f5c3b4b9ab38af28dadf664d8aa7f73205a4c4a31b9443668b281f6e23b40cfb3d1d66e
7
+ data.tar.gz: 8848fb9685a7ccdc03935dd6d49dccbb0e6cfa390535fa02e7ef120cae0fe58565380e0db1b50b7b8f283ebdd1922be255dc332bac79e4f4a229ae6f530d8208
@@ -0,0 +1,28 @@
1
+ name: Run Tests
2
+ on: [push, pull_request]
3
+
4
+ jobs:
5
+ tests:
6
+ name: Tests
7
+ runs-on: ubuntu-latest
8
+
9
+ steps:
10
+ - name: Checkout code
11
+ uses: actions/checkout@v2
12
+ - name: Setup Ruby
13
+ uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6
16
+ - name: Ruby gem cache
17
+ uses: actions/cache@v1
18
+ with:
19
+ path: vendor/bundle
20
+ key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
21
+ restore-keys: |
22
+ ${{ runner.os }}-gems-
23
+ - name: Install gems
24
+ run: |
25
+ bundle config path vendor/bundle
26
+ bundle install --retry 3
27
+ - name: Run tests
28
+ run: bundle exec rake test
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
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 date_range_rollout.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Netlify
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.
@@ -0,0 +1,67 @@
1
+ # DateRangeRollout
2
+
3
+ Gradual transition from one value to another between two dates.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'date_range_rollout'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install date_range_rollout
20
+
21
+ ## Usage
22
+
23
+ Example: Get the percentage of time elapsed in the year:
24
+ ```ruby
25
+ start_value = 0
26
+ start_date = Time.parse("2020-01-01")
27
+ end_value = 100
28
+ end_date = Time.parse("2020-12-31")
29
+
30
+ DateRangeRollout.build(start_value, start_date, end_value, end_date).get
31
+ ```
32
+
33
+ Example: Slowly increase the numbers of jobs per day over the course of a month.
34
+ Imagine this class being run every day. This will perform 100 (`start_value`)
35
+ jobs until `start_date`, slowly increase the number until `end_date`, and stay
36
+ at 500 (`end_value`) after that date.
37
+ Since `get` returns a float, we want to round that value to the nearest integer.
38
+ ```ruby
39
+ class SomeWorker
40
+ def perform
41
+ start_value = 100
42
+ start_date = Time.parse("2020-02-17")
43
+ end_value = 500
44
+ end_date = Time.parse("2020-03-17")
45
+
46
+ jobs_count = DateRangeRollout.build(start_value, start_date, end_value, end_date).get
47
+
48
+ jobs_count.round.times do
49
+ JobClass.do_something
50
+ end
51
+ end
52
+ end
53
+ ```
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/netlify/date_range_rollout.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "date_range_rollout"
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__)
@@ -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,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "date_range_rollout/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "date_range_rollout"
8
+ spec.version = DateRangeRollout::VERSION
9
+ spec.authors = ["Mike Heffner", "Esteban Pastorino"]
10
+ spec.email = ["mike.heffner@netlify.com", "esteban@netlify.com"]
11
+
12
+ spec.summary = %q{Gradual transition from one value to another based on dates}
13
+ spec.description = %q{Gradual transition from one value to another based on dates}
14
+ spec.homepage = "https://github.com/netlify/date_range_rollout"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", ">= 1.17"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency "timecop", "~> 0.9.1"
30
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date_range_rollout/version"
4
+ require "date_range_rollout/rollout"
5
+
6
+ module DateRangeRollout
7
+ class Error < StandardError; end
8
+ class DateRangeError < Error; end
9
+
10
+ def self.build(start_duration, start_date, end_duration, end_date)
11
+ Rollout.new(start_duration, start_date, end_duration, end_date)
12
+ end
13
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module DateRangeRollout
6
+ class Rollout
7
+ def initialize(start_duration, start_date, end_duration, end_date)
8
+ if start_date > end_date
9
+ raise DateRangeError, "Start date should be before end date"
10
+ end
11
+
12
+ @start_duration = start_duration
13
+ @start_date = start_date
14
+ @end_duration = end_duration
15
+ @end_date = end_date
16
+ end
17
+
18
+ def get
19
+ now = Time.now
20
+
21
+ if now <= @start_date
22
+ return @start_duration.to_f
23
+ end
24
+
25
+ if now >= @end_date
26
+ return @end_duration.to_f
27
+ end
28
+
29
+ offset = now.to_f - @start_date.to_f
30
+ pct = offset / (@end_date.to_f - @start_date.to_f)
31
+
32
+ pct * (@end_duration - @start_duration) + @start_duration
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module DateRangeRollout
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_range_rollout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Heffner
8
+ - Esteban Pastorino
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-06-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '1.17'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '1.17'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '5.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '5.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: timecop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.9.1
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.1
70
+ description: Gradual transition from one value to another based on dates
71
+ email:
72
+ - mike.heffner@netlify.com
73
+ - esteban@netlify.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".github/workflows/test.yml"
79
+ - ".gitignore"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - date_range_rollout.gemspec
87
+ - lib/date_range_rollout.rb
88
+ - lib/date_range_rollout/rollout.rb
89
+ - lib/date_range_rollout/version.rb
90
+ homepage: https://github.com/netlify/date_range_rollout
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Gradual transition from one value to another based on dates
113
+ test_files: []