From 616c11b9a4e9942c1c6dac0411e55f5077a513e2 Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Wed, 1 Apr 2026 13:32:40 -0600 Subject: [PATCH] Refactored contracts as schemas Necessary to properly namespace these objects. This will also allow for contracts in the future if ever needed since contracts can be built upon schemas. Milestone: patch --- .config/rubocop/config.yml | 2 +- lib/trmnl/api/container.rb | 20 +++++++++---------- lib/trmnl/api/endpoints/category.rb | 6 +++--- lib/trmnl/api/endpoints/current_screen.rb | 6 +++--- lib/trmnl/api/endpoints/display.rb | 6 +++--- lib/trmnl/api/endpoints/firmware.rb | 6 +++--- lib/trmnl/api/endpoints/ip_address.rb | 6 +++--- lib/trmnl/api/endpoints/model.rb | 6 +++--- lib/trmnl/api/endpoints/palette.rb | 6 +++--- lib/trmnl/api/endpoints/recipe.rb | 6 +++--- lib/trmnl/api/endpoints/setup.rb | 6 +++--- .../api/{contracts => schemas}/category.rb | 2 +- .../{contracts => schemas}/current_screen.rb | 2 +- .../api/{contracts => schemas}/display.rb | 2 +- .../api/{contracts => schemas}/firmware.rb | 2 +- .../api/{contracts => schemas}/ip_address.rb | 2 +- lib/trmnl/api/{contracts => schemas}/model.rb | 2 +- .../api/{contracts => schemas}/palette.rb | 2 +- .../api/{contracts => schemas}/recipe.rb | 2 +- .../{contracts => schemas}/recipes/author.rb | 2 +- lib/trmnl/api/{contracts => schemas}/setup.rb | 2 +- spec/lib/trmnl/api/endpoints/category_spec.rb | 2 +- .../api/endpoints/current_screen_spec.rb | 2 +- spec/lib/trmnl/api/endpoints/display_spec.rb | 2 +- spec/lib/trmnl/api/endpoints/firmware_spec.rb | 2 +- .../trmnl/api/endpoints/ip_address_spec.rb | 2 +- spec/lib/trmnl/api/endpoints/model_spec.rb | 2 +- spec/lib/trmnl/api/endpoints/palette_spec.rb | 2 +- spec/lib/trmnl/api/endpoints/recipe_spec.rb | 2 +- spec/lib/trmnl/api/endpoints/setup_spec.rb | 2 +- .../api/{contracts => schemas}/model_spec.rb | 2 +- 31 files changed, 58 insertions(+), 58 deletions(-) rename lib/trmnl/api/{contracts => schemas}/category.rb (90%) rename lib/trmnl/api/{contracts => schemas}/current_screen.rb (93%) rename lib/trmnl/api/{contracts => schemas}/display.rb (96%) rename lib/trmnl/api/{contracts => schemas}/firmware.rb (92%) rename lib/trmnl/api/{contracts => schemas}/ip_address.rb (93%) rename lib/trmnl/api/{contracts => schemas}/model.rb (98%) rename lib/trmnl/api/{contracts => schemas}/palette.rb (95%) rename lib/trmnl/api/{contracts => schemas}/recipe.rb (98%) rename lib/trmnl/api/{contracts => schemas}/recipes/author.rb (98%) rename lib/trmnl/api/{contracts => schemas}/setup.rb (94%) rename spec/lib/trmnl/api/{contracts => schemas}/model_spec.rb (97%) diff --git a/.config/rubocop/config.yml b/.config/rubocop/config.yml index 92e37b0..0e006a4 100644 --- a/.config/rubocop/config.yml +++ b/.config/rubocop/config.yml @@ -7,7 +7,7 @@ Metrics/ParameterLists: - lib/trmnl/api/requester.rb Naming/VariableNumber: Exclude: - - lib/trmnl/api/contracts/ip_address.rb + - lib/trmnl/api/schemas/ip_address.rb - lib/trmnl/api/models/ip_address.rb - spec/lib/trmnl/api/models/ip_address_spec.rb Style/IpAddresses: diff --git a/lib/trmnl/api/container.rb b/lib/trmnl/api/container.rb index 27ba731..afdbc08 100644 --- a/lib/trmnl/api/container.rb +++ b/lib/trmnl/api/container.rb @@ -19,16 +19,16 @@ module TRMNL HTTP end - namespace :contracts do - register :category, Contracts::Category - register :current_screen, Contracts::CurrentScreen - register :display, Contracts::Display - register :firmware, Contracts::Firmware - register :ip_address, Contracts::IPAddress - register :model, Contracts::Model - register :palette, Contracts::Palette - register :recipe, Contracts::Recipe - register :setup, Contracts::Setup + namespace :schemas do + register :category, Schemas::Category + register :current_screen, Schemas::CurrentScreen + register :display, Schemas::Display + register :firmware, Schemas::Firmware + register :ip_address, Schemas::IPAddress + register :model, Schemas::Model + register :palette, Schemas::Palette + register :recipe, Schemas::Recipe + register :setup, Schemas::Setup end namespace :models do diff --git a/lib/trmnl/api/endpoints/category.rb b/lib/trmnl/api/endpoints/category.rb index 2921d2c..46319c5 100644 --- a/lib/trmnl/api/endpoints/category.rb +++ b/lib/trmnl/api/endpoints/category.rb @@ -8,16 +8,16 @@ module TRMNL module Endpoints # Handles API request/response. class Category - include TRMNL::API::Dependencies[:requester, contract: "contracts.category"] + include TRMNL::API::Dependencies[:requester, schema: "schemas.category"] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call pipe( requester.get("categories"), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), as(:fetch, :data) ) end diff --git a/lib/trmnl/api/endpoints/current_screen.rb b/lib/trmnl/api/endpoints/current_screen.rb index df8cfea..6a3f0d4 100644 --- a/lib/trmnl/api/endpoints/current_screen.rb +++ b/lib/trmnl/api/endpoints/current_screen.rb @@ -10,17 +10,17 @@ module TRMNL class CurrentScreen include TRMNL::API::Dependencies[ :requester, - contract: "contracts.current_screen", + schema: "schemas.current_screen", model: "models.current_screen" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call token: pipe requester.get("current_screen", headers: {"Access-Token" => token}), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), to(model, :for) end end diff --git a/lib/trmnl/api/endpoints/display.rb b/lib/trmnl/api/endpoints/display.rb index 8fd6d09..fac1044 100644 --- a/lib/trmnl/api/endpoints/display.rb +++ b/lib/trmnl/api/endpoints/display.rb @@ -10,17 +10,17 @@ module TRMNL class Display include TRMNL::API::Dependencies[ :requester, - contract: "contracts.display", + schema: "schemas.display", model: "models.display" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call token: pipe requester.get("display", headers: {"Access-Token" => token}), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), to(model, :for) end end diff --git a/lib/trmnl/api/endpoints/firmware.rb b/lib/trmnl/api/endpoints/firmware.rb index 228c863..bdf26fb 100644 --- a/lib/trmnl/api/endpoints/firmware.rb +++ b/lib/trmnl/api/endpoints/firmware.rb @@ -10,17 +10,17 @@ module TRMNL class Firmware include TRMNL::API::Dependencies[ :requester, - contract: "contracts.firmware", + schema: "schemas.firmware", model: "models.firmware" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call pipe requester.get("firmware/latest"), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), to(model, :for) end end diff --git a/lib/trmnl/api/endpoints/ip_address.rb b/lib/trmnl/api/endpoints/ip_address.rb index 2222819..5dec2a9 100644 --- a/lib/trmnl/api/endpoints/ip_address.rb +++ b/lib/trmnl/api/endpoints/ip_address.rb @@ -10,17 +10,17 @@ module TRMNL class IPAddress include TRMNL::API::Dependencies[ :requester, - contract: "contracts.ip_address", + schema: "schemas.ip_address", model: "models.ip_address" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call pipe requester.get("ips"), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), as(:fetch, :data), to(model, :for) end diff --git a/lib/trmnl/api/endpoints/model.rb b/lib/trmnl/api/endpoints/model.rb index 9733334..9be8ce5 100644 --- a/lib/trmnl/api/endpoints/model.rb +++ b/lib/trmnl/api/endpoints/model.rb @@ -10,18 +10,18 @@ module TRMNL class Model include TRMNL::API::Dependencies[ :requester, - contract: "contracts.model", + schema: "schemas.model", model: "models.model" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call pipe( requester.get("models"), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), as(:fetch, :data), map { |data| model.for(**data) } ) diff --git a/lib/trmnl/api/endpoints/palette.rb b/lib/trmnl/api/endpoints/palette.rb index 1de099d..ff182d7 100644 --- a/lib/trmnl/api/endpoints/palette.rb +++ b/lib/trmnl/api/endpoints/palette.rb @@ -10,18 +10,18 @@ module TRMNL class Palette include TRMNL::API::Dependencies[ :requester, - contract: "contracts.palette", + schema: "schemas.palette", model: "models.palette" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call pipe( requester.get("palettes"), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), as(:fetch, :data), map { |data| model.for(**data) } ) diff --git a/lib/trmnl/api/endpoints/recipe.rb b/lib/trmnl/api/endpoints/recipe.rb index 0f6e6d4..344117d 100644 --- a/lib/trmnl/api/endpoints/recipe.rb +++ b/lib/trmnl/api/endpoints/recipe.rb @@ -11,11 +11,11 @@ module TRMNL class Recipe include TRMNL::API::Dependencies[ :requester, - contract: "contracts.recipe", + schema: "schemas.recipe", model: "models.recipe" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable using Refinements::Hash @@ -26,7 +26,7 @@ module TRMNL pipe( requester.get("recipes.json", **parameters), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), to(model, :for) ) end diff --git a/lib/trmnl/api/endpoints/setup.rb b/lib/trmnl/api/endpoints/setup.rb index f6cdb31..0ccff12 100644 --- a/lib/trmnl/api/endpoints/setup.rb +++ b/lib/trmnl/api/endpoints/setup.rb @@ -10,17 +10,17 @@ module TRMNL class Setup include TRMNL::API::Dependencies[ :requester, - contract: "contracts.setup", + schema: "schemas.setup", model: "models.setup" ] - include Inspectable[contract: :type] + include Inspectable[schema: :type] include Pipeable def call id: pipe requester.get("setup", headers: {"ID" => id}), try(:parse, catch: JSON::ParserError), - validate(contract, as: :to_h), + validate(schema, as: :to_h), to(model, :for) end end diff --git a/lib/trmnl/api/contracts/category.rb b/lib/trmnl/api/schemas/category.rb similarity index 90% rename from lib/trmnl/api/contracts/category.rb rename to lib/trmnl/api/schemas/category.rb index 04c75da..9d9eac7 100644 --- a/lib/trmnl/api/contracts/category.rb +++ b/lib/trmnl/api/schemas/category.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. Category = Dry::Schema.JSON { required(:data).array(:string) } end diff --git a/lib/trmnl/api/contracts/current_screen.rb b/lib/trmnl/api/schemas/current_screen.rb similarity index 93% rename from lib/trmnl/api/contracts/current_screen.rb rename to lib/trmnl/api/schemas/current_screen.rb index f2fda7b..73a234f 100644 --- a/lib/trmnl/api/contracts/current_screen.rb +++ b/lib/trmnl/api/schemas/current_screen.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. CurrentScreen = Dry::Schema.JSON do required(:refresh_rate).filled :integer diff --git a/lib/trmnl/api/contracts/display.rb b/lib/trmnl/api/schemas/display.rb similarity index 96% rename from lib/trmnl/api/contracts/display.rb rename to lib/trmnl/api/schemas/display.rb index 5b9f4fb..c166b9b 100644 --- a/lib/trmnl/api/contracts/display.rb +++ b/lib/trmnl/api/schemas/display.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. Display = Dry::Schema.JSON do required(:filename).filled :string diff --git a/lib/trmnl/api/contracts/firmware.rb b/lib/trmnl/api/schemas/firmware.rb similarity index 92% rename from lib/trmnl/api/contracts/firmware.rb rename to lib/trmnl/api/schemas/firmware.rb index 4dc5a75..6e148f8 100644 --- a/lib/trmnl/api/contracts/firmware.rb +++ b/lib/trmnl/api/schemas/firmware.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. Firmware = Dry::Schema.JSON do required(:url).filled :string diff --git a/lib/trmnl/api/contracts/ip_address.rb b/lib/trmnl/api/schemas/ip_address.rb similarity index 93% rename from lib/trmnl/api/contracts/ip_address.rb rename to lib/trmnl/api/schemas/ip_address.rb index 9d9d683..a150fe4 100644 --- a/lib/trmnl/api/contracts/ip_address.rb +++ b/lib/trmnl/api/schemas/ip_address.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. IPAddress = Dry::Schema.JSON do required(:data).hash do diff --git a/lib/trmnl/api/contracts/model.rb b/lib/trmnl/api/schemas/model.rb similarity index 98% rename from lib/trmnl/api/contracts/model.rb rename to lib/trmnl/api/schemas/model.rb index b34c9d4..1eaee20 100644 --- a/lib/trmnl/api/contracts/model.rb +++ b/lib/trmnl/api/schemas/model.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. Model = Dry::Schema.JSON do required(:data).array(:hash) do diff --git a/lib/trmnl/api/contracts/palette.rb b/lib/trmnl/api/schemas/palette.rb similarity index 95% rename from lib/trmnl/api/contracts/palette.rb rename to lib/trmnl/api/schemas/palette.rb index 376b975..62dbffc 100644 --- a/lib/trmnl/api/contracts/palette.rb +++ b/lib/trmnl/api/schemas/palette.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. Palette = Dry::Schema.JSON do required(:data).array(:hash) do diff --git a/lib/trmnl/api/contracts/recipe.rb b/lib/trmnl/api/schemas/recipe.rb similarity index 98% rename from lib/trmnl/api/contracts/recipe.rb rename to lib/trmnl/api/schemas/recipe.rb index cb9a88a..d7f2242 100644 --- a/lib/trmnl/api/contracts/recipe.rb +++ b/lib/trmnl/api/schemas/recipe.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. Recipe = Dry::Schema.JSON do required(:data).array(:hash) do diff --git a/lib/trmnl/api/contracts/recipes/author.rb b/lib/trmnl/api/schemas/recipes/author.rb similarity index 98% rename from lib/trmnl/api/contracts/recipes/author.rb rename to lib/trmnl/api/schemas/recipes/author.rb index 330e3ba..349e145 100644 --- a/lib/trmnl/api/contracts/recipes/author.rb +++ b/lib/trmnl/api/schemas/recipes/author.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas module Recipes # Validates API response. Author = Dry::Schema.JSON do diff --git a/lib/trmnl/api/contracts/setup.rb b/lib/trmnl/api/schemas/setup.rb similarity index 94% rename from lib/trmnl/api/contracts/setup.rb rename to lib/trmnl/api/schemas/setup.rb index 151bf54..593e0cd 100644 --- a/lib/trmnl/api/contracts/setup.rb +++ b/lib/trmnl/api/schemas/setup.rb @@ -4,7 +4,7 @@ require "dry/schema" module TRMNL module API - module Contracts + module Schemas # Validates API response. Setup = Dry::Schema.JSON do required(:api_key).filled :string diff --git a/spec/lib/trmnl/api/endpoints/category_spec.rb b/spec/lib/trmnl/api/endpoints/category_spec.rb index a8aca9b..0c80f47 100644 --- a/spec/lib/trmnl/api/endpoints/category_spec.rb +++ b/spec/lib/trmnl/api/endpoints/category_spec.rb @@ -59,7 +59,7 @@ RSpec.describe TRMNL::API::Endpoints::Category do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") 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 index 7a22521..23b3b5e 100644 --- a/spec/lib/trmnl/api/endpoints/current_screen_spec.rb +++ b/spec/lib/trmnl/api/endpoints/current_screen_spec.rb @@ -66,7 +66,7 @@ RSpec.describe TRMNL::API::Endpoints::CurrentScreen do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/endpoints/display_spec.rb b/spec/lib/trmnl/api/endpoints/display_spec.rb index 548d06f..237b07f 100644 --- a/spec/lib/trmnl/api/endpoints/display_spec.rb +++ b/spec/lib/trmnl/api/endpoints/display_spec.rb @@ -72,7 +72,7 @@ RSpec.describe TRMNL::API::Endpoints::Display do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/endpoints/firmware_spec.rb b/spec/lib/trmnl/api/endpoints/firmware_spec.rb index 7671787..f99e5e2 100644 --- a/spec/lib/trmnl/api/endpoints/firmware_spec.rb +++ b/spec/lib/trmnl/api/endpoints/firmware_spec.rb @@ -60,7 +60,7 @@ RSpec.describe TRMNL::API::Endpoints::Firmware do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/endpoints/ip_address_spec.rb b/spec/lib/trmnl/api/endpoints/ip_address_spec.rb index 9298646..54615c7 100644 --- a/spec/lib/trmnl/api/endpoints/ip_address_spec.rb +++ b/spec/lib/trmnl/api/endpoints/ip_address_spec.rb @@ -62,7 +62,7 @@ RSpec.describe TRMNL::API::Endpoints::IPAddress do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/endpoints/model_spec.rb b/spec/lib/trmnl/api/endpoints/model_spec.rb index 3870670..3c260e3 100644 --- a/spec/lib/trmnl/api/endpoints/model_spec.rb +++ b/spec/lib/trmnl/api/endpoints/model_spec.rb @@ -113,7 +113,7 @@ RSpec.describe TRMNL::API::Endpoints::Model do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/endpoints/palette_spec.rb b/spec/lib/trmnl/api/endpoints/palette_spec.rb index 85aaa6c..4dcf1a2 100644 --- a/spec/lib/trmnl/api/endpoints/palette_spec.rb +++ b/spec/lib/trmnl/api/endpoints/palette_spec.rb @@ -72,7 +72,7 @@ RSpec.describe TRMNL::API::Endpoints::Palette do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/endpoints/recipe_spec.rb b/spec/lib/trmnl/api/endpoints/recipe_spec.rb index 53f2084..6f5dade 100644 --- a/spec/lib/trmnl/api/endpoints/recipe_spec.rb +++ b/spec/lib/trmnl/api/endpoints/recipe_spec.rb @@ -110,7 +110,7 @@ RSpec.describe TRMNL::API::Endpoints::Recipe do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/endpoints/setup_spec.rb b/spec/lib/trmnl/api/endpoints/setup_spec.rb index 6bd6f8e..17c0d3a 100644 --- a/spec/lib/trmnl/api/endpoints/setup_spec.rb +++ b/spec/lib/trmnl/api/endpoints/setup_spec.rb @@ -66,7 +66,7 @@ RSpec.describe TRMNL::API::Endpoints::Setup do describe "#inspect" do it "has inspected attributes" do - expect(described_class.new.inspect).to match_inspection(contract: "Dry::Schema::JSON") + expect(described_class.new.inspect).to match_inspection(schema: "Dry::Schema::JSON") end end end diff --git a/spec/lib/trmnl/api/contracts/model_spec.rb b/spec/lib/trmnl/api/schemas/model_spec.rb similarity index 97% rename from spec/lib/trmnl/api/contracts/model_spec.rb rename to spec/lib/trmnl/api/schemas/model_spec.rb index 323667d..d9bd452 100644 --- a/spec/lib/trmnl/api/contracts/model_spec.rb +++ b/spec/lib/trmnl/api/schemas/model_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe TRMNL::API::Contracts::Model do +RSpec.describe TRMNL::API::Schemas::Model do subject(:contract) { described_class } let :payload do