From 9b6446c5bb0741d0bb4cae5f195dceea59581686 Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Tue, 22 Apr 2025 09:12:34 -0600 Subject: [PATCH] Added current screen endpoint Necessary to handle the request and response to the current screen API endpoint. --- lib/trmnl/api/endpoints/current_screen.rb | 27 ++++++++ .../api/endpoints/current_screen_spec.rb | 66 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 lib/trmnl/api/endpoints/current_screen.rb create mode 100644 spec/lib/trmnl/api/endpoints/current_screen_spec.rb diff --git a/lib/trmnl/api/endpoints/current_screen.rb b/lib/trmnl/api/endpoints/current_screen.rb new file mode 100644 index 0000000..330fd59 --- /dev/null +++ b/lib/trmnl/api/endpoints/current_screen.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "pipeable" + +module TRMNL + module API + module Endpoints + # Handles API request/response. + class CurrentScreen + include Dependencies[ + :client, + contract: "contracts.current_screen", + model: "models.current_screen" + ] + + include Pipeable + + def call token: + pipe client.get("current_screen", 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/current_screen_spec.rb b/spec/lib/trmnl/api/endpoints/current_screen_spec.rb new file mode 100644 index 0000000..a4d246f --- /dev/null +++ b/spec/lib/trmnl/api/endpoints/current_screen_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe TRMNL::API::Endpoints::CurrentScreen 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/current_screen" do + headers["Content-Type"] = "application/json" + status 200 + + <<~JSON + { + "status": 200, + "refresh_rate": 3200, + "image_url": "https://test.io/images/test.bmp", + "filename": "test.bmp", + "rendered_at": null + } + JSON + end + end + end + + it "answers success" do + result = endpoint.call token: "secret" + + expect(result).to be_success( + TRMNL::API::Models::CurrentScreen[ + refresh_rate: 3200, + image_url: "https://test.io/images/test.bmp", + filename: "test.bmp" + ] + ) + end + end + + context "with failure" do + let :http do + HTTP::Fake::Client.new do + get "/api/current_screen" do + headers["Content-Type"] = "application/json" + status 404 + + <<~JSON + {"error": "Danger!"} + JSON + end + end + end + + it "answers error response" do + result = described_class.new(client:).call token: "secret" + expect(result).to match(Failure(be_a(HTTP::Response))) + end + end + end +end