unit4-checkout 0.1.0 → 0.1.3
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/Gemfile +4 -0
- data/Gemfile.lock +76 -0
- data/lib/unit4/checkout/basket.rb +7 -0
- data/lib/unit4/checkout/checkout.rb +74 -0
- data/lib/unit4/checkout/version.rb +1 -1
- data/lib/unit4/checkout.rb +2 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83982e7ddae1c09934ca1125ae9cfb0b0b8b845638fbce0eb19472137057652b
|
4
|
+
data.tar.gz: fb8982c906834e642dcaedbd81cdb89f258555c1dcbab7f73dc1387a9cd609fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 514d5a25a55f090eb87ebcd5d49ce013b7638efa885e79dd59270c361bcb0462f929140706413d04624a5bfdfdad757f07648dc8403f188d72b07fe36b48bee3
|
7
|
+
data.tar.gz: 1721ec7136a1c447903ffbfdbad3ce2c7d0fea7ff11c19eed5ca57a5adfcdd3895b16d930d3a34a910dfd2a21ccf5dc08cf7d200f2945872ceb80502fa74b4da
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
unit4-checkout (0.1.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (7.0.3.1)
|
10
|
+
activesupport (= 7.0.3.1)
|
11
|
+
activerecord (7.0.3.1)
|
12
|
+
activemodel (= 7.0.3.1)
|
13
|
+
activesupport (= 7.0.3.1)
|
14
|
+
activesupport (7.0.3.1)
|
15
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
|
+
i18n (>= 1.6, < 2)
|
17
|
+
minitest (>= 5.1)
|
18
|
+
tzinfo (~> 2.0)
|
19
|
+
ast (2.4.2)
|
20
|
+
cgi (0.3.2)
|
21
|
+
concurrent-ruby (1.1.10)
|
22
|
+
diff-lcs (1.5.0)
|
23
|
+
erb (2.2.3)
|
24
|
+
cgi
|
25
|
+
i18n (1.12.0)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
minitest (5.16.2)
|
28
|
+
parallel (1.21.0)
|
29
|
+
parser (3.1.0.0)
|
30
|
+
ast (~> 2.4.1)
|
31
|
+
rainbow (3.1.1)
|
32
|
+
rake (13.0.6)
|
33
|
+
regexp_parser (2.5.0)
|
34
|
+
rexml (3.2.5)
|
35
|
+
rspec (3.11.0)
|
36
|
+
rspec-core (~> 3.11.0)
|
37
|
+
rspec-expectations (~> 3.11.0)
|
38
|
+
rspec-mocks (~> 3.11.0)
|
39
|
+
rspec-core (3.11.0)
|
40
|
+
rspec-support (~> 3.11.0)
|
41
|
+
rspec-expectations (3.11.0)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.11.0)
|
44
|
+
rspec-mocks (3.11.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.11.0)
|
47
|
+
rspec-support (3.11.0)
|
48
|
+
rubocop (1.25.1)
|
49
|
+
parallel (~> 1.10)
|
50
|
+
parser (>= 3.1.0.0)
|
51
|
+
rainbow (>= 2.2.2, < 4.0)
|
52
|
+
regexp_parser (>= 1.8, < 3.0)
|
53
|
+
rexml
|
54
|
+
rubocop-ast (>= 1.15.1, < 2.0)
|
55
|
+
ruby-progressbar (~> 1.7)
|
56
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
57
|
+
rubocop-ast (1.15.2)
|
58
|
+
parser (>= 3.0.1.1)
|
59
|
+
ruby-progressbar (1.11.0)
|
60
|
+
tzinfo (2.0.5)
|
61
|
+
concurrent-ruby (~> 1.0)
|
62
|
+
unicode-display_width (2.1.0)
|
63
|
+
|
64
|
+
PLATFORMS
|
65
|
+
x86_64-linux
|
66
|
+
|
67
|
+
DEPENDENCIES
|
68
|
+
activerecord
|
69
|
+
erb
|
70
|
+
rake (~> 13.0)
|
71
|
+
rspec (~> 3.0)
|
72
|
+
rubocop (~> 1.21)
|
73
|
+
unit4-checkout!
|
74
|
+
|
75
|
+
BUNDLED WITH
|
76
|
+
2.3.15
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
require "erb"
|
5
|
+
|
6
|
+
class Checkout
|
7
|
+
attr_reader :promotional_rules, :total, :basket, :price_discount_applied_flag, :result
|
8
|
+
|
9
|
+
# {product_discounts: {001: {count:2, price: 3.25}, 004: {count:2, price: 3.25}}, total_price_discount: $50}
|
10
|
+
def initialize(promotional_rules)
|
11
|
+
# maybe raise another error if keys aren't ids or total price, possibly total_item_count
|
12
|
+
raise TypeError, "expected a Hash, got #{promotional_rules.class.name}" unless promotional_rules.is_a? Hash
|
13
|
+
|
14
|
+
@promotional_rules = promotional_rules
|
15
|
+
@total = 0
|
16
|
+
@basket = Basket.new
|
17
|
+
establish_connection
|
18
|
+
end
|
19
|
+
|
20
|
+
# maybe facilitate scanning multiple items at once
|
21
|
+
# no structure for item given, assumed id
|
22
|
+
def scan(item)
|
23
|
+
# check for item id
|
24
|
+
add_to_basket(item)
|
25
|
+
calculate_total(item)
|
26
|
+
puts "#{item.capitalize} has been added to the basket successfully!"
|
27
|
+
end
|
28
|
+
|
29
|
+
def calculate_total(item)
|
30
|
+
find_item_price(item)
|
31
|
+
item_price = 3
|
32
|
+
new_discount_available?(item) ? apply_discounts : @total += item_price
|
33
|
+
@total
|
34
|
+
end
|
35
|
+
|
36
|
+
def find_item_price(item)
|
37
|
+
query = "SELECT * FROM 'users' WHERE 'users'.'id' = ?"
|
38
|
+
sanitized_query = ActiveRecord::Base.sanitize_sql_array([query, item])
|
39
|
+
execute_statement(sanitized_query)
|
40
|
+
end
|
41
|
+
|
42
|
+
def apply_discounts; end
|
43
|
+
|
44
|
+
def new_discount_available?(item); end
|
45
|
+
|
46
|
+
def execute_statement(sql)
|
47
|
+
results = ActiveRecord::Base.connection.exec_query(sql)
|
48
|
+
@result = results if results.present?
|
49
|
+
end
|
50
|
+
|
51
|
+
def establish_connection
|
52
|
+
db_config = setup_db_config
|
53
|
+
ActiveRecord::Base.establish_connection(adapter: db_config["adapter"], database: db_config["database"])
|
54
|
+
end
|
55
|
+
|
56
|
+
def setup_db_config
|
57
|
+
defined?(Rails) && defined?(Rails.env) ? rails_db_config : non_rails_db_config
|
58
|
+
end
|
59
|
+
|
60
|
+
def rails_db_config
|
61
|
+
Rails.application.config.database_configuration[Rails.env]
|
62
|
+
end
|
63
|
+
|
64
|
+
def non_rails_db_config
|
65
|
+
# TODO: use current database instead of development
|
66
|
+
YAML.safe_load(ERB.new(File.read("./config/database.yml")).result, aliases: true)["development"]
|
67
|
+
end
|
68
|
+
|
69
|
+
# maybe add remove item function
|
70
|
+
|
71
|
+
def add_to_basket(item)
|
72
|
+
@basket.items[item] ? @basket.items[item] += 1 : @basket.items[item] = 1
|
73
|
+
end
|
74
|
+
end
|
data/lib/unit4/checkout.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unit4-checkout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Boyan Georgiev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07-
|
11
|
+
date: 2022-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Initial commit
|
14
14
|
email:
|
@@ -20,9 +20,12 @@ files:
|
|
20
20
|
- ".rspec"
|
21
21
|
- ".rubocop.yml"
|
22
22
|
- Gemfile
|
23
|
+
- Gemfile.lock
|
23
24
|
- README.md
|
24
25
|
- Rakefile
|
25
26
|
- lib/unit4/checkout.rb
|
27
|
+
- lib/unit4/checkout/basket.rb
|
28
|
+
- lib/unit4/checkout/checkout.rb
|
26
29
|
- lib/unit4/checkout/version.rb
|
27
30
|
- sig/unit4/checkout.rbs
|
28
31
|
homepage: https://github.com/BoyanGeorgiev96/unit4-checkout
|