mirror of
https://github.com/usetrmnl/trmnl-api.git
synced 2026-04-29 13:35:13 -07:00
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
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user