From aea875bcdeb8e706f2a6c441c4abc1604913ae78 Mon Sep 17 00:00:00 2001 From: Erik Hetzner Date: Fri, 7 May 2021 18:00:30 -0700 Subject: [PATCH] Extract from @graph in json-ld Fix for #58 --- org-chef-json-ld.el | 4 ++++ tests/json-ld-sample-graph.json | 37 +++++++++++++++++++++++++++++++++ tests/org-chef-json-ld-test.el | 8 +++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/json-ld-sample-graph.json diff --git a/org-chef-json-ld.el b/org-chef-json-ld.el index 456b7fd..bcb2c93 100644 --- a/org-chef-json-ld.el +++ b/org-chef-json-ld.el @@ -91,6 +91,10 @@ (and (stringp type) (string= type "Recipe")))) json) + ((and (json-alist-p json) + (assq '@graph json) + (sequencep (cdr (assq '@graph json)))) + (cl-some #'org-chef-json-ld-extract-recipe (cdr (assq '@graph json)))) ((and (not (json-alist-p json)) (sequencep json)) (cl-some #'org-chef-json-ld-extract-recipe json)) diff --git a/tests/json-ld-sample-graph.json b/tests/json-ld-sample-graph.json new file mode 100644 index 0000000..5cbefe7 --- /dev/null +++ b/tests/json-ld-sample-graph.json @@ -0,0 +1,37 @@ +{ + "@context": "https://schema.org", + "@graph": [ + { + "@context": "https://schema.org", + "@type": "Recipe", + "author": "Jake Smith", + "cookTime": "PT2H", + "datePublished": "2015-05-18", + "description": "Your recipe description goes here", + "image": "http://www.example.com/images.jpg", + "recipeIngredient": [ + "ingredient 1", + "ingredient 2", + "ingredient 3", + "ingredient 4", + "ingredient 5" + ], + "interactionStatistic": { + "@type": "InteractionCounter", + "interactionType": "http://schema.org/Comment", + "userInteractionCount": "5" + }, + "name": "Rand's Cookies", + "nutrition": { + "@type": "NutritionInformation", + "calories": "1200 calories", + "carbohydrateContent": "12 carbs", + "proteinContent": "9 grams of protein", + "fatContent": "9 grams fat" + }, + "prepTime": "PT15M", + "recipeInstructions": "This is the long part, etc.", + "recipeYield": "12 cookies" + } + ] +} diff --git a/tests/org-chef-json-ld-test.el b/tests/org-chef-json-ld-test.el index 790cbc7..f705a62 100644 --- a/tests/org-chef-json-ld-test.el +++ b/tests/org-chef-json-ld-test.el @@ -16,6 +16,14 @@ (should (eq 5 (length ingredients))) (should (string= "ingredient 1" (elt ingredients 0))))) +(ert-deftest org-chef-json-ld-extract-recipe-graph () + (let* ((json (json-read-file "json-ld-sample-graph.json")) + (recipe (org-chef-json-ld-extract-recipe json)) + (ingredients (cdr (assq 'recipeIngredient recipe)))) + (should (string= "PT2H" (cdr (assq 'cookTime recipe)))) + (should (eq 5 (length ingredients))) + (should (string= "ingredient 1" (elt ingredients 0))))) + (ert-deftest org-chef-json-ld-parse-duration () (should (string= "1 hour" (org-chef-json-ld-parse-duration "PT1H"))) (should (string= "1 hour" (org-chef-json-ld-parse-duration "PT01H")))