shopscan 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.2.1)
5
+ rspec (2.13.0)
6
+ rspec-core (~> 2.13.0)
7
+ rspec-expectations (~> 2.13.0)
8
+ rspec-mocks (~> 2.13.0)
9
+ rspec-core (2.13.0)
10
+ rspec-expectations (2.13.0)
11
+ diff-lcs (>= 1.1.3, < 2.0)
12
+ rspec-mocks (2.13.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rob Esris
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ shopscan
2
+ ========
3
+
4
+ A very simple ruby API exercise.
5
+
6
+ install and run rspec tests
7
+ ===========================
8
+
9
+ git clone git://github.com/robesris/shopscan.git
10
+ cd shopscan
11
+ bundle install
12
+
13
+ \# set LIST_LENGTH to any non-negative number to test all permutations of that length (default is 1)
14
+ LIST_LENGTH=5 rspec -fd
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/shopscan.rb ADDED
@@ -0,0 +1,71 @@
1
+ module ShopScan
2
+
3
+ # storage and retrieval methods that should be implmented by the including class
4
+ def add_item(item)
5
+ raise "Please implement the add_item method to add a new product given a hash of the form: { 'A' => { :price => PRICE_IN_CENTS, :bulk_qty => BULK_QTY, :bulk_price => BULK_PRICE } }"
6
+ end
7
+
8
+ def product(product_name = nil)
9
+ raise "Please implement the product method, accepting a product name as a string."
10
+ end
11
+
12
+ def all_products
13
+ raise "Please implement the all_products method, returning a hash of all products."
14
+ end
15
+
16
+ def items
17
+ raise "Please implement the items method, to return an array of scanned item names."
18
+ end
19
+
20
+ def add_item_to_cart(item)
21
+ raise "Please implement the add_item_to_cart(item) method, accepting an item name."
22
+ end
23
+
24
+
25
+ # core methods
26
+ def set_pricing(price_list)
27
+ price_list.each_pair do |key, product|
28
+ self.add_item(key => product)
29
+ end
30
+ end
31
+
32
+ def price_list(product_name = nil)
33
+ if product_name
34
+ self.product(product_name)
35
+ else
36
+ self.all_products
37
+ end
38
+ end
39
+
40
+ def scan(item)
41
+ if all_products[item]
42
+ add_item_to_cart(item)
43
+ { :status => :success }
44
+ else
45
+ { :status => :failed, :message => "Unknown product skipped: '#{item}'" }
46
+ end
47
+ end
48
+
49
+ def total
50
+ item_counts = {}
51
+
52
+ self.items.each do |item|
53
+ item_counts[item] ||= 0
54
+ item_counts[item] += 1
55
+ end
56
+
57
+ total = 0
58
+ item_counts.each_pair do |key, item_count|
59
+ #raise @price_list.inspect
60
+ if item_info = self.price_list[key]
61
+ if (bulk_qty = item_info[:bulk_qty]) && bulk_price = item_info[:bulk_price]
62
+ total += item_count / bulk_qty * bulk_price
63
+ item_count %= bulk_qty
64
+ end
65
+ total += item_count * item_info[:price]
66
+ end
67
+ end
68
+
69
+ total
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Shopscan
2
+ VERSION = "0.0.1"
3
+ end
data/lib/terminal.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'shopscan'
2
+
3
+ class Terminal
4
+ include ShopScan
5
+
6
+ def add_item(item)
7
+ @price_list ||= {}
8
+ @price_list.merge! item
9
+ end
10
+
11
+ def product(product_name = nil)
12
+ return self.all_products unless product_name
13
+
14
+ @price_list[product_name.to_s.upcase]
15
+ end
16
+
17
+ def all_products
18
+ @price_list
19
+ end
20
+
21
+ def items
22
+ @items ||= []
23
+ end
24
+
25
+ def add_item_to_cart(item)
26
+ @items ||= []
27
+ @items << item
28
+ end
29
+ end
data/shopscan.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/shopscan/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Rob Esris"]
6
+ gem.email = ["robesris@gmail.com"]
7
+ gem.description = %q{A simple demo api for scanning/totaling products}
8
+ gem.summary = %q{A simple demo api for scanning/totaling products}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "shopscan"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Shopscan::VERSION
17
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+
@@ -0,0 +1,156 @@
1
+ require 'terminal'
2
+
3
+ # helper method
4
+ def default_prices
5
+ {'A' => { :price => 200, :bulk_qty => 4, :bulk_price => 700 },
6
+ 'B' => { :price => 1200 },
7
+ 'C' => { :price => 125, :bulk_qty => 6, :bulk_price => 600 },
8
+ 'D' => { :price => 15 }}
9
+ end
10
+
11
+
12
+ # basic functionality
13
+ describe Terminal do
14
+ it "allows us to set and retrieve pricing" do
15
+ terminal = Terminal.new
16
+ expect {terminal.set_pricing({'A' => { :price => 200, :bulk_qty => 4, :bulk_price => 700 },
17
+ 'B' => { :price => 1200 },
18
+ 'C' => { :price => 125, :bulk_qty => 6, :bulk_price => 600 },
19
+ 'D' => { :price => 15 }}) }.to_not raise_error
20
+ terminal.price_list['A'][:price].should == 200
21
+ terminal.price_list['A'][:bulk_qty].should == 4
22
+ terminal.price_list['B'][:price].should == 1200
23
+ terminal.price_list(:c)[:price].should == 125
24
+ terminal.price_list('C')[:bulk_price].should == 600
25
+ terminal.price_list('d')[:price].should == 15
26
+ end
27
+
28
+ it "scans items and stores them as an array of product names" do
29
+ terminal = Terminal.new
30
+ terminal.set_pricing(default_prices)
31
+
32
+ terminal.scan("A")
33
+ terminal.scan("A")
34
+ terminal.scan("C")
35
+ terminal.scan("B")
36
+ terminal.scan("D")
37
+
38
+ terminal.items.should == [ "A", "A", "C", "B", "D" ]
39
+ end
40
+
41
+ it "correctly totals the minimal cases" do
42
+ test_cases = [ { :list => "ABCDABAA", :expected => 3240 },
43
+ { :list => "CCCCCCC", :expected => 725 },
44
+ { :list => "ABCD", :expected => 1540 } ]
45
+
46
+ test_cases.each do |test_case|
47
+ terminal = Terminal.new
48
+ terminal.set_pricing(default_prices)
49
+
50
+ items = test_case[:list].split ""
51
+ items.each do |item|
52
+ terminal.scan(item)
53
+ end
54
+
55
+ terminal.total.should == test_case[:expected]
56
+ end
57
+ end
58
+
59
+ it "gracefully rejects unknown items with a warning" do
60
+ terminal = Terminal.new
61
+ terminal.set_pricing(default_prices)
62
+
63
+ terminal.scan('A')[:status].should == :success
64
+
65
+ # unknown product
66
+ result = terminal.scan('E')
67
+ result[:status].should == :failed
68
+ result[:message].should == "Unknown product skipped: 'E'"
69
+
70
+ # case sensitive
71
+ result = terminal.scan('a')
72
+ result[:status].should == :failed
73
+ result[:message].should == "Unknown product skipped: 'a'"
74
+
75
+ result = terminal.scan('B')
76
+ result[:status].should == :success
77
+ result[:message].should be_nil
78
+
79
+ result = terminal.scan('ABCD')
80
+ result[:status].should == :failed
81
+ result[:message].should == "Unknown product skipped: 'ABCD'"
82
+
83
+ terminal.items.should == [ 'A', 'B' ]
84
+ terminal.total.should == 1400
85
+ end
86
+
87
+ it "correctly totals all 'carts' of length #{ENV['LIST_LENGTH'] || 1}" do
88
+ list_length = ENV['LIST_LENGTH'] || 1
89
+
90
+ terminal = Terminal.new
91
+ terminal.set_pricing(default_prices)
92
+
93
+ item_names = terminal.price_list.map { |key, item| key }
94
+
95
+ list_length = list_length.to_i
96
+ if list_length >= 1
97
+ old_combos = [ { :list => "", :value => 0, 'A' => 0, 'B' => 0, 'C' => 0, 'D' => 0 } ]
98
+ 1.upto(list_length) do |length|
99
+ new_combos = []
100
+ puts "\nLENGTH #{length}:"
101
+ old_combos.each do |old_combo|
102
+ item_names.each do |item_name|
103
+ new_combo = { :list => old_combo[:list] + item_name,
104
+ :value => nil, # we're going to recalculate
105
+ 'A' => old_combo['A'],
106
+ 'B' => old_combo['B'],
107
+ 'C' => old_combo['C'],
108
+ 'D' => old_combo['D']
109
+ }
110
+ new_combo[item_name] += 1
111
+
112
+ combo_value = 0
113
+
114
+ terminal = Terminal.new
115
+ terminal.set_pricing(default_prices)
116
+ 'A'.upto('D') do |combo_item_name|
117
+ combo_item = terminal.price_list[combo_item_name]
118
+ bulk_qty = combo_item[:bulk_qty]
119
+ bulk_price = combo_item[:bulk_price]
120
+ remaining = new_combo[combo_item_name]
121
+
122
+ if bulk_qty && bulk_price
123
+ combo_value += remaining / bulk_qty * bulk_price
124
+ remaining %= bulk_qty
125
+ end
126
+ combo_value += remaining * combo_item[:price]
127
+ end
128
+ new_combo[:value] = combo_value
129
+
130
+ # now scan and check against expected values
131
+ items = new_combo[:list].split ""
132
+ items.each do |item|
133
+ terminal.scan(item)
134
+ end
135
+
136
+ # moment of truth...
137
+ grand_total = terminal.total
138
+ result_s = if grand_total == new_combo[:value]
139
+ "*** PASSED:"
140
+ else
141
+ "!!! FAILED:"
142
+ end
143
+
144
+ puts "#{result_s} Expected: #{new_combo[:value]}\tActual: #{grand_total}\tCart: #{new_combo[:list]}"
145
+
146
+ # the actual test
147
+ grand_total.should == new_combo[:value]
148
+
149
+ new_combos << new_combo
150
+ end
151
+ end
152
+ old_combos = new_combos
153
+ end
154
+ end
155
+ end
156
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shopscan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Esris
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple demo api for scanning/totaling products
15
+ email:
16
+ - robesris@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - lib/shopscan.rb
29
+ - lib/shopscan/version.rb
30
+ - lib/terminal.rb
31
+ - shopscan.gemspec
32
+ - spec/spec_helper.rb
33
+ - spec/terminal_spec.rb
34
+ homepage: ''
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.24
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A simple demo api for scanning/totaling products
58
+ test_files:
59
+ - spec/spec_helper.rb
60
+ - spec/terminal_spec.rb