-
Notifications
You must be signed in to change notification settings - Fork 12
feat: [HL-80] Meal details by id API 🍔 #99
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
base: develop
Are you sure you want to change the base?
Changes from all commits
5461fba
128741a
4cda54d
bf3628f
b204e8d
88f2872
322edcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // MARK: - Meal Details | ||
| struct MealDetails { | ||
| let id, name, drinkAlternate, category, area, instructions, thumbnail, tags: String | ||
| var ingredients: [String] = [] | ||
| var measures: [String] = [] | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,12 @@ | ||||||||||||||||||||||||||||||||||||||||||
| struct DynamicCodingKeys: CodingKey { | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding it, I like the idea 🙏🏼 |
||||||||||||||||||||||||||||||||||||||||||
| var intValue: Int? | ||||||||||||||||||||||||||||||||||||||||||
| init?(intValue: Int) { | ||||||||||||||||||||||||||||||||||||||||||
| self.intValue = intValue | ||||||||||||||||||||||||||||||||||||||||||
| self.stringValue = "\(intValue)" | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| var stringValue: String | ||||||||||||||||||||||||||||||||||||||||||
| init(stringValue: String) { | ||||||||||||||||||||||||||||||||||||||||||
| self.stringValue = stringValue | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To avoid duplication, you might consider making it generic, or at least have two versions for both Int and String. |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import Foundation | ||
|
|
||
| // MARK: - MealDetailsRequest | ||
| public struct MealDetailsRequest: RequestType { | ||
| public typealias ResponseType = MealDetailsResponse | ||
|
|
||
| private let id: String | ||
|
|
||
| public init(_ id: String) { | ||
| self.id = id | ||
| } | ||
|
|
||
| public var baseUrl: URL { Constants.theMealDB } | ||
| public var path: String { "lookup.php" } | ||
| public var method: String { "GET" } | ||
| public var queryParameters: [String: String] { [ "i": id ] } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||
| // MARK: - MealDetailsResponse | ||||||||||
| public struct MealDetailsResponse: Decodable { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No need to make this public, just return a list of meals. |
||||||||||
| let meals: [MealDetails] | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // MARK: - Meal Details | ||||||||||
| public struct MealDetails: Decodable { | ||||||||||
| let id, name, drinkAlternate, category, area, instructions, thumbnail, tags: String? | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Id should never be optional |
||||||||||
|
|
||||||||||
| var ingredients: [String] = [] | ||||||||||
| var measures: [String] = [] | ||||||||||
|
Comment on lines
+10
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| enum CodingKeys: String, CodingKey { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| case id = "idMeal" | ||||||||||
| case name = "strMeal" | ||||||||||
| case drinkAlternate = "strDrinkAlternate" | ||||||||||
| case category = "strCategory" | ||||||||||
| case area = "strArea" | ||||||||||
| case instructions = "strInstructions" | ||||||||||
| case thumbnail = "strMealThumb" | ||||||||||
| case tags = "strTags" | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public init(from decoder: Decoder) throws { | ||||||||||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||||||||||
|
|
||||||||||
| id = try container.decode(String.self, forKey: .id) | ||||||||||
| name = try container.decode(String.self, forKey: .name) | ||||||||||
| drinkAlternate = try container.decodeIfPresent(String.self, forKey: .drinkAlternate) | ||||||||||
| category = try container.decode(String.self, forKey: .category) | ||||||||||
| area = try container.decode(String.self, forKey: .area) | ||||||||||
| instructions = try container.decode(String.self, forKey: .instructions) | ||||||||||
| thumbnail = try container.decode(String.self, forKey: .thumbnail) | ||||||||||
| tags = try container.decode(String.self, forKey: .tags) | ||||||||||
|
|
||||||||||
| ingredients = try dynamicValuesFor(dynamicKey: "strIngredient", with: decoder) | ||||||||||
| .compactMap { $0.isEmpty ? nil : $0} | ||||||||||
| measures = try dynamicValuesFor(dynamicKey: "strMeasure", with: decoder) | ||||||||||
| .compactMap { $0.isEmpty ? nil : $0} | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private func dynamicValuesFor(dynamicKey: String, with decoder: Decoder) throws -> [String] { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this. A good practice when adding a code that the others in the might might not be aware of is to write comments. Documentation helps your future self understand why ou added this in the first place. |
||||||||||
| let dynamicContainer = try decoder.container(keyedBy: DynamicCodingKeys.self) | ||||||||||
| var dictionary: [String: String] = [:] | ||||||||||
|
|
||||||||||
| dynamicContainer.allKeys.forEach { key in | ||||||||||
| if key.stringValue.hasPrefix(dynamicKey), | ||||||||||
| let value = try? dynamicContainer.decode(String.self, forKey: key) { | ||||||||||
| dictionary[key.stringValue] = value | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return dictionary | ||||||||||
| .sorted { $0.key < $1.key } | ||||||||||
| .compactMap { $0.value } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import XCTest | ||
| @testable import Networking | ||
|
|
||
| final class MealDetailsRequestTests: XCTestCase { | ||
| var sut: MealDetailsRequest! | ||
|
|
||
| let stub = """ | ||
| { | ||
| "meals": [ | ||
| { | ||
| "idMeal": "52772", | ||
| "strMeal": "Teriyaki Chicken Casserole", | ||
| "strDrinkAlternate": null, | ||
| "strCategory": "Chicken", | ||
| "strArea": "Japanese", | ||
| "strInstructions": "Preheat oven to 350° F.", | ||
| "strMealThumb": "https://www.themealdb.com/images/media/meals/wvpsxx1468256321.jpg", | ||
| "strTags": "Meat,Casserole", | ||
| "strIngredient1": "soy sauce", | ||
| "strIngredient2": "water", | ||
| "strIngredient3": "brown sugar", | ||
| "strIngredient4": "ground ginger", | ||
| "strIngredient5": "minced garlic", | ||
| "strIngredient6": "cornstarch", | ||
| "strIngredient7": "chicken breasts", | ||
| "strIngredient8": "stir-fry vegetables", | ||
| "strIngredient9": "brown rice", | ||
| "strMeasure1": "3/4 cup", | ||
| "strMeasure2": "1/2 cup", | ||
| "strMeasure3": "1/4 cup", | ||
| "strMeasure4": "1/2 teaspoon", | ||
| "strMeasure5": "1/2 teaspoon", | ||
| "strMeasure6": "4 Tablespoons", | ||
| "strMeasure7": "2", | ||
| "strMeasure8": "1 (12 oz.)", | ||
| "strMeasure9": "3 cups", | ||
| } | ||
| ] | ||
| } | ||
| """ | ||
|
|
||
| // MARK: - Lifecycle | ||
|
|
||
| override func setUp() { | ||
| sut = MealDetailsRequest("52772") | ||
| } | ||
|
|
||
| // MARK: - Tests | ||
|
|
||
| func testMealDetailsRequestProperties() { | ||
| // Then | ||
| XCTAssertEqual(sut.baseUrl, Constants.theMealDB) | ||
| XCTAssertEqual(sut.path, "lookup.php") | ||
| XCTAssertEqual(sut.method, "GET") | ||
| } | ||
|
|
||
| func testMealDetailsResponseDecoder() throws { | ||
| // Given | ||
| let mealDetailsRequest = MealDetailsRequest("52772") | ||
|
|
||
| // When | ||
| let mealDetailsResponseData = try XCTUnwrap(stub.data(using: .utf8)) | ||
| let mealDetailsResponse = try? mealDetailsRequest.responseDecoder(mealDetailsResponseData) | ||
|
|
||
| // Then | ||
| let meal = try XCTUnwrap(mealDetailsResponse?.meals[0]) | ||
| XCTAssertNotNil(mealDetailsResponse) | ||
| XCTAssertEqual(mealDetailsResponse?.meals.count, 1) | ||
| XCTAssertEqual(meal.id, "52772") | ||
| XCTAssertEqual(meal.name, "Teriyaki Chicken Casserole") | ||
| XCTAssertEqual(meal.category, "Chicken") | ||
| XCTAssertEqual(meal.area, "Japanese") | ||
| XCTAssertEqual(meal.instructions, "Preheat oven to 350° F.") | ||
| XCTAssertEqual(meal.thumbnail, "https://www.themealdb.com/images/media/meals/wvpsxx1468256321.jpg") | ||
| XCTAssertEqual(meal.ingredients, ["soy sauce", "water", "brown sugar", "ground ginger", | ||
| "minced garlic", "cornstarch", "chicken breasts", | ||
| "stir-fry vegetables", "brown rice"]) | ||
| XCTAssertEqual(meal.measures, ["3/4 cup", "1/2 cup", "1/4 cup", "1/2 teaspoon", "1/2 teaspoon", | ||
| "4 Tablespoons", "2", "1 (12 oz.)", "3 cups"]) | ||
| } | ||
| } |
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.
Mutability is a big source of bugs. Avoid it.