Added display model

Necessary to model the API response payload.
This commit is contained in:
Brooke Kuhlmann
2025-04-22 14:24:33 -06:00
parent e7bac9edc3
commit 8137e1f5f0
2 changed files with 100 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
# frozen_string_literal: true
module TRMNL
module API
module Models
# Models data for API display responses.
Display = Struct.new(
:filename,
:firmware_url,
:image_url,
:image_url_timeout,
:refresh_rate,
:reset_firmware,
:special_function,
:update_firmware
) do
def self.for(attributes) = new(**attributes)
def initialize(**)
super
apply_defaults
freeze
end
def to_json(*) = to_h.to_json(*)
private
def apply_defaults
self[:image_url_timeout] ||= 0
self[:refresh_rate] ||= 300
self[:reset_firmware] ||= false
self[:update_firmware] ||= false
self[:special_function] ||= "sleep"
end
end
end
end
end
+61
View File
@@ -0,0 +1,61 @@
# frozen_string_literal: true
require "spec_helper"
RSpec.describe TRMNL::API::Models::Display do
subject(:model) { described_class.new }
describe ".for" do
let :attributes do
{
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
it "answers record for attributes" do
expect(described_class.for(attributes)).to eq(described_class[**attributes])
end
end
describe "#initialize" do
it "answers default attributes" do
expect(model.to_h).to eq(
filename: nil,
firmware_url: nil,
image_url: nil,
image_url_timeout: 0,
refresh_rate: 300,
reset_firmware: false,
special_function: "sleep",
update_firmware: false
)
end
it "is frozen" do
expect(model.frozen?).to be(true)
end
end
describe "#to_json" do
it "answers JSON" do
payload = JSON model.to_json, symbolize_names: true
expect(payload).to eq(
filename: nil,
firmware_url: nil,
image_url: nil,
image_url_timeout: 0,
refresh_rate: 300,
reset_firmware: false,
special_function: "sleep",
update_firmware: false
)
end
end
end