From f59688fee55ec1bb9c1f95b983a25c768ac14bdf Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Thu, 22 Jan 2026 14:12:09 -0700 Subject: [PATCH] Added recipe meta model Necessary to handle API response metadata so we can have distinct models for data and metadata. RuboCop has been updated to ignore the parameters because RuboCop still doesn't have the ability to distinguish between whole value objects and standard objects (and we need safe defaults in this case). Milestone: minor --- .config/rubocop/config.yml | 1 + lib/trmnl/api/models/recipes/meta.rb | 34 +++++++++++ .../lib/trmnl/api/models/recipes/meta_spec.rb | 57 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 lib/trmnl/api/models/recipes/meta.rb create mode 100644 spec/lib/trmnl/api/models/recipes/meta_spec.rb diff --git a/.config/rubocop/config.yml b/.config/rubocop/config.yml index 089e133..92e37b0 100644 --- a/.config/rubocop/config.yml +++ b/.config/rubocop/config.yml @@ -3,6 +3,7 @@ inherit_gem: Metrics/ParameterLists: Exclude: + - lib/trmnl/api/models/recipes/meta.rb - lib/trmnl/api/requester.rb Naming/VariableNumber: Exclude: diff --git a/lib/trmnl/api/models/recipes/meta.rb b/lib/trmnl/api/models/recipes/meta.rb new file mode 100644 index 0000000..8bbd119 --- /dev/null +++ b/lib/trmnl/api/models/recipes/meta.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module TRMNL + module API + module Models + module Recipes + # Models the metadata of the API response. + Meta = ::Data.define( + :from, + :to, + :current_page, + :per_page, + :total, + :prev_page_url, + :next_page_url + ) do + def initialize from: 0, + to: 0, + current_page: 0, + per_page: 0, + total: 0, + prev_page_url: nil, + next_page_url: nil + super + end + + def more? = to.positive? && to < total + + def next_page = current_page + 1 + end + end + end + end +end diff --git a/spec/lib/trmnl/api/models/recipes/meta_spec.rb b/spec/lib/trmnl/api/models/recipes/meta_spec.rb new file mode 100644 index 0000000..015b2ed --- /dev/null +++ b/spec/lib/trmnl/api/models/recipes/meta_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe TRMNL::API::Models::Recipes::Meta do + subject(:meta) { described_class[**attributes] } + + let :attributes do + { + 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 "#initialize" do + it "answers defaults" do + expect(described_class.new).to eq( + described_class[ + from: 0, + to: 0, + current_page: 0, + per_page: 0, + total: 0, + prev_page_url: nil, + next_page_url: nil + ] + ) + end + end + + describe "#more?" do + it "answers true when there are more pages" do + expect(meta.more?).to be(true) + end + + it "answers false when to is zero" do + attributes[:to] = 0 + expect(meta.more?).to be(false) + end + + it "answers false when there are no more pages" do + attributes[:to] = 200 + expect(meta.more?).to be(false) + end + end + + describe "#next_page" do + it "answers next page" do + expect(meta.next_page).to eq(2) + end + end +end