Bladeren bron

Change Recipe structure type from Class to Interface

Tati 7 jaren geleden
bovenliggende
commit
8de2b6f4e3

+ 7 - 27
ktchn/src/recipes/index.ts

@@ -43,33 +43,13 @@ export interface Author {
   website?: string,
 }
 
-export class Recipe {
-  public name: string;
-  ingredients!: ComposedIngredients[];
-  details!: RecipeDetails;
-  instructions!: string[];
-  author: Author;
+export interface Recipe {
+  name: string;
+  ingredients?: ComposedIngredients[];
+  details?: RecipeDetails;
+  instructions?: string[];
+  author?: Author;
   tags?: string[];
   course?: string[];
   summary?: string;
-
-  constructor(
-    name: string,
-    author: Author,
-    details?: RecipeDetails,
-    ingredients?: ComposedIngredients[],
-    instructions?: string[],
-    tags?: string[],
-    course?: string[],
-    summary?: string,
-  ) {
-    this.name = name;
-    this.author = author;
-    this.details = details || new RecipeDetails();
-    this.ingredients = ingredients || [];
-    this.instructions = instructions || [];
-    this.tags = tags || [];
-    this.course = course || [];
-    this.summary = summary || '';
-  }
-}
+}

+ 9 - 10
ktchn/src/recipes/scrape/sources/all-recipes.ts

@@ -63,16 +63,15 @@ const AR_CONFIG = {
   name: 'All Recipes',
   domain: 'allrecipes',
   website: 'https://www.allrecipes.com',
-  scrapeRecipe: ($: CheerioSelector): Recipe => new Recipe(
-    getRecipeName($),
-    getAuthorData($),
-    getRecipeDetails($),
-    getIngredients($),
-    getTextList(SELECTORS.INSTRUCTIONS)($),
-    $(SELECTORS.TAGS).map((i, e) => $(e).attr('content')).get(),
-    [],
-    $(SELECTORS.SUMMARY).attr('content'),
-  ),
+  scrapeRecipe: ($: CheerioSelector): Recipe => ({
+    name: getRecipeName($),
+    author: getAuthorData($),
+    details: getRecipeDetails($),
+    ingredients: getIngredients($),
+    instructions: getTextList(SELECTORS.INSTRUCTIONS)($),
+    tags: $(SELECTORS.TAGS).map((i, e) => $(e).attr('content')).get(),
+    summary: $(SELECTORS.SUMMARY).attr('content'),
+  }),
 }
 
 export default AR_CONFIG;

+ 8 - 12
ktchn/src/recipes/scrape/sources/home-cooking-adventure.ts

@@ -57,18 +57,14 @@ const HCA_CONFIG = {
   name: 'Home Cooking Adventure',
   domain: 'homecookingadventure',
   website: 'https://www.homecookingadventure.com',
-  scrapeRecipe: ($: CheerioSelector): Recipe => new Recipe(
-    getRecipeName($),
-    {
-      name: 'Home Cooking Adventure',
-    },
-    getRecipeDetails($),
-    getIngredients($),
-    getInstructions($),
-    [],
-    [],
-    $(SELECTORS.SUMMARY).children().first().text().trim(),  
-  )
+  scrapeRecipe: ($: CheerioSelector): Recipe => ({
+    name: getRecipeName($),
+    author: { name: 'Home Cooking Adventure' },
+    details: getRecipeDetails($),
+    ingredients: getIngredients($),
+    instructions: getInstructions($),
+    summary: $(SELECTORS.SUMMARY).children().first().text().trim(),  
+  }),
 }
 
 export default HCA_CONFIG;

+ 7 - 7
ktchn/src/recipes/scrape/sources/joy-of-baking.ts

@@ -52,13 +52,13 @@ const JOB_CONFIG = {
   domain: 'joyofbaking',
   website: JOB_AUTHOR_DATA.website,
   scrapeRecipe: ($: CheerioSelector): Recipe => {
-    return new Recipe(
-      getRecipeName($),
-      JOB_AUTHOR_DATA,
-      new RecipeDetails(), // There is no way to scrape this data from JOB
-      getIngredients($),
-      getInstructions($),
-    ); 
+    return {
+      name: getRecipeName($),
+      author: JOB_AUTHOR_DATA,
+      details: new RecipeDetails(), // There is no way to scrape this data from JOB
+      ingredients: getIngredients($),
+      instructions: getInstructions($),
+    }; 
   }
 }
 

+ 10 - 12
ktchn/src/recipes/scrape/sources/just-one-cookbook.ts

@@ -71,18 +71,16 @@ const JOC_CONFIG = {
   name: 'Just One Cookbook',
   domain: 'justonecookbook',
   website: 'https://www.justonecookbook.com',
-  scrapeRecipe: ($: CheerioSelector): Recipe => new Recipe(
-    getRecipeName($),
-    {
-      name: 'Nami',
-    },
-    getRecipeDetails($),
-    getIngredients($),
-    getTextList(SELECTORS.INSTRUCTIONS)($),
-    (getText(SELECTORS.TAGS)($)).split(',').map(R.trim),
-    [getText(SELECTORS.COURSE)($)],
-    getText(SELECTORS.SUMMARY)($),
-  )
+  scrapeRecipe: ($: CheerioSelector): Recipe => ({
+    name: getRecipeName($),
+    author: { name: 'Nami' },
+    details: getRecipeDetails($),
+    ingredients: getIngredients($),
+    instructions: getTextList(SELECTORS.INSTRUCTIONS)($),
+    tags: (getText(SELECTORS.TAGS)($)).split(',').map(R.trim),
+    course: [getText(SELECTORS.COURSE)($)],
+    summary: getText(SELECTORS.SUMMARY)($),
+  })
 }
 
 export default JOC_CONFIG;

+ 7 - 9
ktchn/src/recipes/scrape/sources/laura-in-the-kitchen.ts

@@ -106,15 +106,13 @@ const LITK_CONFIG = {
   name: 'Laura in the Kitchen',
   domain: 'laurainthekitchen',
   website: LITK_AUTHOR_DATA.website,
-  scrapeRecipe: ($: CheerioSelector): Recipe => {
-    return new Recipe(
-      getRecipeName($),
-      LITK_AUTHOR_DATA,
-      getRecipeDetails($),
-      getIngredients($),
-      getInstructions($),
-    )
-  },
+  scrapeRecipe: ($: CheerioSelector): Recipe => ({
+    name: getRecipeName($),
+    author: LITK_AUTHOR_DATA,
+    details: getRecipeDetails($),
+    ingredients: getIngredients($),
+    instructions: getInstructions($),
+  })
 }
 
 export default LITK_CONFIG;