Added client

Necessary to provide a single object with access to all endpoints.

Milestone: minor
This commit is contained in:
Brooke Kuhlmann
2025-05-05 17:00:55 -06:00
parent 1c6e8f84dd
commit 386a36e829
2 changed files with 111 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
# frozen_string_literal: true
module TRMNL
module API
# Provides the primary client for making API requests.
class Client
include Dependencies[:settings]
include Endpoints::Dependencies[
endpoint_current_screen: :current_screen,
endpoint_display: :display,
endpoint_firmware: :firmware,
endpoint_log: :log,
endpoint_setup: :setup
]
def initialize(**)
super
yield settings if block_given?
end
def current_screen(**) = endpoint_current_screen.call(**)
def display(**) = endpoint_display.call(**)
def firmware = endpoint_firmware.call
def log(**) = endpoint_log.call(**)
def setup(**) = endpoint_setup.call(**)
end
end
end
+78
View File
@@ -0,0 +1,78 @@
# frozen_string_literal: true
require "spec_helper"
RSpec.describe TRMNL::API::Client do
subject(:client) { described_class.new }
include_context "with application dependencies"
describe "#initialize" do
it "answers original settings without block" do
client
expect(settings).to eq(
TRMNL::API::Configuration::Content[
content_type: "application/json",
uri: "https://trmnl.app/api"
]
)
end
it "modifies settings with block" do
described_class.new { |settings| settings.uri = "https://api.test.io" }
expect(settings).to eq(
TRMNL::API::Configuration::Content[
content_type: "application/json",
uri: "https://api.test.io"
]
)
end
end
describe "#current_screen" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::CurrentScreen }
it "messages endpoint" do
client = described_class.new endpoint_current_screen: endpoint
expect(client.current_screen(token: "abc")).to have_received(:call).with(token: "abc")
end
end
describe "#display" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::Display }
it "messages endpoint" do
client = described_class.new endpoint_display: endpoint
expect(client.display(token: "abc")).to have_received(:call).with(token: "abc")
end
end
describe "#firmware" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::Firmware }
it "messages endpoint" do
client = described_class.new endpoint_firmware: endpoint
expect(client.firmware).to have_received(:call)
end
end
describe "#log" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::Log }
it "messages endpoint" do
client = described_class.new endpoint_log: endpoint
expect(client.log(token: "abc", log: {})).to have_received(:call).with(token: "abc", log: {})
end
end
describe "#setup" do
let(:endpoint) { instance_spy TRMNL::API::Endpoints::Setup }
it "messages endpoint" do
client = described_class.new endpoint_setup: endpoint
expect(client.setup(id: "abc")).to have_received(:call).with(id: "abc")
end
end
end