mirror of
https://github.com/usetrmnl/trmnl-api.git
synced 2026-04-29 13:35:13 -07:00
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
This commit is contained in:
@@ -3,6 +3,7 @@ inherit_gem:
|
||||
|
||||
Metrics/ParameterLists:
|
||||
Exclude:
|
||||
- lib/trmnl/api/models/recipes/meta.rb
|
||||
- lib/trmnl/api/requester.rb
|
||||
Naming/VariableNumber:
|
||||
Exclude:
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user