From 70e1d247967cff23b5a0d61e57449b5a7a73fc4b Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Tue, 22 Apr 2025 09:29:38 -0600 Subject: [PATCH] Added configuration loader Necessary to load the configuration with preference for any defaults set via the environment. --- lib/trmnl/api/configuration/loader.rb | 26 +++++++++++++++ .../trmnl/api/configuration/loader_spec.rb | 32 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 lib/trmnl/api/configuration/loader.rb create mode 100644 spec/lib/trmnl/api/configuration/loader_spec.rb diff --git a/lib/trmnl/api/configuration/loader.rb b/lib/trmnl/api/configuration/loader.rb new file mode 100644 index 0000000..4ff0c1f --- /dev/null +++ b/lib/trmnl/api/configuration/loader.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module TRMNL + module API + module Configuration + # Loads configuration based on environment or falls back to defaults. + class Loader + def initialize model: Content, environment: ENV + @model = model + @environment = environment + end + + def call + model[ + content_type: environment.fetch("TRMNL_API_CONTENT_TYPE", "application/json"), + uri: environment.fetch("TRMNL_API_URI", "https://trmnl.app/api") + ] + end + + private + + attr_reader :model, :environment + end + end + end +end diff --git a/spec/lib/trmnl/api/configuration/loader_spec.rb b/spec/lib/trmnl/api/configuration/loader_spec.rb new file mode 100644 index 0000000..3f97d0c --- /dev/null +++ b/spec/lib/trmnl/api/configuration/loader_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe TRMNL::API::Configuration::Loader do + subject(:loader) { described_class.new environment: Hash.new } + + describe "#call" do + it "answers default configuration when environment is unset" do + expect(loader.call).to eq( + TRMNL::API::Configuration::Content[ + content_type: "application/json", + uri: "https://trmnl.app/api" + ] + ) + end + + it "answers custom configuration when environment is set" do + loader = described_class.new environment: { + "TRMNL_API_CONTENT_TYPE" => "application/xml", + "TRMNL_API_URI" => "https://api.trmnl.com" + } + + expect(loader.call).to eq( + TRMNL::API::Configuration::Content[ + content_type: "application/xml", + uri: "https://api.trmnl.com" + ] + ) + end + end +end