Added categories endpoint

Necessary to provide access to category data.

Milestone: minor
This commit is contained in:
Brooke Kuhlmann
2025-12-11 14:53:55 -07:00
parent 7155b0a2a5
commit ef400115e2
5 changed files with 104 additions and 0 deletions
+4
View File
@@ -9,6 +9,7 @@ module TRMNL
include Dependencies[:settings]
include Endpoints::Dependencies[
endpoint_categories: :categories,
endpoint_current_screen: :current_screen,
endpoint_display: :display,
endpoint_firmware: :firmware,
@@ -18,6 +19,7 @@ module TRMNL
]
include Inspectable[
endpoint_categories: :class,
endpoint_current_screen: :class,
endpoint_display: :class,
endpoint_firmware: :class,
@@ -31,6 +33,8 @@ module TRMNL
yield settings if block_given?
end
def categories = endpoint_categories.call
def current_screen(**) = endpoint_current_screen.call(**)
def display(**) = endpoint_display.call(**)
+27
View File
@@ -0,0 +1,27 @@
# frozen_string_literal: true
require "inspectable"
require "pipeable"
module TRMNL
module API
module Endpoints
# Handles API request/response.
class Category
include TRMNL::API::Dependencies[:requester, contract: "contracts.category"]
include Inspectable[contract: :class]
include Pipeable
def call
pipe(
requester.get("categories"),
try(:parse, catch: JSON::ParserError),
validate(contract, as: :to_h),
as(:fetch, :data)
)
end
end
end
end
end
+1
View File
@@ -9,6 +9,7 @@ module TRMNL
module Container
extend Containable
register(:categories) { Category.new }
register(:current_screen) { CurrentScreen.new }
register(:display) { Display.new }
register(:firmware) { Firmware.new }
+9
View File
@@ -31,6 +31,15 @@ RSpec.describe TRMNL::API::Client do
end
end
describe "#categories" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::Category }
it "messages endpoint" do
client = described_class.new endpoint_categories: endpoint
expect(client.categories).to have_received(:call)
end
end
describe "#current_screen" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::CurrentScreen }
@@ -0,0 +1,63 @@
# frozen_string_literal: true
require "spec_helper"
RSpec.describe TRMNL::API::Endpoints::Category do
subject(:endpoint) { described_class.new requester: }
include_context "with application dependencies"
let(:requester) { TRMNL::API::Requester.new http: }
describe "#call" do
context "with success" do
let :http do
HTTP::Fake::Client.new do
get "/api/categories" do
headers["Content-Type"] = "application/json"
status 200
<<~JSON
{
"data": [
"analytics",
"art",
"calendar"
]
}
JSON
end
end
end
it "answers response" do
result = endpoint.call
expect(result).to be_success(%w[analytics art calendar])
end
end
context "with failure" do
let :http do
HTTP::Fake::Client.new do
get "/api/categories" do
headers["Content-Type"] = "application/json"
status 404
<<~JSON
{"error": "Danger!"}
JSON
end
end
end
it "answers failure response" 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