Added configuration loader

Necessary to load the configuration with preference for any defaults set via the environment.
This commit is contained in:
Brooke Kuhlmann
2025-04-22 14:24:32 -06:00
parent bbd82f4d3c
commit 70e1d24796
2 changed files with 58 additions and 0 deletions
+26
View File
@@ -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
@@ -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