-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce Product Resource #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
imRohan
wants to merge
1
commit into
main
Choose a base branch
from
rl-introduce-products
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class Allergen < Bls::Core::ObjectV2; end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class DietaryTag < Bls::Core::ObjectV2; end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class Ingredient < Bls::Core::ObjectV2; end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class NutritionFact < Bls::Core::ObjectV2; end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class ObjectV2 < Bls::Core::Object | ||
|
imRohan marked this conversation as resolved.
|
||
| def self.build_from_array(array) | ||
| array.map { |object| build(object) } | ||
| end | ||
|
|
||
| def resources_path | ||
| "#{api.base_url}/api/#{object_name.downcase}" | ||
| end | ||
|
|
||
| def resource_path | ||
| "#{api.base_url}/api/#{object_name.downcase}/#{id}" | ||
| end | ||
|
|
||
| def api | ||
| @api ||= Bls::Core::V2.build | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class Product < Bls::Core::ObjectV2 | ||
| def object_name | ||
| "products" | ||
| end | ||
|
|
||
| def self.build_from_response(data) | ||
| factory = ProductFactory.new(data) | ||
| factory.create | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bls | ||
| module Core | ||
| class ProductFactory | ||
| attr_accessor :response | ||
|
|
||
| def initialize(response) | ||
| @response = response | ||
| end | ||
|
|
||
| def create | ||
| product = Product.build(response) | ||
| product.ingredients = ingredients | ||
| product.allergens = allergens | ||
| product.dietary_tags = dietary_tags | ||
| product.nutrition_facts = nutrition_facts | ||
| product | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def ingredients | ||
| Ingredient.build_from_array(response[:ingredients]) | ||
| end | ||
|
|
||
| def allergens | ||
| Allergen.build_from_array(response[:allergens]) | ||
| end | ||
|
|
||
| def dietary_tags | ||
| DietaryTag.build_from_array(response[:dietary_tags]) | ||
| end | ||
|
|
||
| def nutrition_facts | ||
| as_packaged = NutritionFact. | ||
| build_from_array(response[:nutrition_facts][:as_packaged]) | ||
| as_cooked = NutritionFact. | ||
| build_from_array(response[:nutrition_facts]. | ||
| fetch(:as_cooked, [])) | ||
| { as_packaged: as_packaged, as_cooked: as_cooked } | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Bls::Core::ProductFactory do | ||
| describe "#create" do | ||
| it "assigns the ingredients to the Product" do | ||
| response = build_product_response | ||
| ingredient = Bls::Core::Ingredient.build(response[:ingredients]) | ||
| factory = Bls::Core::ProductFactory.new(response) | ||
|
|
||
| result = factory.create | ||
|
|
||
| expect(result.ingredients).to match_array(ingredient) | ||
| end | ||
|
|
||
| it "assigns the allergens to the Product" do | ||
| response = build_product_response | ||
| allergen = Bls::Core::Allergen.build_from_array(response[:allergens]) | ||
| factory = Bls::Core::ProductFactory.new(response) | ||
|
|
||
| result = factory.create | ||
|
|
||
| expect(result.allergens).to match_array(allergen) | ||
| end | ||
|
|
||
| it "assigns the dietary tags to the Product" do | ||
| response = build_product_response | ||
| dietary_tags = Bls::Core::Allergen. | ||
| build_from_array(response[:dietary_tags]) | ||
| factory = Bls::Core::ProductFactory.new(response) | ||
|
|
||
| result = factory.create | ||
|
|
||
| expect(result.dietary_tags).to match_array(dietary_tags) | ||
| end | ||
|
|
||
| it "assigns the nutrition facts to the Product" do | ||
| response = build_product_response | ||
| factory = Bls::Core::ProductFactory.new(response) | ||
| product = factory.create | ||
|
|
||
| result = product.nutrition_facts | ||
|
|
||
| expect(result.keys).to match_array([:as_packaged, :as_cooked]) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Bls::Core::Product do | ||
| describe ".retrieve" do | ||
| it "returns the product" do | ||
| product_id = "MP0527" | ||
| body = build_product_response(product_id: product_id) | ||
| response = build_response(body: body) | ||
| stub_api_v2_authentication | ||
| stub_api_response response: response | ||
|
|
||
| result = Bls::Core::Product.retrieve(product_id) | ||
|
|
||
| expect(result.code).to eq(product_id) | ||
| end | ||
| end | ||
|
|
||
| describe ".build_from_response" do | ||
| it "creates a product using the product factory" do | ||
| response = build_product_response | ||
| factory = Bls::Core::ProductFactory.new(response) | ||
| allow(Bls::Core::ProductFactory).to receive(:new).and_return(factory) | ||
| allow(factory).to receive(:create) | ||
|
|
||
| Bls::Core::Product.build_from_response(response) | ||
|
|
||
| expect(factory).to have_received(:create).once | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "code": "string", | ||
| "name": "string", | ||
| "status": "DRAFT", | ||
| "refrigeration_type": "NONE", | ||
| "refrigeration_level": "STANDARD", | ||
| "unit_volume": { | ||
| "value": 0.1 | ||
| }, | ||
| "unit_slots": { | ||
| "value": 0.1 | ||
| }, | ||
| "ingredients": [ | ||
| { | ||
| "code": "string", | ||
| "name": "string", | ||
| "quantity": 0.1, | ||
| "unit": "string" | ||
| } | ||
| ], | ||
| "nutrition_facts": { | ||
| "as_packaged": [ | ||
| { | ||
| "code": "CALORIES", | ||
| "amount": 0, | ||
| "unit": "string" | ||
| } | ||
| ], | ||
| "as_cooked": [ | ||
| { | ||
| "code": "CALORIES", | ||
| "amount": 0, | ||
| "unit": "string" | ||
| } | ||
| ] | ||
| }, | ||
| "allergens": [ | ||
| { | ||
| "code": "MILK", | ||
| "source": "NONE" | ||
| } | ||
| ], | ||
| "dietary_tags": [ | ||
| "PESCATARIAN" | ||
| ] | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DietaryTag is kind of confusing naming... what is this and what is it used for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the naming which is returned by the core api, and is an array of diets which the meal abides by ex:
["PESCATARIAN"].If the naming is not clear, perhaps the api should not return this key, and I can adjust this PR accordingly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it sounds like this needs to be thought about at the API level. make a note for you to revisit this with them. i think it's worth you reviewing naming across the entire product resource to comment on what might still sound sunbasket specific in the response