Added palette endpoint

Necessary to request and process Palettes API response.

Milestone: minor
This commit is contained in:
Brooke Kuhlmann
2025-12-11 14:53:55 -07:00
parent c200ae5491
commit 1cc2aeadc2
5 changed files with 123 additions and 0 deletions
+4
View File
@@ -16,6 +16,7 @@ module TRMNL
endpoint_ip_addresses: :ip_addresses,
endpoint_log: :log,
endpoint_models: :models,
endpoint_palettes: :palettes,
endpoint_setup: :setup
]
@@ -27,6 +28,7 @@ module TRMNL
endpoint_ip_addresses: :class,
endpoint_log: :class,
endpoint_models: :class,
endpoint_palettes: :class,
endpoint_setup: :class
]
@@ -49,6 +51,8 @@ module TRMNL
def models(**) = endpoint_models.call(**)
def palettes = endpoint_palettes.call
def setup(**) = endpoint_setup.call(**)
end
end
+1
View File
@@ -16,6 +16,7 @@ module TRMNL
register(:ip_addresses) { IPAddress.new }
register(:log) { Log.new }
register(:models) { Model.new }
register(:palettes) { Palette.new }
register(:setup) { Setup.new }
end
end
+32
View File
@@ -0,0 +1,32 @@
# frozen_string_literal: true
require "inspectable"
require "pipeable"
module TRMNL
module API
module Endpoints
# Handles API request/response.
class Palette
include TRMNL::API::Dependencies[
:requester,
contract: "contracts.palette",
model: "models.palette"
]
include Inspectable[contract: :class]
include Pipeable
def call
pipe(
requester.get("palettes"),
try(:parse, catch: JSON::ParserError),
validate(contract, as: :to_h),
as(:fetch, :data),
map { |data| model.for(**data) }
)
end
end
end
end
end
+10
View File
@@ -94,6 +94,15 @@ RSpec.describe TRMNL::API::Client do
end
end
describe "#palette" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::Palette }
it "messages endpoint" do
client = described_class.new endpoint_palettes: endpoint
expect(client.palettes).to have_received(:call).with(no_args)
end
end
describe "#setup" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::Setup }
@@ -112,6 +121,7 @@ RSpec.describe TRMNL::API::Client do
"@endpoint_ip_addresses=TRMNL::API::Endpoints::IPAddress, " \
"@endpoint_log=TRMNL::API::Endpoints::Log, " \
"@endpoint_models=TRMNL::API::Endpoints::Model, " \
"@endpoint_palettes=TRMNL::API::Endpoints::Palette, " \
"@endpoint_setup=TRMNL::API::Endpoints::Setup, "
)
end
@@ -0,0 +1,76 @@
# frozen_string_literal: true
require "spec_helper"
RSpec.describe TRMNL::API::Endpoints::Palette do
subject(:endpoint) { described_class.new requester: }
include_context "with application dependencies"
let(:requester) { TRMNL::API::Requester.new http: }
describe "#call" do
let :http do
HTTP::Fake::Client.new do
get "/api/palettes" do
headers["Content-Type"] = "application/json"
status 200
<<~JSON
{
"data": [
{
"id": "test",
"name": "Test",
"grays": 2,
"colors": ["#000000", "#FFFFFF"],
"framework_class": "screen--1bit"
}
]
}
JSON
end
end
end
it "answers success" do
result = endpoint.call
expect(result).to be_success(
[
TRMNL::API::Models::Palette[
id: "test",
name: "Test",
grays: 2,
colors: %w[#000000 #FFFFFF],
framework_class: "screen--1bit"
]
]
)
end
context "with failure" do
let :http do
HTTP::Fake::Client.new do
get "/api/palettes" do
headers["Content-Type"] = "application/json"
status 404
<<~JSON
{"error": "Danger!"}
JSON
end
end
end
it "answers failure" do
result = described_class.new(requester:).call
expect(result).to match(Failure(be_a(HTTP::Response)))
end
end
end
describe "#inspect" do
it_behaves_like "an inspectable endpoint", described_class.new
end
end