billing_cycle 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +71 -0
- data/Rakefile +13 -0
- data/lib/billing_cycle.rb +5 -0
- data/lib/billing_cycle/billing_cycle.rb +94 -0
- data/lib/billing_cycle/version.rb +3 -0
- metadata +163 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de1567f4fc4e2813c2e8a24d7c38d4972de4dd92
|
4
|
+
data.tar.gz: e433e801af93c75c6d5dc056d9af3d34c236a0b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cb00109a6b6d23b477aa8c5b6b4c3304f8c39441bf42939f17624ab3e47300cb076727d7996dc44083f4596ae6487863016f2b0f3f5581dbd904d511c2b1878
|
7
|
+
data.tar.gz: 7cf9c934466b6d9c4092f87f8f3af6aa60b18817786529f86599371b749991ccaf36717ebf68474f28ffafed8eb12f112d4a46e2e5173e6a4afc0415e364972b
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Brian Pattison
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Billing Cycle
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/simplymadeapps/billing_cycle.svg?branch=master)](https://travis-ci.org/simplymadeapps/billing_cycle)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/simplymadeapps/billing_cycle/badges/gpa.svg)](https://codeclimate.com/github/simplymadeapps/billing_cycle)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/simplymadeapps/billing_cycle/badges/coverage.svg)](https://codeclimate.com/github/simplymadeapps/billing_cycle/coverage)
|
6
|
+
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/simplymadeapps/billing_cycle/)
|
7
|
+
|
8
|
+
Billing Cycle is a gem used to calculate the next billing date for a recurring subscription.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "billing_cycle"
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
$ bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
original_billing_date = Time.zone.parse("2018-01-31 00:00:00")
|
28
|
+
billing_interval = 1.month
|
29
|
+
billing_cycle = BillingCycle::BillingCycle.new(original_billing_date, billing_interval)
|
30
|
+
|
31
|
+
Time.zone.now
|
32
|
+
# => Tue, 26 Jun 2018 00:00:00 CDT -05:00
|
33
|
+
|
34
|
+
billing_cycle.next_due_at
|
35
|
+
# => Sat, 30 Jun 2018 00:00:00 CDT -05:00
|
36
|
+
|
37
|
+
billing_cycle.next_due_at(Time.zone.parse("2020-02-01 00:00:00")
|
38
|
+
# => Sat, 29 Feb 2020 00:00:00 CST -06:00
|
39
|
+
|
40
|
+
billing_cycle.previous_due_at
|
41
|
+
# => Thu, 31 May 2018 00:00:00 CDT -05:00
|
42
|
+
|
43
|
+
billing_cycle.previous_due_at(Time.zone.parse("2020-02-01 00:00:00")
|
44
|
+
# => Fri, 31 Jan 2020 00:00:00 CST -06:00
|
45
|
+
```
|
46
|
+
|
47
|
+
If the original billing date is in the future, the `next_due_at` will always be the
|
48
|
+
original billing date, and the `previous_due_at` will be `nil`.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
original_billing_date = Time.zone.parse("9999-12-31 00:00:00")
|
52
|
+
billing_interval = 1.month
|
53
|
+
billing_cycle = BillingCycle::BillingCycle.new(original_billing_date, billing_interval)
|
54
|
+
|
55
|
+
billing_cycle.next_due_at
|
56
|
+
# => Fri, 31 Dec 9999 00:00:00 CST -06:00
|
57
|
+
|
58
|
+
billing_cycle.previous_due_at
|
59
|
+
# => nil
|
60
|
+
```
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create new Pull Request
|
69
|
+
|
70
|
+
## License
|
71
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
begin
|
2
|
+
require "bundler/setup"
|
3
|
+
rescue LoadError
|
4
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
5
|
+
end
|
6
|
+
|
7
|
+
begin
|
8
|
+
require "rspec/core/rake_task"
|
9
|
+
RSpec::Core::RakeTask.new(:spec)
|
10
|
+
task default: :spec
|
11
|
+
rescue LoadError
|
12
|
+
puts "Could not load Rspec Rake task"
|
13
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_support/core_ext/integer"
|
3
|
+
require "active_support/time_with_zone"
|
4
|
+
|
5
|
+
module BillingCycle
|
6
|
+
# Utility class for calculating the next billing date for a subscription.
|
7
|
+
class BillingCycle
|
8
|
+
attr_accessor :created_at, :interval
|
9
|
+
|
10
|
+
# Initialize a billing cycle.
|
11
|
+
# @param created_at [Time] The date/time the subscription was created
|
12
|
+
# @param interval [ActiveSupport::Duration] The interval the subscription is paid
|
13
|
+
def initialize(created_at, interval)
|
14
|
+
@created_at = created_at
|
15
|
+
@interval = interval
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns the next billing date based on the subscription
|
19
|
+
# @param now [Time] The current time
|
20
|
+
# @return [Time] The next billing date/time
|
21
|
+
def next_due_at(now = Time.zone.now)
|
22
|
+
# The next billing date is always the original date if "now" is earlier than the original date
|
23
|
+
return created_at if now <= created_at
|
24
|
+
|
25
|
+
number_of_cycles = number_of_cycles_since_created(now)
|
26
|
+
next_due_at = calculate_due_at(number_of_cycles)
|
27
|
+
|
28
|
+
# The number of cycles since the billing cycle was created only gets us to "now"
|
29
|
+
# so we need probably need to add another cycle to get the next billing date.
|
30
|
+
while next_due_at < now
|
31
|
+
number_of_cycles += interval_value
|
32
|
+
next_due_at = calculate_due_at(number_of_cycles)
|
33
|
+
end
|
34
|
+
|
35
|
+
next_due_at
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the previous billing date based on the subscription
|
39
|
+
# @param now [Time] The current time
|
40
|
+
# @return [Time] The previous billing date/time
|
41
|
+
def previous_due_at(now = Time.zone.now)
|
42
|
+
# There was no previous billing date before the original billing date
|
43
|
+
return nil if now <= created_at
|
44
|
+
|
45
|
+
number_of_cycles = number_of_cycles_since_created(now) + interval_value
|
46
|
+
previous_due_at = calculate_due_at(number_of_cycles)
|
47
|
+
|
48
|
+
# The number of cycles since the billing cycle was created gets us to "now",
|
49
|
+
# and if now matches a billing date exactly, we want the previous billing date.
|
50
|
+
while previous_due_at >= now
|
51
|
+
number_of_cycles -= interval_value
|
52
|
+
previous_due_at = calculate_due_at(number_of_cycles)
|
53
|
+
end
|
54
|
+
|
55
|
+
previous_due_at
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Calculate the due date based on number of billing cycles since
|
61
|
+
# or before the date/time the subscription was created.
|
62
|
+
# @param number_of_cycles [Integer]
|
63
|
+
# @return [Time]
|
64
|
+
def calculate_due_at(number_of_cycles)
|
65
|
+
number_of_cycles.send(interval_units).from_now(created_at)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns the number from the interval's duration.
|
69
|
+
# @return [Integer] `6` for a duration of `6.months`
|
70
|
+
def interval_value
|
71
|
+
if interval.parts.is_a? Array
|
72
|
+
interval.parts[0][1]
|
73
|
+
else
|
74
|
+
interval.parts.values[0]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns the interval type from the interval's duration.
|
79
|
+
# @return [Symbol] `:month` for a duration of `1.month`
|
80
|
+
def interval_units
|
81
|
+
if interval.parts.is_a? Array
|
82
|
+
interval.parts[0][0]
|
83
|
+
else
|
84
|
+
interval.parts.keys[0]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns the number billing cycles that have occurred between the created date and "now".
|
89
|
+
# @return [Integer]
|
90
|
+
def number_of_cycles_since_created(now)
|
91
|
+
(interval_value * ((now - created_at).to_i / interval)).to_i
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: billing_cycle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Pattison
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-10 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.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: appraisal
|
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: codeclimate-test-reporter
|
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: rainbow
|
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: rspec-rails
|
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: rubocop
|
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
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov-rcov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Billing Cycle is a gem used to calculate the next billing date for a
|
126
|
+
recurring subscription.
|
127
|
+
email:
|
128
|
+
- brian@brianpattison.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- MIT-LICENSE
|
134
|
+
- README.md
|
135
|
+
- Rakefile
|
136
|
+
- lib/billing_cycle.rb
|
137
|
+
- lib/billing_cycle/billing_cycle.rb
|
138
|
+
- lib/billing_cycle/version.rb
|
139
|
+
homepage: https://github.com/simplymadeapps/billing_cycle
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
metadata: {}
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 2.5.2
|
160
|
+
signing_key:
|
161
|
+
specification_version: 4
|
162
|
+
summary: Utility for calculating the next billing date for a recurring subscription.
|
163
|
+
test_files: []
|