Skip to content

Commit fecdd0f

Browse files
authored
feat: add NetboxClientRuby::DCIM::Cable(s)
1 parent 25aece0 commit fecdd0f

File tree

7 files changed

+310
-0
lines changed

7 files changed

+310
-0
lines changed

lib/netbox_client_ruby/api/dcim.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module NetboxClientRuby
44
module DCIM
55
{
6+
cables: Cables,
67
console_connections: ConsoleConnections,
78
console_ports: ConsolePorts,
89
console_server_ports: ConsoleServerPorts,
@@ -33,6 +34,7 @@ module DCIM
3334
end
3435

3536
{
37+
cable: Cable,
3638
console_connection: ConsoleConnection,
3739
console_port: ConsolePort,
3840
console_server_port: ConsoleServerPort,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module NetboxClientRuby
4+
module DCIM
5+
class Cable
6+
include Entity
7+
8+
id id: :id
9+
deletable true
10+
path 'dcim/cables/:id/'
11+
creation_path 'dcim/cables/'
12+
end
13+
end
14+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module NetboxClientRuby
4+
module DCIM
5+
class Cables
6+
include Entities
7+
8+
path 'dcim/cables/'
9+
data_key 'results'
10+
count_key 'count'
11+
entity_creator :entity_creator
12+
13+
private
14+
15+
def entity_creator(raw_entity)
16+
Cable.new raw_entity['id']
17+
end
18+
end
19+
end
20+
end

spec/fixtures/dcim/cable_1.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"id": 1,
3+
"display": "#1",
4+
"type": "cat5",
5+
"a_terminations": [],
6+
"b_terminations": [],
7+
"status": {
8+
"value": "connected",
9+
"label": "Connected"
10+
},
11+
"tenant": null,
12+
"label": null,
13+
"color": null,
14+
"length": null,
15+
"length_unit": null,
16+
"description": null,
17+
"comments": null,
18+
"tags": [],
19+
"custom_fields": {}
20+
}

spec/fixtures/dcim/cables.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"count": 3,
3+
"next": null,
4+
"previous": null,
5+
"results": [
6+
{
7+
"id": 1,
8+
"display": "#1",
9+
"type": "cat5",
10+
"a_terminations": [],
11+
"b_terminations": [],
12+
"status": {
13+
"value": "connected",
14+
"label": "Connected"
15+
},
16+
"tenant": null,
17+
"label": null,
18+
"color": null,
19+
"length": null,
20+
"length_unit": null,
21+
"description": null,
22+
"comments": null,
23+
"tags": [],
24+
"custom_fields": {}
25+
},
26+
{
27+
"id": 2,
28+
"display": "#2",
29+
"type": "cat5",
30+
"a_terminations": [],
31+
"b_terminations": [],
32+
"status": {
33+
"value": "connected",
34+
"label": "Connected"
35+
},
36+
"tenant": null,
37+
"label": null,
38+
"color": null,
39+
"length": null,
40+
"length_unit": null,
41+
"description": null,
42+
"comments": null,
43+
"tags": [],
44+
"custom_fields": {}
45+
},
46+
{
47+
"id": 3,
48+
"display": "#3",
49+
"type": "cat5",
50+
"a_terminations": [],
51+
"b_terminations": [],
52+
"status": {
53+
"value": "connected",
54+
"label": "Connected"
55+
},
56+
"tenant": null,
57+
"label": null,
58+
"color": null,
59+
"length": null,
60+
"length_unit": null,
61+
"description": null,
62+
"comments": null,
63+
"tags": [],
64+
"custom_fields": {}
65+
}
66+
]
67+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe NetboxClientRuby::DCIM::Cable, faraday_stub: true do
6+
subject { sut.new entity_id }
7+
8+
let(:entity_id) { 1 }
9+
let(:expected_type) { 'cat5' }
10+
let(:sut) { described_class }
11+
let(:base_url) { '/api/dcim/cables/' }
12+
13+
let(:request_url) { "#{base_url}#{entity_id}/" }
14+
let(:response) { File.read("spec/fixtures/dcim/cable_#{entity_id}.json") }
15+
16+
describe '#id' do
17+
it 'shall be the expected id' do
18+
expect(subject.id).to eq(entity_id)
19+
end
20+
end
21+
22+
describe '#type' do
23+
it 'fetches the data' do
24+
allow(faraday).to receive(:get).and_call_original
25+
26+
expect(subject.type).to_not be_nil
27+
end
28+
29+
it 'shall be the expected type' do
30+
expect(subject.type).to eq(expected_type)
31+
end
32+
end
33+
34+
describe '.delete' do
35+
let(:request_method) { :delete }
36+
let(:response_status) { 204 }
37+
let(:response) { nil }
38+
39+
it 'deletes the object' do
40+
expect(faraday).to receive(request_method).and_call_original
41+
subject.delete
42+
end
43+
end
44+
45+
describe '.update' do
46+
let(:request_method) { :patch }
47+
let(:request_params) { { 'type' => 'coaxial' } }
48+
49+
it 'updates the object' do
50+
expect(faraday).to receive(request_method).and_call_original
51+
expect(subject.update(type: 'coaxial').type).to eq(expected_type)
52+
end
53+
end
54+
55+
describe '.reload' do
56+
it 'reloads the object' do
57+
expect(faraday).to receive(request_method).twice.and_call_original
58+
59+
subject.reload
60+
subject.reload
61+
end
62+
end
63+
64+
describe '.save' do
65+
let(:type) { 'coaxial' }
66+
let(:request_params) { { 'type' => type } }
67+
68+
context 'update' do
69+
subject do
70+
cable = sut.new entity_id
71+
cable.type = type
72+
cable
73+
end
74+
75+
let(:request_method) { :patch }
76+
77+
it 'does not call PATCH until save is called' do
78+
expect(faraday).to_not receive(request_method)
79+
expect(faraday).to_not receive(:get)
80+
81+
expect(subject.type).to eq(type)
82+
end
83+
84+
it 'calls PATCH when save is called' do
85+
expect(faraday).to receive(request_method).and_call_original
86+
87+
expect(subject.save).to be(subject)
88+
end
89+
90+
it 'Reads the answer from the PATCH answer' do
91+
expect(faraday).to receive(request_method).and_call_original
92+
93+
subject.save
94+
expect(subject.type).to eq(expected_type)
95+
end
96+
end
97+
98+
context 'create' do
99+
subject do
100+
cable = sut.new
101+
cable.type = type
102+
cable
103+
end
104+
105+
let(:request_method) { :post }
106+
let(:request_url) { base_url }
107+
108+
it 'does not POST until save is called' do
109+
expect(faraday).to_not receive(request_method)
110+
expect(faraday).to_not receive(:get)
111+
112+
expect(subject.type).to eq(type)
113+
end
114+
115+
it 'POSTs the data upon a call of save' do
116+
expect(faraday).to receive(request_method).and_call_original
117+
118+
expect(subject.save).to be(subject)
119+
end
120+
121+
it 'Reads the answer from the POST' do
122+
expect(faraday).to receive(request_method).and_call_original
123+
124+
subject.save
125+
126+
expect(subject.id).to be(1)
127+
expect(subject.type).to eq(expected_type)
128+
end
129+
end
130+
end
131+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe NetboxClientRuby::DCIM::Cables, faraday_stub: true do
6+
let(:expected_length) { 3 }
7+
let(:singular_type) { NetboxClientRuby::DCIM::Cable }
8+
9+
let(:response) { File.read('spec/fixtures/dcim/cables.json') }
10+
let(:request_url) { '/api/dcim/cables/' }
11+
12+
context 'unpaged fetch' do
13+
describe '#length' do
14+
it 'shall be the expected length' do
15+
expect(subject.length).to be expected_length
16+
end
17+
end
18+
19+
describe '#total' do
20+
it 'shall be the expected total' do
21+
expect(subject.total).to be expected_length
22+
end
23+
end
24+
25+
describe '#reload' do
26+
it 'fetches the correct data' do
27+
expect(faraday).to receive(:get).and_call_original
28+
subject.reload
29+
end
30+
31+
it 'caches the data' do
32+
expect(faraday).to receive(:get).and_call_original
33+
subject.total
34+
subject.total
35+
end
36+
37+
it 'reloads the data' do
38+
expect(faraday).to receive(:get).twice.and_call_original
39+
subject.reload
40+
subject.reload
41+
end
42+
end
43+
44+
describe '#as_array' do
45+
it 'returns the correct amount' do
46+
expect(subject.to_a.length).to be expected_length
47+
end
48+
49+
it 'returns Cable instances' do
50+
subject.to_a.each do |element|
51+
expect(element).to be_a singular_type
52+
end
53+
end
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)