From 97bf0e3c308e8886196480a2ebd9e446cde2feb0 Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Wed, 21 Jan 2026 17:43:58 -0700 Subject: [PATCH] Updated recipe model to distinquish between data and meta information Necessary to prevent the API design from infecting this implementation by clarifying the difference between data and metadata information (like how a basic JSON Data API response should be structured). Milestone: minor --- lib/trmnl/api/models/recipe.rb | 24 +++++++-------- spec/lib/trmnl/api/endpoints/recipe_spec.rb | 16 +++++----- spec/lib/trmnl/api/models/recipe_spec.rb | 34 ++++++++++++++++----- 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/lib/trmnl/api/models/recipe.rb b/lib/trmnl/api/models/recipe.rb index 998efbf..4ffb19d 100644 --- a/lib/trmnl/api/models/recipe.rb +++ b/lib/trmnl/api/models/recipe.rb @@ -3,20 +3,20 @@ module TRMNL module API module Models - # Models the data of the API response. - Recipe = ::Data.define( - :data, - :total, - :from, - :to, - :per_page, - :current_page, - :prev_page_url, - :next_page_url - ) do + # Models the payload of the API response. + Recipe = ::Data.define :data, :meta do def self.for(**attributes) + meta = attributes.slice :from, + :to, + :current_page, + :per_page, + :total, + :prev_page_url, + :next_page_url + data = attributes[:data].map { Recipes::Data.for(**it) } - new(**attributes.merge!(data:)) + + new meta: Recipes::Meta[**meta], data: end end end diff --git a/spec/lib/trmnl/api/endpoints/recipe_spec.rb b/spec/lib/trmnl/api/endpoints/recipe_spec.rb index d7bd532..367e3a5 100644 --- a/spec/lib/trmnl/api/endpoints/recipe_spec.rb +++ b/spec/lib/trmnl/api/endpoints/recipe_spec.rb @@ -73,13 +73,15 @@ RSpec.describe TRMNL::API::Endpoints::Recipe do statistics: TRMNL::API::Models::Recipes::Statistics[installs: 1, forks: 2] ] ], - total: 2, - from: 1, - to: 2, - per_page: 25, - current_page: 1, - prev_page_url: nil, - next_page_url: "/recipes.json?page=2" + meta: TRMNL::API::Models::Recipes::Meta[ + total: 2, + from: 1, + to: 2, + per_page: 25, + current_page: 1, + prev_page_url: nil, + next_page_url: "/recipes.json?page=2" + ] ] ) end diff --git a/spec/lib/trmnl/api/models/recipe_spec.rb b/spec/lib/trmnl/api/models/recipe_spec.rb index d2e15d9..420bab5 100644 --- a/spec/lib/trmnl/api/models/recipe_spec.rb +++ b/spec/lib/trmnl/api/models/recipe_spec.rb @@ -3,6 +3,21 @@ require "spec_helper" RSpec.describe TRMNL::API::Models::Recipe do + subject(:recipe) { described_class[**attributes] } + + let :attributes do + { + data: [], + from: 1, + to: 25, + per_page: 25, + current_page: 1, + total: 200, + prev_page_url: nil, + next_page_url: "/recipes.json?page=2" + } + end + describe ".for" do let :attributes do { @@ -38,11 +53,12 @@ RSpec.describe TRMNL::API::Models::Recipe do } } ], - total: 100, + from: 1, to: 25, per_page: 25, current_page: 1, + total: 100, prev_page_url: nil, next_page_url: "/recipes.json?page=2" } @@ -81,13 +97,15 @@ RSpec.describe TRMNL::API::Models::Recipe do statistics: TRMNL::API::Models::Recipes::Statistics[installs: 6, forks: 0] ] ], - total: 100, - from: 1, - to: 25, - per_page: 25, - current_page: 1, - prev_page_url: nil, - next_page_url: "/recipes.json?page=2" + meta: TRMNL::API::Models::Recipes::Meta[ + from: 1, + to: 25, + per_page: 25, + current_page: 1, + total: 100, + prev_page_url: nil, + next_page_url: "/recipes.json?page=2" + ] ] ) end