time_pricing 0.0.0 → 1.0.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.
- checksums.yaml +4 -4
- data/.gitignore +59 -0
- data/.rspec +3 -0
- data/.travis.yml +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +211 -0
- data/Rakefile +10 -0
- data/_config.yml +1 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/time_pricing.rb +20 -3
- data/lib/time_pricing/base.rb +34 -0
- data/lib/time_pricing/calculation.rb +118 -0
- data/lib/time_pricing/config.rb +41 -0
- data/lib/time_pricing/plan.rb +25 -0
- data/lib/time_pricing/utils.rb +22 -0
- data/lib/time_pricing/version.rb +3 -0
- data/time_pricing.gemspec +33 -0
- metadata +88 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d0a50ee4383425a1ff741d67843221521a8c8f9b
|
|
4
|
+
data.tar.gz: 1e41bc3d686ea381d9266930a8bf837291541782
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: efdfb185cca8b25356d62ad21c587b9551b2a956ae78f43123f3e19f43ed6bde60585c5263e03dafc5b47fb3b3a2a825c5949a45a8e80c2608e4706c1c712b8f
|
|
7
|
+
data.tar.gz: 78c4bc3016db11c8741583695d290510c60fa6deab200836f820cf7559afabdf74b4f0713d206b9ed5c59e3fdcb8219fc8c3283bc43c40f6a62056c738a9c611
|
data/.gitignore
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
/.config
|
|
4
|
+
/coverage/
|
|
5
|
+
/InstalledFiles
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/spec/examples.txt
|
|
9
|
+
/test/tmp/
|
|
10
|
+
/test/version_tmp/
|
|
11
|
+
/tmp/
|
|
12
|
+
|
|
13
|
+
# Used by dotenv library to load environment variables.
|
|
14
|
+
# .env
|
|
15
|
+
|
|
16
|
+
# Ignore Byebug command history file.
|
|
17
|
+
.byebug_history
|
|
18
|
+
|
|
19
|
+
## Specific to RubyMotion:
|
|
20
|
+
.dat*
|
|
21
|
+
.repl_history
|
|
22
|
+
build/
|
|
23
|
+
*.bridgesupport
|
|
24
|
+
build-iPhoneOS/
|
|
25
|
+
build-iPhoneSimulator/
|
|
26
|
+
|
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
|
28
|
+
#
|
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
|
32
|
+
#
|
|
33
|
+
# vendor/Pods/
|
|
34
|
+
|
|
35
|
+
## Documentation cache and generated files:
|
|
36
|
+
/.yardoc/
|
|
37
|
+
/_yardoc/
|
|
38
|
+
/doc/
|
|
39
|
+
/rdoc/
|
|
40
|
+
|
|
41
|
+
## Environment normalization:
|
|
42
|
+
/.bundle/
|
|
43
|
+
/vendor/bundle
|
|
44
|
+
/lib/bundler/man/
|
|
45
|
+
|
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
48
|
+
# Gemfile.lock
|
|
49
|
+
# .ruby-version
|
|
50
|
+
# .ruby-gemset
|
|
51
|
+
|
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
53
|
+
.rvmrc
|
|
54
|
+
|
|
55
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
|
56
|
+
# .rubocop-https?--*
|
|
57
|
+
|
|
58
|
+
# rspec failure tracking
|
|
59
|
+
.rspec_status
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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 im@danielpaul.me. 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
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
time_pricing (1.0.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
activesupport (5.2.4.2)
|
|
10
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
11
|
+
i18n (>= 0.7, < 2)
|
|
12
|
+
minitest (~> 5.1)
|
|
13
|
+
tzinfo (~> 1.1)
|
|
14
|
+
concurrent-ruby (1.1.6)
|
|
15
|
+
i18n (1.8.2)
|
|
16
|
+
concurrent-ruby (~> 1.0)
|
|
17
|
+
minitest (5.14.0)
|
|
18
|
+
rake (13.0.1)
|
|
19
|
+
thread_safe (0.3.6)
|
|
20
|
+
tzinfo (1.2.7)
|
|
21
|
+
thread_safe (~> 0.1)
|
|
22
|
+
|
|
23
|
+
PLATFORMS
|
|
24
|
+
ruby
|
|
25
|
+
|
|
26
|
+
DEPENDENCIES
|
|
27
|
+
activesupport
|
|
28
|
+
bundler (~> 2.0)
|
|
29
|
+
minitest (~> 5.14)
|
|
30
|
+
rake (~> 13.0)
|
|
31
|
+
time_pricing!
|
|
32
|
+
|
|
33
|
+
BUNDLED WITH
|
|
34
|
+
2.0.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Identity Square
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# ⏳💰 TimePricing
|
|
2
|
+
|
|
3
|
+
Calculate time based pricing based on duration or start time + end time. Useful for services, bookings or appointments where pricing is based on duration. This can be used with repeating combination of plans or without combining plans.
|
|
4
|
+
|
|
5
|
+
## 🛠Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'time_pricing'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install time_pricing
|
|
20
|
+
|
|
21
|
+
## ✅ Basic Usage Example
|
|
22
|
+
|
|
23
|
+
Initialize the service, define available plans and query for price with a time range or duration.
|
|
24
|
+
|
|
25
|
+
``` ruby
|
|
26
|
+
# New pricing calculation
|
|
27
|
+
time_pricing = TimePricing.new
|
|
28
|
+
|
|
29
|
+
# Add available plans
|
|
30
|
+
|
|
31
|
+
time_pricing.add_plan!({
|
|
32
|
+
name: 'per_hour',
|
|
33
|
+
duration: 1.hour, # duration in milliseconds
|
|
34
|
+
cost: 500 # €5.00 for an hour
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
time_pricing.add_plan!({
|
|
38
|
+
name: 'per_day',
|
|
39
|
+
duration: 1.day,
|
|
40
|
+
cost: 2000 # €20.00 for 1 day
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
# Calculate with time ranges
|
|
44
|
+
|
|
45
|
+
cost = time_pricing.for_time(Time.now, Time.now + 6.hours).cost
|
|
46
|
+
|
|
47
|
+
# OR with duration
|
|
48
|
+
|
|
49
|
+
cost = time_pricing.for_duration(6.hours).cost
|
|
50
|
+
|
|
51
|
+
puts cost
|
|
52
|
+
# => 2000
|
|
53
|
+
# (€20.00 because 'per day' rate is cheaper than 6 * 'per hour' rate of €60.00 in total)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 🛠 Options
|
|
57
|
+
|
|
58
|
+
### Initializing
|
|
59
|
+
|
|
60
|
+
``` ruby
|
|
61
|
+
TimePricing.new({
|
|
62
|
+
combine_plans: true
|
|
63
|
+
})
|
|
64
|
+
```
|
|
65
|
+
* `combine_plans` *(optional, default `true`)*: Set to `false` to not combine multiple plans to make up the duration. Each plan is only added with itself to make up the duration.
|
|
66
|
+
|
|
67
|
+
### Adding a plan
|
|
68
|
+
|
|
69
|
+
``` ruby
|
|
70
|
+
time_pricing.add_plan!({
|
|
71
|
+
name: 'per_hour',
|
|
72
|
+
duration: 1.hour,
|
|
73
|
+
cost: 2000
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
* `name` *(required)*: a unique identifier for each plan
|
|
78
|
+
* `duration` *(required)*: milliseconds of how long is this plan for
|
|
79
|
+
* `cost` *(required)*: a positive integer representing how much to charge in the smallest currency unit (e.g., 100 cents to charge $1.00 or 100 to charge ¥100, a zero-decimal currency). Can be set to 0 for a free plan.
|
|
80
|
+
|
|
81
|
+
### Removing a plan
|
|
82
|
+
|
|
83
|
+
``` ruby
|
|
84
|
+
# remove a plan with it's unique name
|
|
85
|
+
time_pricing.remove_plan!('per_day')
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Other methods
|
|
89
|
+
``` ruby
|
|
90
|
+
# a list of plans that are setup. Returns an array of plan objects
|
|
91
|
+
time_pricing.plans
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Calculating price
|
|
95
|
+
|
|
96
|
+
``` ruby
|
|
97
|
+
time_pricing.for_time(Time.now, Time.now + 6.hours).cost
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
* `start_time` *(required)*: timestamp
|
|
101
|
+
* `end_time` *(required)*: timestamp
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
``` ruby
|
|
105
|
+
time_pricing.for_duration(6.hours).cost
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
* `duration` *(required)*
|
|
109
|
+
|
|
110
|
+
### Calculation breakdown
|
|
111
|
+
How did we get to the final cost.
|
|
112
|
+
|
|
113
|
+
``` ruby
|
|
114
|
+
calculation = time_pricing.for_duration(6.hours)
|
|
115
|
+
|
|
116
|
+
# price in cents
|
|
117
|
+
calculation.cost
|
|
118
|
+
|
|
119
|
+
# if using the for_time method
|
|
120
|
+
calculation.start_time
|
|
121
|
+
calculation.end_time
|
|
122
|
+
|
|
123
|
+
# the requested duration for pricing
|
|
124
|
+
calculation.duration
|
|
125
|
+
|
|
126
|
+
# Total duration (in milliseconds) the plans make up
|
|
127
|
+
calculation.total_duration
|
|
128
|
+
|
|
129
|
+
# any extra duration (in milliseconds) than the requested
|
|
130
|
+
# to make up for the requested duration
|
|
131
|
+
calculation.extra_duration
|
|
132
|
+
|
|
133
|
+
# breakdown of how the cost was calculated and what plans were used
|
|
134
|
+
calculation.breakdown
|
|
135
|
+
# [
|
|
136
|
+
# {
|
|
137
|
+
# start_time: "",
|
|
138
|
+
# end_time: "",
|
|
139
|
+
# duration: 0,
|
|
140
|
+
# name: "per_day",
|
|
141
|
+
# cost: 1000
|
|
142
|
+
# },
|
|
143
|
+
# {
|
|
144
|
+
# start_time: "",
|
|
145
|
+
# end_time: "",
|
|
146
|
+
# duration: 0,
|
|
147
|
+
# name: "per_day",
|
|
148
|
+
# cost: 1000
|
|
149
|
+
# },
|
|
150
|
+
# {...}
|
|
151
|
+
# ]
|
|
152
|
+
# start_time and end_time only appears if using for_time to calculate cost
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## 🏎 Caching the combinations externally
|
|
157
|
+
|
|
158
|
+
TimePricing uses cache to keep track of pricing that we have calculated already to speed up the going through all the combinations possible if `combine_plan: true`. This significantly speeds up the calculations. This can be saved externally from the gem and can be set for even faster look up /for the same plans/.
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
``` ruby
|
|
162
|
+
# Get the cache
|
|
163
|
+
# Save this in your application's persisted cache
|
|
164
|
+
saved_cache = time_pricing.cache
|
|
165
|
+
|
|
166
|
+
# Setting cache back for another session
|
|
167
|
+
TimePricing.new({cache: saved_cache})
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
*Important:* Be sure to clear your externally saved cache if you add, remove or change plans.
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
## ⚠️ Known Issues
|
|
174
|
+
|
|
175
|
+
If you have smaller duration plans and are looking for pricing for a large duration (approx. over 500 times the largest duration plan - which is a lot!), you might encounter `stack level too deep` error. Consider limiting the duration you query a pricing for.
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
## 🤓 Contributing
|
|
179
|
+
|
|
180
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/identitysquare/time_pricing. 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.
|
|
181
|
+
|
|
182
|
+
### Development
|
|
183
|
+
|
|
184
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
185
|
+
|
|
186
|
+
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).
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
191
|
+
|
|
192
|
+
## Code of Conduct
|
|
193
|
+
|
|
194
|
+
Everyone interacting in the TimePricing project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/time_pricing/blob/master/CODE_OF_CONDUCT.md).
|
|
195
|
+
|
|
196
|
+
## 🥳 Contributors
|
|
197
|
+
|
|
198
|
+
<!-- ALL-CONTRIBUTORS-LIST:START -->
|
|
199
|
+
<!-- prettier-ignore-start -->
|
|
200
|
+
<!-- markdownlint-disable -->
|
|
201
|
+
<table>
|
|
202
|
+
<tr>
|
|
203
|
+
<td align="center"><a href="https://github.com/Annajoe96"><img src="https://avatars0.githubusercontent.com/u/57370408?v=4" width="100px;" alt=""/><br /><sub><b>Anna Joe</b></sub></a></td>
|
|
204
|
+
<td align="center"><a href="https://github.com/danielpaul"><img src="https://avatars3.githubusercontent.com/u/333233?v=4" width="100px;" alt=""/><br /><sub><b>Daniel Paul</b></sub></a></td>
|
|
205
|
+
<td align="center"><a href="https://github.com/iJohnPaul"><img src="https://avatars0.githubusercontent.com/u/25507937?v=4" width="100px;" alt=""/><br /><sub><b>John Paul</b></sub></a></td>
|
|
206
|
+
</tr>
|
|
207
|
+
</table>
|
|
208
|
+
|
|
209
|
+
<!-- markdownlint-enable -->
|
|
210
|
+
<!-- prettier-ignore-end -->
|
|
211
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
data/Rakefile
ADDED
data/_config.yml
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
theme: jekyll-theme-leap-day
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "time_pricing"
|
|
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
data/lib/time_pricing.rb
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
RubyVM::InstructionSequence.compile_option = {
|
|
2
|
+
tailcall_optimization: true,
|
|
3
|
+
trace_instruction: false
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
require 'time_pricing/plan'
|
|
7
|
+
require 'time_pricing/utils'
|
|
8
|
+
require 'time_pricing/config'
|
|
9
|
+
require 'time_pricing/base'
|
|
10
|
+
require 'time_pricing/calculation'
|
|
11
|
+
|
|
12
|
+
module TimePricing
|
|
13
|
+
class Error < StandardError; end
|
|
14
|
+
class ParameterMissing < StandardError; end
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def new(**args)
|
|
18
|
+
config = TimePricing::Config.new(args)
|
|
19
|
+
TimePricing::Base.new(config)
|
|
20
|
+
end
|
|
4
21
|
end
|
|
5
22
|
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module TimePricing
|
|
2
|
+
class Base
|
|
3
|
+
attr_reader :config
|
|
4
|
+
|
|
5
|
+
def initialize(config)
|
|
6
|
+
@config = config
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def plans
|
|
10
|
+
@config.plans
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add_plan!(**args)
|
|
14
|
+
@config.add_plan!(**args)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def remove_plan!(name)
|
|
18
|
+
@config.remove_plan!(name)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def combine_plans?
|
|
22
|
+
@config.combine_plans?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def for_time(start_time, end_time)
|
|
26
|
+
Calculation.new(config).for_time(start_time, end_time)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def for_duration(duration)
|
|
30
|
+
Calculation.new(config).for_duration(duration)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
module TimePricing
|
|
2
|
+
class Calculation
|
|
3
|
+
attr_reader :config, :start_time, :end_time, :duration, :cost, :plans_used
|
|
4
|
+
|
|
5
|
+
def initialize(config)
|
|
6
|
+
@config = config
|
|
7
|
+
@start_time = nil
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def for_time(start_time, end_time)
|
|
11
|
+
@start_time = start_time
|
|
12
|
+
@end_time = end_time
|
|
13
|
+
|
|
14
|
+
# find duration from start_time & end_time
|
|
15
|
+
duration = (end_time - start_time).to_i
|
|
16
|
+
for_duration(duration)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def for_duration(duration)
|
|
20
|
+
@duration = duration
|
|
21
|
+
|
|
22
|
+
if @config.combine_plans?
|
|
23
|
+
result = best_cost_with_combos(duration)
|
|
24
|
+
else
|
|
25
|
+
result = best_cost_without_combos(duration)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
@cost = result[:cost]
|
|
29
|
+
@plans_used = result[:plans_used]
|
|
30
|
+
|
|
31
|
+
self
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def breakdown
|
|
35
|
+
result = []
|
|
36
|
+
current_start_time = @start_time
|
|
37
|
+
|
|
38
|
+
@plans_used.each do |plan_name|
|
|
39
|
+
plan = @config.plans[plan_name]
|
|
40
|
+
plan_json = plan.to_json
|
|
41
|
+
|
|
42
|
+
if @start_time
|
|
43
|
+
end_time = @start_time + plan.duration
|
|
44
|
+
|
|
45
|
+
plan_json.merge!({
|
|
46
|
+
start_time: current_start_time,
|
|
47
|
+
end_time: end_time
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
current_start_time = end_time + 1
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
result << plan_json
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
result
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def total_duration
|
|
60
|
+
duration = 0
|
|
61
|
+
|
|
62
|
+
@plans_used.each do |plan_name|
|
|
63
|
+
plan = @config.plans[plan_name]
|
|
64
|
+
duration += plan.duration
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
duration
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def extra_duration
|
|
71
|
+
total_duration - @duration
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def best_cost_without_combos(duration)
|
|
77
|
+
costs = []
|
|
78
|
+
|
|
79
|
+
@config.plans.each do |key, plan|
|
|
80
|
+
costs << cost_for_plan(plan, duration)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# puts costs
|
|
84
|
+
costs.min_by{|n| n[:cost] }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def cost_for_plan(plan, duration)
|
|
89
|
+
return Utils.cost_struct(0) if duration <= 0
|
|
90
|
+
|
|
91
|
+
Utils.add_hash(
|
|
92
|
+
Utils.cost_struct(plan.cost, [plan.name]),
|
|
93
|
+
cost_for_plan(plan, duration - plan.duration)
|
|
94
|
+
)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def best_cost_with_combos(duration)
|
|
99
|
+
return Utils.cost_struct(0) if duration <= 0
|
|
100
|
+
|
|
101
|
+
cache_key = duration.to_s.to_sym
|
|
102
|
+
if @config.cache[cache_key]
|
|
103
|
+
return @config.cache[cache_key]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
charges = []
|
|
107
|
+
|
|
108
|
+
@config.plans.each do |_key, plan|
|
|
109
|
+
current_plan = Utils.cost_struct(plan.cost, [plan.name])
|
|
110
|
+
remainder = best_cost_with_combos(duration - plan.duration)
|
|
111
|
+
charges << Utils.add_hash(current_plan, remainder)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
@config.cache[cache_key] = charges.min_by{|n| n[:cost] }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module TimePricing
|
|
2
|
+
class Config
|
|
3
|
+
attr_accessor :combine_plans, :plans, :cache
|
|
4
|
+
|
|
5
|
+
def initialize(**args)
|
|
6
|
+
@combine_plans = args[:combine_plans] || args[:combine_plans] == nil
|
|
7
|
+
|
|
8
|
+
@plans = {}
|
|
9
|
+
|
|
10
|
+
if args[:cache].is_a?(Hash)
|
|
11
|
+
@cache = args[:cache]
|
|
12
|
+
else
|
|
13
|
+
@cache = {}
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def clear_cache!
|
|
18
|
+
@cache = {}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def add_plan!(**args)
|
|
22
|
+
plan = Plan.new(args)
|
|
23
|
+
|
|
24
|
+
# clear cache as plans change
|
|
25
|
+
clear_cache!
|
|
26
|
+
|
|
27
|
+
@plans[plan.name] = plan
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def remove_plan!(name)
|
|
31
|
+
# clear cache as plans change
|
|
32
|
+
clear_cache!
|
|
33
|
+
@plans.delete(name)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def combine_plans?
|
|
37
|
+
@combine_plans
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module TimePricing
|
|
2
|
+
class Plan
|
|
3
|
+
attr_reader :name, :duration, :cost
|
|
4
|
+
|
|
5
|
+
def initialize(**args)
|
|
6
|
+
@name = args[:name]
|
|
7
|
+
@duration = args[:duration]
|
|
8
|
+
@cost = args[:cost]
|
|
9
|
+
|
|
10
|
+
# validate plan data
|
|
11
|
+
if !@name || !@duration.is_a?(Integer) || !@cost.is_a?(Integer)
|
|
12
|
+
raise TimePricing::ParameterMissing.new "Not a valid plan"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_json
|
|
17
|
+
{
|
|
18
|
+
name: @name,
|
|
19
|
+
duration: @duration,
|
|
20
|
+
cost: @cost
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module TimePricing
|
|
2
|
+
class Utils
|
|
3
|
+
class << self
|
|
4
|
+
|
|
5
|
+
def cost_struct(cost, plans_used=[])
|
|
6
|
+
{
|
|
7
|
+
cost: cost,
|
|
8
|
+
plans_used: plans_used
|
|
9
|
+
}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def add_hash(a, b)
|
|
13
|
+
result = {}
|
|
14
|
+
a.each do |key, value|
|
|
15
|
+
result[key] = a[key] + b[key]
|
|
16
|
+
end
|
|
17
|
+
result
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require "time_pricing/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "time_pricing"
|
|
7
|
+
spec.version = TimePricing::VERSION
|
|
8
|
+
spec.authors = ["Anna Joe", "Daniel Paul"]
|
|
9
|
+
spec.email = ["57370408+Annajoe96@users.noreply.github.com", "im@danielpaul.me"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "TimePricing"
|
|
12
|
+
spec.description = "A gem to calcualte pricing for a time based service, bookings or appointments."
|
|
13
|
+
spec.homepage = "https://identitysquare.github.io/time_pricing/"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/IdentitySquare/time_pricing"
|
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/IdentitySquare/time_pricing/blob/master/CHANGELOG.md"
|
|
19
|
+
|
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
24
|
+
end
|
|
25
|
+
spec.bindir = "exe"
|
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
27
|
+
spec.require_paths = ["lib"]
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
30
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
|
31
|
+
spec.add_development_dependency "minitest", "~> 5.14"
|
|
32
|
+
spec.add_development_dependency "activesupport"
|
|
33
|
+
end
|
metadata
CHANGED
|
@@ -1,27 +1,107 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: time_pricing
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Daniel Paul
|
|
8
7
|
- Anna Joe
|
|
8
|
+
- Daniel Paul
|
|
9
9
|
autorequire:
|
|
10
|
-
bindir:
|
|
10
|
+
bindir: exe
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2020-
|
|
13
|
-
dependencies:
|
|
12
|
+
date: 2020-04-09 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: '2.0'
|
|
21
|
+
type: :development
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - "~>"
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '2.0'
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: rake
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - "~>"
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '13.0'
|
|
35
|
+
type: :development
|
|
36
|
+
prerelease: false
|
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - "~>"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '13.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.14'
|
|
49
|
+
type: :development
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - "~>"
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '5.14'
|
|
56
|
+
- !ruby/object:Gem::Dependency
|
|
57
|
+
name: activesupport
|
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
type: :development
|
|
64
|
+
prerelease: false
|
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
14
70
|
description: A gem to calcualte pricing for a time based service, bookings or appointments.
|
|
15
|
-
email:
|
|
71
|
+
email:
|
|
72
|
+
- 57370408+Annajoe96@users.noreply.github.com
|
|
73
|
+
- im@danielpaul.me
|
|
16
74
|
executables: []
|
|
17
75
|
extensions: []
|
|
18
76
|
extra_rdoc_files: []
|
|
19
77
|
files:
|
|
78
|
+
- ".gitignore"
|
|
79
|
+
- ".rspec"
|
|
80
|
+
- ".travis.yml"
|
|
81
|
+
- CODE_OF_CONDUCT.md
|
|
82
|
+
- Gemfile
|
|
83
|
+
- Gemfile.lock
|
|
84
|
+
- LICENSE.txt
|
|
85
|
+
- README.md
|
|
86
|
+
- Rakefile
|
|
87
|
+
- _config.yml
|
|
88
|
+
- bin/console
|
|
89
|
+
- bin/setup
|
|
20
90
|
- lib/time_pricing.rb
|
|
21
|
-
|
|
91
|
+
- lib/time_pricing/base.rb
|
|
92
|
+
- lib/time_pricing/calculation.rb
|
|
93
|
+
- lib/time_pricing/config.rb
|
|
94
|
+
- lib/time_pricing/plan.rb
|
|
95
|
+
- lib/time_pricing/utils.rb
|
|
96
|
+
- lib/time_pricing/version.rb
|
|
97
|
+
- time_pricing.gemspec
|
|
98
|
+
homepage: https://identitysquare.github.io/time_pricing/
|
|
22
99
|
licenses:
|
|
23
100
|
- MIT
|
|
24
|
-
metadata:
|
|
101
|
+
metadata:
|
|
102
|
+
homepage_uri: https://identitysquare.github.io/time_pricing/
|
|
103
|
+
source_code_uri: https://github.com/IdentitySquare/time_pricing
|
|
104
|
+
changelog_uri: https://github.com/IdentitySquare/time_pricing/blob/master/CHANGELOG.md
|
|
25
105
|
post_install_message:
|
|
26
106
|
rdoc_options: []
|
|
27
107
|
require_paths:
|