From 2da39573fc28e8afd539e842b970a9bd3a3f94dc Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Tue, 22 Apr 2025 09:14:23 -0600 Subject: [PATCH] Added display endpoint Necessary to handle the request and response of the display API endpoint. --- lib/trmnl/api/endpoints/display.rb | 22 ++++++ spec/lib/trmnl/api/endpoints/display_spec.rb | 72 ++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 lib/trmnl/api/endpoints/display.rb create mode 100644 spec/lib/trmnl/api/endpoints/display_spec.rb diff --git a/lib/trmnl/api/endpoints/display.rb b/lib/trmnl/api/endpoints/display.rb new file mode 100644 index 0000000..77b3eeb --- /dev/null +++ b/lib/trmnl/api/endpoints/display.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "pipeable" + +module TRMNL + module API + module Endpoints + # Handles API request/response. + class Display + include Dependencies[:client, contract: "contracts.display", model: "models.display"] + include Pipeable + + def call token: + pipe client.get("display", headers: {"Access-Token" => token}), + try(:parse, catch: JSON::ParserError), + validate(contract, as: :to_h), + to(model, :for) + end + end + end + end +end diff --git a/spec/lib/trmnl/api/endpoints/display_spec.rb b/spec/lib/trmnl/api/endpoints/display_spec.rb new file mode 100644 index 0000000..0642ddb --- /dev/null +++ b/spec/lib/trmnl/api/endpoints/display_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe TRMNL::API::Endpoints::Display do + subject(:endpoint) { described_class.new client: } + + include_context "with application dependencies" + + let(:client) { TRMNL::API::Client.new http: } + + describe "#call" do + context "with success" do + let :http do + HTTP::Fake::Client.new do + get "/api/display" do + headers["Content-Type"] = "application/json" + status 200 + + <<~JSON + { + "filename": "test.bmp", + "firmware_url": "https://test.io/FW1.4.8.bin", + "image_url": "https://test.io/images/test.bmp", + "refresh_rate": 3200, + "reset_firmware": false, + "special_function": "restart_playlist", + "status": 0, + "update_firmware": true + } + JSON + end + end + end + + it "answers response" do + result = endpoint.call token: "secret" + expect(result).to be_success( + TRMNL::API::Models::Display[ + filename: "test.bmp", + firmware_url: "https://test.io/FW1.4.8.bin", + image_url: "https://test.io/images/test.bmp", + refresh_rate: 3200, + reset_firmware: false, + special_function: "restart_playlist", + update_firmware: true + ] + ) + end + end + + context "with failure" do + let :http do + HTTP::Fake::Client.new do + get "/api/display" do + headers["Content-Type"] = "application/json" + status 404 + + <<~JSON + {"error": "Danger!"} + JSON + end + end + end + + it "answers failure response" do + result = described_class.new(client:).call token: "secret" + expect(result).to match(Failure(be_a(HTTP::Response))) + end + end + end +end