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
This commit is contained in:
Brooke Kuhlmann
2026-01-22 15:19:53 -07:00
parent 9cd951f908
commit 97bf0e3c30
3 changed files with 47 additions and 27 deletions
+12 -12
View File
@@ -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
+9 -7
View File
@@ -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
+26 -8
View File
@@ -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