diff --git a/lib/trmnl/api/client.rb b/lib/trmnl/api/client.rb new file mode 100644 index 0000000..bf0f21e --- /dev/null +++ b/lib/trmnl/api/client.rb @@ -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 diff --git a/spec/lib/trmnl/api/client_spec.rb b/spec/lib/trmnl/api/client_spec.rb new file mode 100644 index 0000000..4de76d2 --- /dev/null +++ b/spec/lib/trmnl/api/client_spec.rb @@ -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