simple_marketplace 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 584c4c8da0fb70ded67448e246ada28e30317e0e
4
+ data.tar.gz: efa875669befe8ca9df938129540b6daed179ff0
5
+ SHA512:
6
+ metadata.gz: 1576db61c3a19fc15930ac31602c0283f6fecfc93d070b78f6a915c367b3159dd0c06ab50e8d948c5cc1ae7bf333a70cbbfef651d64925d1a01cb02788bc0a7e
7
+ data.tar.gz: '08fc80ae3a2551d10a9bc621710ebcf0b0f23513dcd51db9140408e266a656cad61d665103fe6e1ca044ecea54915c3d580b985dbcb726cffaa1fd073cc97398'
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # sublime files
15
+ *.sublime-*
16
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ sm
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simple_marketplace.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Pedro Benevides
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,71 @@
1
+ # SimpleMarketplace
2
+
3
+ Ruby gem that operates simple checkout cart operations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_marketplace'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_marketplace
20
+
21
+ ## Usage
22
+
23
+ Create some products
24
+
25
+ ```ruby
26
+ # Let's create some products here...
27
+ SimpleMarketplace::Product.create(
28
+ { code: "001", name: "Lavender Heart", price: BigDecimal.new("9.25") },
29
+ { code: "002", name: "Personalised cufflinks", price: BigDecimal.new("45", 4) },
30
+ { code: "003", name: "Kids T-shirt", price: BigDecimal.new("19.95", 4) })
31
+ ```
32
+
33
+ Create a checkout basket and add items to it
34
+
35
+ ```ruby
36
+ co = SimpleMarketplace::Checkout.new
37
+
38
+ co.scan("001")
39
+ co.scan("002")
40
+ co.scan("001")
41
+ ```
42
+
43
+ Check the total
44
+
45
+ ```ruby
46
+ co.pretty_total
47
+ => £36.95
48
+ ```
49
+
50
+ Customize promotions
51
+
52
+ ```ruby
53
+
54
+ promotion = SimpleMarketplace::Offer::ProductDiscountByAmount.new('001', 2, BigDecimal.new("8.50"))
55
+ co = SimpleMarketplace::Checkout.new(promotion)
56
+ ```
57
+
58
+ ## Development
59
+
60
+ - `bin/setup` to install dependencies;
61
+ - `rake spec` to run the tests;
62
+ - `bin/console` for an interactive prompt that will allow you to experiment.
63
+ - `ruby examples/usage` to see this gem in action (check the source).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pedroaugustosb/simple_marketplace.
68
+
69
+ ## License
70
+
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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_marketplace"
5
+
6
+ require "pry"
7
+ Pry.start
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
data/examples/usage.rb ADDED
@@ -0,0 +1,52 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require "simple_marketplace"
3
+
4
+ # Let's create some products here...
5
+ SimpleMarketplace::Product.create(
6
+ { code: "001", name: "Lavender Heart", price: BigDecimal.new("9.25") },
7
+ { code: "002", name: "Personalised cufflinks", price: BigDecimal.new("45", 4) },
8
+ { code: "003", name: "Kids T-shirt", price: BigDecimal.new("19.95", 4) })
9
+
10
+
11
+ # Offering a discount to a specifc product
12
+ offer1 = SimpleMarketplace::Offer::ProductDiscountByAmount.new('001', 2, BigDecimal.new(8.5, 4))
13
+
14
+ # Offering discount in the entire order
15
+ offer2 = SimpleMarketplace::Offer::TotalPercentageDiscountByMinValue.new(BigDecimal.new(60.01, 4), 10)
16
+
17
+ # Adding the discounts to this checkout
18
+ co = SimpleMarketplace::Checkout.new(offer1, offer2)
19
+
20
+ # Adding products
21
+ co.scan("001")
22
+ co.scan("002")
23
+ co.scan("003")
24
+
25
+ # reading the total
26
+ puts "Basket 001,002,003"
27
+ puts co.pretty_total
28
+
29
+ # empty basket
30
+ co.clear_items
31
+
32
+ # Adding products
33
+ co.scan("001")
34
+ co.scan("003")
35
+ co.scan("001")
36
+
37
+ # reading the total
38
+ puts "Basket 001,003,001"
39
+ puts co.pretty_total
40
+
41
+ # empty basket
42
+ co.clear_items
43
+
44
+ # Adding products
45
+ co.scan("001")
46
+ co.scan("002")
47
+ co.scan("001")
48
+ co.scan("003")
49
+
50
+ # reading the total
51
+ puts "Basket 001,002,001,003"
52
+ puts co.pretty_total
@@ -0,0 +1,13 @@
1
+ require "simple_marketplace/version"
2
+ require "bigdecimal"
3
+
4
+ module SimpleMarketplace
5
+ autoload :Product, 'simple_marketplace/product'
6
+ autoload :Offerable, 'simple_marketplace/offerable'
7
+ autoload :Checkout, 'simple_marketplace/checkout'
8
+
9
+ module Offer
10
+ autoload :ProductDiscountByAmount, 'simple_marketplace/offer/product_discount_by_amount'
11
+ autoload :TotalPercentageDiscountByMinValue, 'simple_marketplace/offer/total_percentage_discount_by_min_value'
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ module SimpleMarketplace
2
+ class Checkout
3
+
4
+ def initialize(*promotional_rules)
5
+ @promotional_rules = promotional_rules
6
+ @items = []
7
+ end
8
+
9
+ # Adds an item to the cart
10
+ def scan(product_code)
11
+ if (product = Product.get_by_code(product_code))
12
+ @items << product
13
+ end
14
+ end
15
+
16
+ # Calculates the total with promotions applied
17
+ def total
18
+ @total = @items.map(&:price).reduce(:+)
19
+ apply_promotional_rules
20
+ @total
21
+ end
22
+
23
+ # Shows the formated total
24
+ def pretty_total
25
+ "£#{'%.2f' % total}"
26
+ end
27
+
28
+ # Restart the cart
29
+ def clear_items
30
+ @items = []
31
+ end
32
+
33
+ private
34
+
35
+ # calls the promotional rules in order
36
+ def apply_promotional_rules
37
+ @promotional_rules.each do |rule|
38
+ @total = rule.call(@items, @total)
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ module SimpleMarketplace::Offer
2
+ class ProductDiscountByAmount
3
+ include ::SimpleMarketplace::Offerable
4
+
5
+ def initialize(product_code, initial_amount, new_value)
6
+ @product_code, @initial_amount, @new_value = product_code, initial_amount, new_value
7
+ end
8
+
9
+ # Verify if the product amount is the desired to change the product value
10
+ def call(items, total)
11
+ if items.count{ |product| product.code == @product_code } >= @initial_amount
12
+ items.collect{|product| product.code == @product_code ? @new_value : product.price }.reduce(:+)
13
+ else
14
+ total
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module SimpleMarketplace::Offer
2
+ class TotalPercentageDiscountByMinValue
3
+ include ::SimpleMarketplace::Offerable
4
+
5
+ def initialize(min_value, percentage)
6
+ @min_value, @percentage = min_value, percentage
7
+ end
8
+
9
+ # Applies the percentage discount if total is greater than min_value
10
+ def call(items, total)
11
+ total >= @min_value ? total.mult(BigDecimal.new(100).sub(@percentage, 4), 4).div(BigDecimal.new(100), 4) : total
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module SimpleMarketplace::Offerable
2
+
3
+ # Every new offer/promotion will have to implement this method
4
+ # in order to execute its promotional rules.
5
+ def call(items, total)
6
+ raise "Must implement"
7
+ end
8
+ end
@@ -0,0 +1,34 @@
1
+ module SimpleMarketplace
2
+ class Product
3
+ attr_reader :code, :name, :price
4
+
5
+ @@all = Hash.new
6
+
7
+ def initialize(code, name, price)
8
+ @code, @name, @price = code, name, price
9
+ end
10
+
11
+ # Fills the products database
12
+ def self.create(*args)
13
+ args.each do |hash|
14
+ @@all[hash[:code]] = Product.new(hash[:code], hash[:name], hash[:price])
15
+ end
16
+ end
17
+
18
+ # returns product with the code
19
+ def self.get_by_code(code)
20
+ @@all[code]
21
+ end
22
+
23
+ # pretty print
24
+ def to_s
25
+ "Product(code: #{@code}, name: #{@name}, price: #{@price})"
26
+ end
27
+
28
+ # checks equality
29
+ def ==(other)
30
+ return code == other.code, name == other.name, price: other.price
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleMarketplace
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "simple_marketplace/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_marketplace"
8
+ spec.version = SimpleMarketplace::VERSION
9
+ spec.authors = ["Pedro Benevides"]
10
+ spec.email = ["pedro.augusto.sb@gmail.com"]
11
+
12
+ spec.summary = %q{Library to handle basic checkout cart operations.}
13
+ spec.homepage = "https://github.com/pedroaugustosb/simple_marketplace"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ 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", "~> 1.15"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "pry-byebug"
28
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_marketplace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Benevides
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-04 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: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: pry-byebug
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
+ description:
84
+ email:
85
+ - pedro.augusto.sb@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-gemset"
93
+ - ".ruby-version"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - examples/usage.rb
102
+ - lib/simple_marketplace.rb
103
+ - lib/simple_marketplace/checkout.rb
104
+ - lib/simple_marketplace/offer/product_discount_by_amount.rb
105
+ - lib/simple_marketplace/offer/total_percentage_discount_by_min_value.rb
106
+ - lib/simple_marketplace/offerable.rb
107
+ - lib/simple_marketplace/product.rb
108
+ - lib/simple_marketplace/version.rb
109
+ - simple_marketplace.gemspec
110
+ homepage: https://github.com/pedroaugustosb/simple_marketplace
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.11
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Library to handle basic checkout cart operations.
134
+ test_files: []