voucherify 0.5.1 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 331f6a24ae1e9b9a777a6918b266238fe742f88f
4
- data.tar.gz: b3ce1cfd31685e08e0e1167c71bd9882708ec6c5
3
+ metadata.gz: aa1601ccbeefeb58cfa46de45babc383d27a6afe
4
+ data.tar.gz: 07448203a4f06daabf97e0e282f37971e7235d51
5
5
  SHA512:
6
- metadata.gz: 2a4b14ca70950f4e9cac4480185bdaebb605c9e706b179a03fb3f5d9220716c6afda7d24486474366d51ad0d8ffc827b59a63b65869d02c099bd54480ed72b9f
7
- data.tar.gz: a272767515360acc0748c813380ec44f119e32afeb1932c92355e8194dd19a7e28419684509b832ec50fe3f6f67c84f6bbcdb1b8a70b8a338c801816b0f05420
6
+ metadata.gz: 006a45e1b0042c58e9ac589c8f75385ea5186020b65bb60c695b7d3cd45adc7666c0837ba16afd096665136d14e8bce52bd8bb9e1f315289fd9feba3b565130a
7
+ data.tar.gz: d9e6cf8f30ef571f2297f3b3f2cd23278d77375b4e31d2bdc9eb4896e6a97bea2895137f25e7bb7f4460805d6756a51a412a186187b59379e6ce787a40bcf95f
data/README.md CHANGED
@@ -381,7 +381,7 @@ voucherify.redeem({
381
381
 
382
382
  ##### 4. With customer id
383
383
 
384
- If you already created a customer profile in Voucherify's database, whether it was implicitly by providing it to the `redeem` function or explicitly by invoking the [`customer.create`](#create-customer) method, you can identify your customer in following redemptions by a generated id (starting with `cust_`).
384
+ If you already created a customer profile in Voucherify's database, whether it was implicitly by providing it to the `redeem` function or explicitly by invoking the [`customer.create`](#create-customer) method, you can identify your customer in following redemptions by a generated id (starting with `cust_`).
385
385
 
386
386
  ```ruby
387
387
  voucherify.redeem({
@@ -412,7 +412,7 @@ Filter parameters:
412
412
 
413
413
  - limit (default: 100)
414
414
  - page (default: 0)
415
- - start_date (default: beginning of current month)
415
+ - start_date (default: beginning of current month)
416
416
  - end_date (default: end of current month)
417
417
  - result - Success | Failure-NotExist | Failure-Inactive
418
418
  - customer - id or source_id
@@ -653,6 +653,67 @@ voucherify.delete_customer("cust_WGG615E92dhOHz7PV9Vo9gk9")
653
653
 
654
654
  The response has empty body.
655
655
 
656
+ #### Utilities
657
+
658
+ These methods are used to help calculate prices and discounts.
659
+
660
+ ```ruby
661
+ Utils.calculate_price(base_price, voucher, unit_price)
662
+ Utils.calculate_discount(base_price, voucher, unit_price)
663
+ ```
664
+
665
+ Example:
666
+
667
+ ```ruby
668
+ voucher = {
669
+ category: "New Customers",
670
+ type: "DISCOUNT_VOUCHER",
671
+ discount: {
672
+ percent_off: 10.0,
673
+ type: "PERCENT"
674
+ },
675
+ start_date: "2016-01-01T00:00:00Z",
676
+ expiration_date: "2016-12-31T23:59:59Z",
677
+ redemption: {
678
+ quantity: 1
679
+ }
680
+ }
681
+ ```
682
+ ```ruby
683
+ Utils.calculate_price(20.0, voucher, 0);
684
+ ```
685
+
686
+ Result:
687
+ ```ruby
688
+ 18
689
+ ```
690
+
691
+ Example:
692
+
693
+ ```ruby
694
+ voucher = {
695
+ category: "New Customers",
696
+ type: "DISCOUNT_VOUCHER",
697
+ discount: {
698
+ amount_off: 7.0,
699
+ type: "AMOUNT"
700
+ },
701
+ start_date: "2016-01-01T00:00:00Z",
702
+ expiration_date: "2016-12-31T23:59:59Z",
703
+ redemption: {
704
+ quantity: 1
705
+ }
706
+ }
707
+ ```
708
+ ```ruby
709
+ Utils.calculate_discount(20.0, voucher, 0);
710
+ ```
711
+
712
+ Result:
713
+ ```ruby
714
+ 7
715
+ ```
716
+
656
717
  ## Development
657
718
 
658
719
  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.
@@ -664,6 +725,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
664
725
  Bug reports and pull requests are welcome on GitHub at https://github.com/rspective/voucherify-ruby-sdk.
665
726
 
666
727
  ## Changelog
728
+ - **2016-07-05** - `0.6.0` - new utils module
667
729
  - **2016-06-16** - `0.5.0` - unified naming convention
668
730
  - **2016-06-12** - `0.4.0` - new customer sdk methods
669
731
  - **2016-05-24** - `0.3.0` - new publish structure
data/examples/utils.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'voucherify/utils'
2
+
3
+ include Utils
4
+
5
+ discount = Utils.calculate_discount(20, {
6
+ discount: {
7
+ unit_off: 2.0,
8
+ type: 'UNIT'
9
+ }
10
+ }, 5)
11
+
12
+ print(discount)
13
+
14
+ discount = Utils.calculate_price(20, amount_voucher_object = {
15
+ discount: {
16
+ amount_off: 7.0,
17
+ type: 'AMOUNT'
18
+ }
19
+ })
20
+
21
+ print(discount)
@@ -0,0 +1,85 @@
1
+ module Utils
2
+ def round_money(value)
3
+ value.round(2)
4
+ end
5
+
6
+ def validate_percent_discount(discount)
7
+ if !(discount.is_a? Numeric) || !discount.between?(0, 100)
8
+ raise "Invalid voucher, percent discount should be between 0-100."
9
+ end
10
+ end
11
+
12
+ def validate_amount_discount(discount)
13
+ if !(discount.is_a? Numeric) || discount < 0
14
+ raise "Invalid voucher, amount discount must be bigger than zero."
15
+ end
16
+ end
17
+
18
+ def validate_unit_discount(discount)
19
+ if !(discount.is_a? Numeric) || discount < 0
20
+ raise "Invalid voucher, unit discount must be bigger than zero."
21
+ end
22
+ end
23
+
24
+ def calculate_price(base_price, voucher, unit_price = nil)
25
+ if !voucher[:discount]
26
+ raise "Unsupported voucher type."
27
+ end
28
+
29
+ if voucher[:discount][:type] === 'PERCENT'
30
+ discount = voucher[:discount][:percent_off]
31
+ validate_percent_discount(discount);
32
+ price_discount = base_price * (discount / 100)
33
+ return round_money(base_price - price_discount)
34
+
35
+ elsif voucher[:discount][:type] === 'AMOUNT'
36
+ discount = voucher[:discount][:amount_off]
37
+ validate_amount_discount(discount)
38
+ new_price = base_price - discount
39
+ return round_money(new_price > 0 ? (new_price) : 0)
40
+
41
+ elsif voucher[:discount][:type] === 'UNIT'
42
+ if !unit_price
43
+ raise "Missing unit_price argument."
44
+ end
45
+ discount = voucher[:discount][:unit_off]
46
+ validate_unit_discount(discount)
47
+ new_price = base_price - unit_price * discount
48
+ return round_money(new_price > 0 ? (new_price) : 0)
49
+
50
+ else
51
+ raise "Unsupported discount type"
52
+ end
53
+ end
54
+
55
+ def calculate_discount(base_price, voucher, unit_price = nil)
56
+ if !voucher[:discount]
57
+ raise "Unsupported voucher type."
58
+ end
59
+
60
+ if voucher[:discount][:type] === 'PERCENT'
61
+ discount = voucher[:discount][:percent_off]
62
+ validate_percent_discount(discount);
63
+ price_discount = base_price * (discount / 100)
64
+ return round_money(price_discount)
65
+
66
+ elsif voucher[:discount][:type] === 'AMOUNT'
67
+ discount = voucher[:discount][:amount_off]
68
+ validate_amount_discount(discount)
69
+ new_price = base_price - discount
70
+ return round_money(new_price > 0 ? (discount) : (base_price))
71
+
72
+ elsif voucher[:discount][:type] === 'UNIT'
73
+ if !unit_price
74
+ raise "Missing unit_price argument."
75
+ end
76
+ discount = voucher[:discount][:unit_off]
77
+ validate_unit_discount(discount)
78
+ price_discount = unit_price * discount
79
+ return round_money(price_discount > base_price ? (base_price) : (price_discount))
80
+
81
+ else
82
+ raise "Unsupported discount type"
83
+ end
84
+ end
85
+ end
@@ -1,3 +1,3 @@
1
1
  class Voucherify
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voucherify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawelrychlik
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-01 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,7 +81,9 @@ files:
81
81
  - bin/console
82
82
  - bin/setup
83
83
  - examples/customer.rb
84
+ - examples/utils.rb
84
85
  - lib/voucherify.rb
86
+ - lib/voucherify/utils.rb
85
87
  - lib/voucherify/version.rb
86
88
  - voucherify.gemspec
87
89
  homepage: https://github.com/rspective/voucherify-ruby-sdk/