mirror of
https://github.com/usetrmnl/trmnl-api.git
synced 2026-04-29 13:35:13 -07:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user