mirror of
https://github.com/usetrmnl/trmnl-api.git
synced 2026-04-29 13:35:13 -07:00
Added setup endpoint
Necessary to handle the request and response of the setup API endpoint.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "pipeable"
|
||||
|
||||
module TRMNL
|
||||
module API
|
||||
module Endpoints
|
||||
# Handles API request/response.
|
||||
class Setup
|
||||
include Dependencies[:client, contract: "contracts.setup", model: "models.setup"]
|
||||
include Pipeable
|
||||
|
||||
def call id:
|
||||
pipe client.get("setup", headers: {"ID" => id}),
|
||||
try(:parse, catch: JSON::ParserError),
|
||||
validate(contract, as: :to_h),
|
||||
to(model, :for)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,66 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
RSpec.describe TRMNL::API::Endpoints::Setup do
|
||||
subject(:endpoint) { described_class.new client: }
|
||||
|
||||
include_context "with application dependencies"
|
||||
|
||||
let(:client) { TRMNL::API::Client.new http: }
|
||||
|
||||
describe "#call" do
|
||||
context "with success" do
|
||||
let :http do
|
||||
HTTP::Fake::Client.new do
|
||||
get "/api/setup" do
|
||||
headers["Content-Type"] = "application/json"
|
||||
status 200
|
||||
|
||||
<<~JSON
|
||||
{
|
||||
"api_key": "abc",
|
||||
"friendly_id": "10CBAF",
|
||||
"image_url": "https://usetrmnl.com/images/logo.bmp",
|
||||
"message": "Welcome!"
|
||||
}
|
||||
JSON
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "answers record" do
|
||||
result = endpoint.call id: "ABC"
|
||||
|
||||
expect(result).to be_success(
|
||||
TRMNL::API::Models::Setup[
|
||||
api_key: "abc",
|
||||
friendly_id: "10CBAF",
|
||||
image_url: "https://usetrmnl.com/images/logo.bmp",
|
||||
message: "Welcome!"
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "with failure" do
|
||||
let :http do
|
||||
HTTP::Fake::Client.new do
|
||||
get "/api/setup" do
|
||||
headers["Content-Type"] = "application/json"
|
||||
status 404
|
||||
|
||||
<<~JSON
|
||||
{"error": "Danger!"}
|
||||
JSON
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "answers failure" do
|
||||
result = described_class.new(client:).call id: "ABC"
|
||||
expect(result).to match(Failure(be_a(HTTP::Response)))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user