mirror of
https://github.com/usetrmnl/trmnl-api.git
synced 2026-04-29 13:35:13 -07:00
Added configuration loader
Necessary to load the configuration with preference for any defaults set via the environment.
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user