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