Skip to content

Commit 87b0b7e

Browse files
committed
WIP pr comments
1 parent 2943511 commit 87b0b7e

7 files changed

Lines changed: 106 additions & 27 deletions

File tree

‎lib/bls/core.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
require_relative "core/resources/api_error"
1818
require_relative "core/authorization/bearer"
1919
require_relative "core/resources/object"
20-
require_relative "core/resources/objectv2"
20+
require_relative "core/resources/object_v2"
2121
require_relative "core/resources/allergen"
2222
require_relative "core/resources/dietary_tag"
2323
require_relative "core/resources/ingredient"
@@ -30,6 +30,7 @@
3030
require_relative "core/resources/menu_item"
3131
require_relative "core/serializers/menu_item_serializer"
3232
require_relative "core/resources/product"
33+
require_relative "core/resources/product_factory"
3334
require_relative "core/resources/recipient"
3435
require_relative "core/serializers/recipient_serializer"
3536
require_relative "core/version"

‎lib/bls/core/resources/product.rb‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,13 @@
33
module Bls
44
module Core
55
class Product < Bls::Core::ObjectV2
6-
def initialize(args = {})
7-
super
8-
end
9-
106
def object_name
117
"products"
128
end
139

1410
def self.build_from_response(data)
15-
product = build(data)
16-
product.ingredients = Ingredient.build_from_array(data[:ingredients])
17-
as_packaged = NutritionFact.
18-
build_from_array(data[:nutrition_facts][:as_packaged])
19-
as_cooked = NutritionFact.
20-
build_from_array(data[:nutrition_facts].
21-
fetch(:as_cooked, []))
22-
product.allergens = Allergen.build_from_array(data[:allergens])
23-
product.dietary_tags = DietaryTag.build_from_array(data[:dietary_tags])
24-
product.nutrition_facts = { as_packaged: as_packaged,
25-
as_cooked: as_cooked }
26-
product
11+
factory = ProductFactory.new(data)
12+
factory.create
2713
end
2814
end
2915
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module Bls
4+
module Core
5+
class ProductFactory
6+
attr_accessor :response
7+
8+
def initialize(response)
9+
@response = response
10+
end
11+
12+
def create
13+
product = Product.build(response)
14+
product.ingredients = ingredients
15+
product.allergens = allergens
16+
product.dietary_tags = dietary_tags
17+
product.nutrition_facts = nutrition_facts
18+
product
19+
end
20+
21+
protected
22+
23+
def ingredients
24+
Ingredient.build_from_array(response[:ingredients])
25+
end
26+
27+
def allergens
28+
Allergen.build_from_array(response[:allergens])
29+
end
30+
31+
def dietary_tags
32+
DietaryTag.build_from_array(response[:dietary_tags])
33+
end
34+
35+
def nutrition_facts
36+
as_packaged = NutritionFact.
37+
build_from_array(response[:nutrition_facts][:as_packaged])
38+
as_cooked = NutritionFact.
39+
build_from_array(response[:nutrition_facts].
40+
fetch(:as_cooked, []))
41+
{ as_packaged: as_packaged, as_cooked: as_cooked }
42+
end
43+
end
44+
end
45+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Bls::Core::ProductFactory do
4+
describe "#create" do
5+
it "assigns the ingredients to the Product" do
6+
response = build_product_response
7+
ingredient = Bls::Core::Ingredient.build(response[:ingredients])
8+
factory = Bls::Core::ProductFactory.new(response)
9+
10+
result = factory.create
11+
12+
expect(result.ingredients).to match_array(ingredient)
13+
end
14+
15+
it "assigns the allergens to the Product" do
16+
response = build_product_response
17+
allergen = Bls::Core::Allergen.build_from_array(response[:allergens])
18+
factory = Bls::Core::ProductFactory.new(response)
19+
20+
result = factory.create
21+
22+
expect(result.allergens).to match_array(allergen)
23+
end
24+
25+
it "assigns the dietary tags to the Product" do
26+
response = build_product_response
27+
dietary_tags = Bls::Core::Allergen.
28+
build_from_array(response[:dietary_tags])
29+
factory = Bls::Core::ProductFactory.new(response)
30+
31+
result = factory.create
32+
33+
expect(result.dietary_tags).to match_array(dietary_tags)
34+
end
35+
36+
it "assigns the nutrition facts to the Product" do
37+
response = build_product_response
38+
factory = Bls::Core::ProductFactory.new(response)
39+
product = factory.create
40+
41+
result = product.nutrition_facts
42+
43+
expect(result.keys).to match_array([:as_packaged, :as_cooked])
44+
end
45+
end
46+
end

‎spec/core/resources/product_spec.rb‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313

1414
expect(result.code).to eq(product_id)
1515
end
16+
end
1617

17-
it "returns packaged and cooked nutrition facts" do
18-
body = build_product_response
19-
response = build_response(body: body)
20-
stub_api_v2_authentication
21-
stub_api_response response: response
18+
describe ".build_from_response" do
19+
it "creates a product using the product factory" do
20+
response = build_product_response
21+
factory = Bls::Core::ProductFactory.new(response)
22+
allow(Bls::Core::ProductFactory).to receive(:new).and_return(factory)
23+
allow(factory).to receive(:create)
2224

23-
result = Bls::Core::Product.retrieve("TEST123")
25+
Bls::Core::Product.build_from_response(response)
2426

25-
expect(result.nutrition_facts.keys).to match_array(
26-
[:as_packaged, :as_cooked]
27-
)
27+
expect(factory).to have_received(:create).once
2828
end
2929
end
3030
end

‎spec/support/fixtures/product_response.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@
4343
"dietary_tags": [
4444
"PESCATARIAN"
4545
]
46-
}
46+
}
47+

0 commit comments

Comments
 (0)