diff --git a/lib/trmnl/api/models/model.rb b/lib/trmnl/api/models/model.rb index d997d33..bf2e250 100644 --- a/lib/trmnl/api/models/model.rb +++ b/lib/trmnl/api/models/model.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "refinements/hash" + module TRMNL module API module Models @@ -18,10 +20,10 @@ module TRMNL :height, :offset_x, :offset_y, - :palette_ids, + :palette_names, :css ) do - def self.for(attributes) = new(**attributes) + def self.for(attributes) = new(**attributes.transform_keys(palette_ids: :palette_names)) def initialize(**) super @@ -34,6 +36,8 @@ module TRMNL private def apply_defaults + self[:palette_names] ||= [] + %i[colors bit_depth scale_factor rotation width height offset_x offset_y].each do |name| self[name] ||= 0 end diff --git a/spec/lib/trmnl/api/endpoints/model_spec.rb b/spec/lib/trmnl/api/endpoints/model_spec.rb index c92b15a..455b66e 100644 --- a/spec/lib/trmnl/api/endpoints/model_spec.rb +++ b/spec/lib/trmnl/api/endpoints/model_spec.rb @@ -71,7 +71,7 @@ RSpec.describe TRMNL::API::Endpoints::Model do height: 480, offset_x: 10, offset_y: 15, - palette_ids: %w[bw grey-4], + palette_names: %w[bw grey-4], css: { classes: { device: "screen--v2", diff --git a/spec/lib/trmnl/api/models/model_spec.rb b/spec/lib/trmnl/api/models/model_spec.rb index 8ae48ee..6831cc0 100644 --- a/spec/lib/trmnl/api/models/model_spec.rb +++ b/spec/lib/trmnl/api/models/model_spec.rb @@ -36,7 +36,34 @@ RSpec.describe TRMNL::API::Models::Model do end it "answers record for attributes" do - expect(described_class.for(attributes)).to eq(described_class[**attributes]) + expect(described_class.for(attributes)).to eq( + described_class[ + name: "test", + label: "Test", + description: "A test.", + kind: "trmnl", + colors: 2, + bit_depth: 1, + scale_factor: 1, + rotation: 90, + mime_type: "image/png", + width: 800, + height: 480, + offset_x: 10, + offset_y: 15, + palette_names: %w[bw grey-4], + css: { + classes: { + device: "screen--v2", + size: "screen--lg" + }, + variables: [ + %w[--screen-w 1040px], + %w[--screen-h 780px] + ] + } + ] + ) end end @@ -56,7 +83,7 @@ RSpec.describe TRMNL::API::Models::Model do height: 0, offset_x: 0, offset_y: 0, - palette_ids: nil, + palette_names: [], css: nil ) end @@ -84,7 +111,7 @@ RSpec.describe TRMNL::API::Models::Model do height: 0, offset_x: 0, offset_y: 0, - palette_ids: nil, + palette_names: [], css: nil ) end