From a941c8f18014cb97b86c0eef30a0d7671fa08320 Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Tue, 22 Apr 2025 09:14:50 -0600 Subject: [PATCH] Added firmware endpoint Necessary to handle the request and response of the firmware API endpoint. --- lib/trmnl/api/endpoints/firmware.rb | 22 +++++++ spec/lib/trmnl/api/endpoints/firmware_spec.rb | 60 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 lib/trmnl/api/endpoints/firmware.rb create mode 100644 spec/lib/trmnl/api/endpoints/firmware_spec.rb diff --git a/lib/trmnl/api/endpoints/firmware.rb b/lib/trmnl/api/endpoints/firmware.rb new file mode 100644 index 0000000..a78e831 --- /dev/null +++ b/lib/trmnl/api/endpoints/firmware.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "pipeable" + +module TRMNL + module API + module Endpoints + # Handles API request/response. + class Firmware + include Dependencies[:client, contract: "contracts.firmware", model: "models.firmware"] + include Pipeable + + def call + pipe client.get("firmware/latest"), + 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/firmware_spec.rb b/spec/lib/trmnl/api/endpoints/firmware_spec.rb new file mode 100644 index 0000000..2c50379 --- /dev/null +++ b/spec/lib/trmnl/api/endpoints/firmware_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe TRMNL::API::Endpoints::Firmware do + subject(:endpoint) { described_class.new client: } + + include_context "with application dependencies" + + let(:client) { TRMNL::API::Client.new http: } + + describe "#call" do + let :http do + HTTP::Fake::Client.new do + get "/api/firmware/latest" do + headers["Content-Type"] = "application/json" + status 200 + + <<~JSON + { + "url": "https://test.io/FW1.2.3.bin", + "version": "1.2.3" + } + JSON + end + end + end + + it "answers success" do + result = endpoint.call + + expect(result).to be_success( + TRMNL::API::Models::Firmware[ + url: "https://test.io/FW1.2.3.bin", + version: "1.2.3" + ] + ) + end + + context "with failure" do + let :http do + HTTP::Fake::Client.new do + get "/api/firmware/latest" do + headers["Content-Type"] = "application/json" + status 404 + + <<~JSON + {"error": "Danger!"} + JSON + end + end + end + + it "answers failure" do + result = described_class.new(client:).call + expect(result).to match(Failure(be_a(HTTP::Response))) + end + end + end +end