Skip to content

Commit f1b7ca0

Browse files
committed
Dry API modules loading
1 parent b75f400 commit f1b7ca0

8 files changed

Lines changed: 24 additions & 28 deletions

File tree

lib/netbox_client_ruby.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ module NetboxClientRuby
6868
setting :ssl_options, default: { verify: true }
6969
end
7070

71+
def self.load_collection(collection, method_name, class_name)
72+
collection.define_method(method_name) { class_name.new }
73+
collection.__send__(:module_function, method_name)
74+
end
75+
76+
def self.load_entity(entity, method_name, class_name)
77+
entity.define_method(method_name) { |id| class_name.new id }
78+
entity.__send__(:module_function, method_name)
79+
end
80+
7181
def self.circuits
7282
NetboxClientRuby::Circuits
7383
end

lib/netbox_client_ruby/api/circuits.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ module Circuits
88
circuit_types: CircuitTypes,
99
circuit_terminations: CircuitTerminations,
1010
}.each_pair do |method_name, class_name|
11-
define_method(method_name) { class_name.new }
12-
module_function(method_name)
11+
NetboxClientRuby.load_collection(self, method_name, class_name)
1312
end
1413

1514
{
@@ -18,8 +17,7 @@ module Circuits
1817
circuit_type: CircuitType,
1918
circuit_termination: CircuitTermination,
2019
}.each_pair do |method_name, class_name|
21-
define_method(method_name) { |id| class_name.new id }
22-
module_function(method_name)
20+
NetboxClientRuby.load_entity(self, method_name, class_name)
2321
end
2422
end
2523
end

lib/netbox_client_ruby/api/dcim.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ module DCIM
2626
sites: Sites,
2727
virtual_chassis_list: VirtualChassisList,
2828
}.each_pair do |method_name, class_name|
29-
define_method(method_name) { class_name.new }
30-
module_function(method_name)
29+
NetboxClientRuby.load_collection(self, method_name, class_name)
3130
end
3231

3332
{
@@ -54,8 +53,7 @@ module DCIM
5453
site: Site,
5554
virtual_chassis: VirtualChassis,
5655
}.each_pair do |method_name, class_name|
57-
define_method(method_name) { |id| class_name.new id }
58-
module_function(method_name)
56+
NetboxClientRuby.load_entity(self, method_name, class_name)
5957
end
6058
end
6159
end

lib/netbox_client_ruby/api/extras.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ module Extras
77
journal_entries: JournalEntries,
88
tags: Tags,
99
}.each_pair do |method_name, class_name|
10-
define_method(method_name) { class_name.new }
11-
module_function(method_name)
10+
NetboxClientRuby.load_collection(self, method_name, class_name)
1211
end
1312

1413
{
1514
config_context: ConfigContext,
1615
journal_entry: JournalEntry,
1716
tag: Tag,
1817
}.each_pair do |method_name, class_name|
19-
define_method(method_name) { |id| class_name.new id }
20-
module_function(method_name)
18+
NetboxClientRuby.load_entity(self, method_name, class_name)
2119
end
2220
end
2321
end

lib/netbox_client_ruby/api/ipam.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ module IPAM
1414
vlan_groups: VlanGroups,
1515
vrfs: Vrfs,
1616
}.each_pair do |method_name, class_name|
17-
define_method(method_name) { class_name.new }
18-
module_function(method_name)
17+
NetboxClientRuby.load_collection(self, method_name, class_name)
1918
end
2019

2120
{
@@ -30,8 +29,7 @@ module IPAM
3029
vlan_group: VlanGroup,
3130
vrf: Vrf,
3231
}.each_pair do |method_name, class_name|
33-
define_method(method_name) { |id| class_name.new id }
34-
module_function(method_name)
32+
NetboxClientRuby.load_entity(self, method_name, class_name)
3533
end
3634
end
3735
end

lib/netbox_client_ruby/api/secrets.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ module Secrets
88
generate_rsa_key_pair: RSAKeyPair,
99
get_session_key: SessionKey,
1010
}.each_pair do |method_name, class_name|
11-
define_method(method_name) { class_name.new }
12-
module_function(method_name)
11+
NetboxClientRuby.load_collection(self, method_name, class_name)
1312
end
1413

1514
{
1615
secret_role: SecretRole,
1716
secret: Secret,
1817
}.each_pair do |method_name, class_name|
19-
define_method(method_name) { |id| class_name.new id }
20-
module_function(method_name)
18+
NetboxClientRuby.load_entity(self, method_name, class_name)
2119
end
2220

2321
def session_key=(session_key)

lib/netbox_client_ruby/api/tenancy.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ module Tenancy
88
contacts: Contacts,
99
contact_groups: ContactGroups,
1010
}.each_pair do |method_name, class_name|
11-
define_method(method_name) { class_name.new }
12-
module_function(method_name)
11+
NetboxClientRuby.load_collection(self, method_name, class_name)
1312
end
1413

1514
{
@@ -18,8 +17,7 @@ module Tenancy
1817
contact: Contact,
1918
contact_group: ContactGroup,
2019
}.each_pair do |method_name, class_name|
21-
define_method(method_name) { |id| class_name.new id }
22-
module_function(method_name)
20+
NetboxClientRuby.load_entity(self, method_name, class_name)
2321
end
2422
end
2523
end

lib/netbox_client_ruby/api/virtualization.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ module Virtualization
99
virtual_machines: VirtualMachines,
1010
interfaces: Interfaces,
1111
}.each_pair do |method_name, class_name|
12-
define_method(method_name) { class_name.new }
13-
module_function(method_name)
12+
NetboxClientRuby.load_collection(self, method_name, class_name)
1413
end
1514

1615
{
@@ -20,8 +19,7 @@ module Virtualization
2019
virtual_machine: VirtualMachine,
2120
interface: Interface,
2221
}.each_pair do |method_name, class_name|
23-
define_method(method_name) { |id| class_name.new id }
24-
module_function(method_name)
22+
NetboxClientRuby.load_entity(self, method_name, class_name)
2523
end
2624
end
2725
end

0 commit comments

Comments
 (0)