From 7799ad706c67db4b2ed9f06ce64da04a77fc5f16 Mon Sep 17 00:00:00 2001 From: Letam Patrick-Bienwi Date: Tue, 13 Jan 2026 15:19:50 +0000 Subject: [PATCH 1/3] Added documents for location functionality --- lib/netbox_client_ruby/api/dcim/location.rb | 12 ++ lib/netbox_client_ruby/api/dcim/locations.rb | 18 +++ spec/fixtures/dcim/location_1.json | 6 + spec/fixtures/dcim/locations.json | 13 ++ .../api/dcim/location_spec.rb | 138 ++++++++++++++++++ .../api/dcim/locations_spec.rb | 57 ++++++++ 6 files changed, 244 insertions(+) create mode 100644 lib/netbox_client_ruby/api/dcim/location.rb create mode 100644 lib/netbox_client_ruby/api/dcim/locations.rb create mode 100644 spec/fixtures/dcim/location_1.json create mode 100644 spec/fixtures/dcim/locations.json create mode 100644 spec/netbox_client_ruby/api/dcim/location_spec.rb create mode 100644 spec/netbox_client_ruby/api/dcim/locations_spec.rb diff --git a/lib/netbox_client_ruby/api/dcim/location.rb b/lib/netbox_client_ruby/api/dcim/location.rb new file mode 100644 index 0000000..4c357ef --- /dev/null +++ b/lib/netbox_client_ruby/api/dcim/location.rb @@ -0,0 +1,12 @@ +module NetboxClientRuby + module DCIM + class Location + include Entity + + id id: :id + deletable true + path 'dcim/locations/:id/' + creation_path 'dcim/locations/' + end + end +end \ No newline at end of file diff --git a/lib/netbox_client_ruby/api/dcim/locations.rb b/lib/netbox_client_ruby/api/dcim/locations.rb new file mode 100644 index 0000000..cf3dd0f --- /dev/null +++ b/lib/netbox_client_ruby/api/dcim/locations.rb @@ -0,0 +1,18 @@ +module NetboxClientRuby + module DCIM + class Locations + include Entities + + path 'dcim/locations/' + data_key 'results' + count_key 'count' + entity_creator :entity_creator + + private + + def entity_creator(raw_entity) + Location.new raw_entity['id'] + end + end + end +end \ No newline at end of file diff --git a/spec/fixtures/dcim/location_1.json b/spec/fixtures/dcim/location_1.json new file mode 100644 index 0000000..c2f2e49 --- /dev/null +++ b/spec/fixtures/dcim/location_1.json @@ -0,0 +1,6 @@ +{ + "id": 1, + "name": "1", + "slug": "1", + "description": "The location of my office" +} \ No newline at end of file diff --git a/spec/fixtures/dcim/locations.json b/spec/fixtures/dcim/locations.json new file mode 100644 index 0000000..9b0c579 --- /dev/null +++ b/spec/fixtures/dcim/locations.json @@ -0,0 +1,13 @@ +{ + "count": 1, + "next": null, + "previous": null, + "results": [ + { + "id": 1, + "name": "1", + "slug": "1", + "description": "The location of my office" + } + ] +} \ No newline at end of file diff --git a/spec/netbox_client_ruby/api/dcim/location_spec.rb b/spec/netbox_client_ruby/api/dcim/location_spec.rb new file mode 100644 index 0000000..d5f6feb --- /dev/null +++ b/spec/netbox_client_ruby/api/dcim/location_spec.rb @@ -0,0 +1,138 @@ + + +require 'spec_helper' + +RSpec.describe NetboxClientRuby::DCIM::Location, faraday_stub: true do + subject { sut.new entity_id } + + let(:entity_id) { 1 } + let(:expected_name) { '1' } + let(:sut) { described_class } + let(:base_url) { '/api/dcim/locations/' } + + let(:request_url) { "#{base_url}#{entity_id}/" } + let(:response) { File.read("spec/fixtures/dcim/location_#{entity_id}.json") } + + describe '#id' do + it 'shall be the expected id' do + expect(subject.id).to eq(entity_id) + end + end + + describe '#name' do + it 'fetches the data' do + expect(faraday).to receive(:get).and_call_original + + expect(subject.name).to_not be_nil + end + + it 'shall be the expected name' do + expect(subject.name).to eq(expected_name) + end + end + + describe '.delete' do + let(:request_method) { :delete } + let(:response_status) { 204 } + let(:response) { nil } + + it 'deletes the object' do + expect(faraday).to receive(request_method).and_call_original + subject.delete + end + end + + describe '.update' do + let(:request_method) { :patch } + let(:request_params) { { 'name' => 'noob' } } + + it 'updates the object' do + expect(faraday).to receive(request_method).and_call_original + expect(subject.update(name: 'noob').name).to eq(expected_name) + end + end + + describe '.reload' do + it 'reloads the object' do + expect(faraday).to receive(request_method).twice.and_call_original + + subject.reload + subject.reload + end + end + + describe '.save' do + let(:name) { 'foobar' } + let(:slug) { name } + let(:request_params) { { 'name' => name, 'slug' => slug } } + + context 'update' do + subject do + region = sut.new entity_id + region.name = name + region.slug = slug + region + end + + let(:request_method) { :patch } + + it 'does not call PATCH until save is called' do + expect(faraday).to_not receive(request_method) + expect(faraday).to_not receive(:get) + + expect(subject.name).to eq(name) + expect(subject.slug).to eq(slug) + end + + it 'calls PATCH when save is called' do + expect(faraday).to receive(request_method).and_call_original + + expect(subject.save).to be(subject) + end + + it 'Reads the answer from the PATCH answer' do + expect(faraday).to receive(request_method).and_call_original + + subject.save + expect(subject.name).to eq(expected_name) + expect(subject.slug).to eq(expected_name) + end + end + + context 'create' do + subject do + region = sut.new + region.name = name + region.slug = slug + region + end + + let(:request_method) { :post } + let(:request_url) { base_url } + + it 'does not POST until save is called' do + expect(faraday).to_not receive(request_method) + expect(faraday).to_not receive(:get) + + expect(subject.name).to eq(name) + expect(subject.slug).to eq(slug) + end + + it 'POSTs the data upon a call of save' do + expect(faraday).to receive(request_method).and_call_original + + expect(subject.save).to be(subject) + end + + it 'Reads the answer from the POST' do + expect(faraday).to receive(request_method).and_call_original + + subject.save + + expect(subject.id).to be(1) + expect(subject.name).to eq(expected_name) + expect(subject.slug).to eq(expected_name) + end + end + end +end \ No newline at end of file diff --git a/spec/netbox_client_ruby/api/dcim/locations_spec.rb b/spec/netbox_client_ruby/api/dcim/locations_spec.rb new file mode 100644 index 0000000..3eb2cf8 --- /dev/null +++ b/spec/netbox_client_ruby/api/dcim/locations_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +RSpec.describe NetboxClientRuby::DCIM::Locations, faraday_stub: true do + let(:expected_length) { 1 } + let(:singular_type) { NetboxClientRuby::DCIM::Location } + + let(:response) { File.read('spec/fixtures/dcim/locations.json') } + let(:request_url) { '/api/dcim/locations/' } + let(:request_url_params) do + { limit: NetboxClientRuby.config.netbox.pagination.default_limit } + end + + context 'unpaged fetch' do + describe '#length' do + it 'shall be the expected length' do + expect(subject.length).to be expected_length + end + end + + describe '#total' do + it 'shall be the expected total' do + expect(subject.total).to be expected_length + end + end + end + + describe '#reload' do + it 'fetches the correct data' do + expect(faraday).to receive(:get).and_call_original + subject.reload + end + + it 'caches the data' do + expect(faraday).to receive(:get).and_call_original + subject.total + subject.total + end + + it 'reloads the data' do + expect(faraday).to receive(:get).twice.and_call_original + subject.reload + subject.reload + end + end + + describe '#as_array' do + it 'return the correct amount' do + expect(subject.to_a.length).to be expected_length + end + + it 'returns Location instances' do + subject.to_a.each do |element| + expect(element).to be_a singular_type + end + end + end +end \ No newline at end of file From 10ee1e186ed9d83af006565908ea25a453b13d23 Mon Sep 17 00:00:00 2001 From: Letam Patrick-Bienwi Date: Tue, 13 Jan 2026 16:17:05 +0000 Subject: [PATCH 2/3] Added locations to dcim --- README.md | 1 + lib/netbox_client_ruby/api/dcim.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index f983e36..e7582bb 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen * Front Ports: `NetboxClientRuby.dcim.front_ports` * Interfaces: `NetboxClientRuby.dcim.interfaces` * Interface Connections: `NetboxClientRuby.dcim.interface_connections` + * Locations: `NetboxClientRuby.dcim.locations` * Manufacturers: `NetboxClientRuby.dcim.manufacturers` * Platforms: `NetboxClientRuby.dcim.platforms` * Power Connections: `NetboxClientRuby.dcim.power_connections` diff --git a/lib/netbox_client_ruby/api/dcim.rb b/lib/netbox_client_ruby/api/dcim.rb index a3cbe86..a64c61d 100644 --- a/lib/netbox_client_ruby/api/dcim.rb +++ b/lib/netbox_client_ruby/api/dcim.rb @@ -13,6 +13,7 @@ module DCIM interfaces: Interfaces, interface_connections: InterfaceConnections, inventory_items: InventoryItems, + locations: Locations, mac_addresses: MacAddresses, manufacturers: Manufacturers, platforms: Platforms, @@ -42,6 +43,7 @@ module DCIM interface: Interface, interface_connection: InterfaceConnection, inventory_item: InventoryItem, + location: Location, mac_address: MacAddress, manufacturer: Manufacturer, platform: Platform, From 9faedeee298e1fb99a605e1a2bf3726dcacc92dc Mon Sep 17 00:00:00 2001 From: Letam Patrick-Bienwi Date: Tue, 13 Jan 2026 16:37:35 +0000 Subject: [PATCH 3/3] Adjusted files for rubocop --- lib/netbox_client_ruby/api/dcim/location.rb | 4 +++- lib/netbox_client_ruby/api/dcim/locations.rb | 4 +++- spec/netbox_client_ruby/api/dcim/location_spec.rb | 4 ++-- spec/netbox_client_ruby/api/dcim/locations_spec.rb | 4 +++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/netbox_client_ruby/api/dcim/location.rb b/lib/netbox_client_ruby/api/dcim/location.rb index 4c357ef..576da1f 100644 --- a/lib/netbox_client_ruby/api/dcim/location.rb +++ b/lib/netbox_client_ruby/api/dcim/location.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module NetboxClientRuby module DCIM class Location @@ -9,4 +11,4 @@ class Location creation_path 'dcim/locations/' end end -end \ No newline at end of file +end diff --git a/lib/netbox_client_ruby/api/dcim/locations.rb b/lib/netbox_client_ruby/api/dcim/locations.rb index cf3dd0f..aa00c32 100644 --- a/lib/netbox_client_ruby/api/dcim/locations.rb +++ b/lib/netbox_client_ruby/api/dcim/locations.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module NetboxClientRuby module DCIM class Locations @@ -15,4 +17,4 @@ def entity_creator(raw_entity) end end end -end \ No newline at end of file +end diff --git a/spec/netbox_client_ruby/api/dcim/location_spec.rb b/spec/netbox_client_ruby/api/dcim/location_spec.rb index d5f6feb..a262bf0 100644 --- a/spec/netbox_client_ruby/api/dcim/location_spec.rb +++ b/spec/netbox_client_ruby/api/dcim/location_spec.rb @@ -1,4 +1,4 @@ - +# frozen_string_literal: true require 'spec_helper' @@ -135,4 +135,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/netbox_client_ruby/api/dcim/locations_spec.rb b/spec/netbox_client_ruby/api/dcim/locations_spec.rb index 3eb2cf8..84338e7 100644 --- a/spec/netbox_client_ruby/api/dcim/locations_spec.rb +++ b/spec/netbox_client_ruby/api/dcim/locations_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' RSpec.describe NetboxClientRuby::DCIM::Locations, faraday_stub: true do @@ -54,4 +56,4 @@ end end end -end \ No newline at end of file +end