From d4209e5f011f45fc1ce40ec3fae5055321fd14aa Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Tue, 13 Jan 2026 15:02:41 -0700 Subject: [PATCH] Added recipe author model Necessary to model a recipe author. A `Struct` is used because some keys are optional. This injects the `LocaleReducer` in order to convert all description locales to a hash for quick lookup rather than use the redundant keys provided by the API. Will soon be used by the recipe model (soon to be added). Milestone: minor --- lib/trmnl/api/models/recipes/author.rb | 28 +++++++++++++++++ .../trmnl/api/models/recipes/author_spec.rb | 31 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lib/trmnl/api/models/recipes/author.rb create mode 100644 spec/lib/trmnl/api/models/recipes/author_spec.rb diff --git a/lib/trmnl/api/models/recipes/author.rb b/lib/trmnl/api/models/recipes/author.rb new file mode 100644 index 0000000..b8b4139 --- /dev/null +++ b/lib/trmnl/api/models/recipes/author.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module TRMNL + module API + module Models + module Recipes + # Models the author of the API response. + Author = Struct.new( + :keyname, + :name, + :field_type, + :category, + :description, + :description_locales, + :email_address, + :learn_more_url, + :github_url, + :youtube_url + ) do + def self.for(locale_reducer: LocaleReducer, **attributes) + description_locales = locale_reducer.call attributes + new(description_locales:, **attributes) + end + end + end + end + end +end diff --git a/spec/lib/trmnl/api/models/recipes/author_spec.rb b/spec/lib/trmnl/api/models/recipes/author_spec.rb new file mode 100644 index 0000000..b826422 --- /dev/null +++ b/spec/lib/trmnl/api/models/recipes/author_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe TRMNL::API::Models::Recipes::Author do + subject(:author) { described_class.new } + + describe ".for" do + let :attributes do + { + name: "test", + "description-de": "german", + "description-nl": "dutch", + "description-fr": "french" + } + end + + it "transforms description locales to hash by key only" do + expect(described_class.for(**attributes)).to eq( + described_class[ + name: "test", + description_locales: { + "de" => "german", + "nl" => "dutch", + "fr" => "french" + } + ] + ) + end + end +end