From f54458f18f8e02abf9b83d77d459d83ae627103d Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Thu, 11 Dec 2025 11:09:07 -0700 Subject: [PATCH] Added IP address model Necessary to model the response. RuboCop is updated to ignore the variable name issues since this stems from the API design. The model translates the keys to prevent this design flaw from permeating further downstream at least. Milestone: minor --- .config/rubocop/config.yml | 3 +++ lib/trmnl/api/container.rb | 1 + lib/trmnl/api/models/ip_address.rb | 14 ++++++++++++++ spec/lib/trmnl/api/models/ip_address_spec.rb | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 lib/trmnl/api/models/ip_address.rb create mode 100644 spec/lib/trmnl/api/models/ip_address_spec.rb diff --git a/.config/rubocop/config.yml b/.config/rubocop/config.yml index 9530268..d19cf14 100644 --- a/.config/rubocop/config.yml +++ b/.config/rubocop/config.yml @@ -7,6 +7,9 @@ Metrics/ParameterLists: Naming/VariableNumber: Exclude: - lib/trmnl/api/contracts/ip_address.rb + - lib/trmnl/api/models/ip_address.rb + - spec/lib/trmnl/api/models/ip_address_spec.rb Style/IpAddresses: Exclude: - spec/lib/trmnl/api/types_spec.rb + - spec/lib/trmnl/api/models/ip_address_spec.rb diff --git a/lib/trmnl/api/container.rb b/lib/trmnl/api/container.rb index 5c0774a..43d2fea 100644 --- a/lib/trmnl/api/container.rb +++ b/lib/trmnl/api/container.rb @@ -33,6 +33,7 @@ module TRMNL register :current_screen, Models::CurrentScreen register :display, Models::Display register :firmware, Models::Firmware + register :ip_address, Models::IPAddress register :model, Models::Model register :setup, Models::Setup end diff --git a/lib/trmnl/api/models/ip_address.rb b/lib/trmnl/api/models/ip_address.rb new file mode 100644 index 0000000..0a1250e --- /dev/null +++ b/lib/trmnl/api/models/ip_address.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module TRMNL + module API + module Models + # IPs API response. + IPAddress = Data.define :version_4, :version_6 do + def self.for attributes + new(**attributes.transform_keys(ipv4: :version_4, ipv6: :version_6)) + end + end + end + end +end diff --git a/spec/lib/trmnl/api/models/ip_address_spec.rb b/spec/lib/trmnl/api/models/ip_address_spec.rb new file mode 100644 index 0000000..aca1606 --- /dev/null +++ b/spec/lib/trmnl/api/models/ip_address_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe TRMNL::API::Models::IPAddress do + describe ".for" do + it "answers record for attributes" do + attributes = {ipv4: ["192.168.1.10"], ipv6: ["1a00:100:100:1000::1"]} + + expect(described_class.for(attributes)).to eq( + described_class[ + version_4: ["192.168.1.10"], + version_6: ["1a00:100:100:1000::1"] + ] + ) + end + end +end