Module: NetboxClientRuby::Entities

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Communication

#connection, #hash_to_object, #response

Class Method Details

.included(other_klass) ⇒ Object



8
9
10
# File 'lib/netbox_client_ruby/entities.rb', line 8

def self.included(other_klass)
  other_klass.extend ClassMethods
end

Instance Method Details

#[](index) ⇒ Object



137
138
139
140
141
# File 'lib/netbox_client_ruby/entities.rb', line 137

def [](index)
  return nil if length <= index

  as_entity raw_data_array[index]
end

#allObject



105
106
107
108
109
# File 'lib/netbox_client_ruby/entities.rb', line 105

def all
  @instance_limit = NetboxClientRuby.config.netbox.pagination.max_limit
  reset
  self
end

#eachObject



143
144
145
# File 'lib/netbox_client_ruby/entities.rb', line 143

def each
  raw_data_array.each { |raw_entity| yield as_entity(raw_entity) }
end

#filter(filter) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/netbox_client_ruby/entities.rb', line 97

def filter(filter)
  fail ArgumentError, '"filter" expects a hash' unless filter.is_a? Hash

  @filter = filter
  reset
  self
end

#find_by(attributes) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/netbox_client_ruby/entities.rb', line 77

def find_by(attributes)
  fail ArgumentError, '"attributes" expects a hash' unless attributes.is_a? Hash

  filter(attributes).find do |netbox_object|
    attributes.all? do |filter_key, filter_value|
      if filter_key.to_s.start_with?('cf_')
        custom_field = filter_key.to_s.sub('cf_', '')

        netbox_object.custom_fields[custom_field].to_s == filter_value.to_s
      else
        if netbox_object.respond_to?(filter_key)
          netbox_object.public_send(filter_key).to_s == filter_value.to_s
        else
          false
        end
      end
    end
  end
end

#lengthObject Also known as: size

The number of entities that have been fetched



149
150
151
# File 'lib/netbox_client_ruby/entities.rb', line 149

def length
  raw_data_array.length
end

#limit(limit) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/netbox_client_ruby/entities.rb', line 111

def limit(limit)
  self.class.check_limit limit unless limit.nil?

  @instance_limit = limit
  reset
  self
end

#offset(offset) ⇒ Object

Raises:

  • (ArgumentError)


119
120
121
122
123
124
125
126
# File 'lib/netbox_client_ruby/entities.rb', line 119

def offset(offset)
  raise ArgumentError, "The offset '#{offset}' is not numeric." unless offset.is_a? Numeric
  raise ArgumentError, "The offset '#{offset}' must not be negative." if offset.negative?

  @offset = offset
  reset
  self
end

#page(page) ⇒ Object

Raises:

  • (ArgumentError)


128
129
130
131
132
133
134
135
# File 'lib/netbox_client_ruby/entities.rb', line 128

def page(page)
  raise ArgumentError, "The offset '#{page}' is not numeric but has to be." unless page.is_a? Numeric
  raise ArgumentError, "The offset '#{page}' must be integer but isn't." unless page.integer?
  raise ArgumentError, "The offset '#{page}' must not be negative but is." if page.negative?

  limit = @instance_limit || self.class.limit
  offset(limit * page)
end

#raw_data!Object



164
165
166
# File 'lib/netbox_client_ruby/entities.rb', line 164

def raw_data!
  data
end

#reloadObject Also known as: get!



159
160
161
162
# File 'lib/netbox_client_ruby/entities.rb', line 159

def reload
  @data = get
  self
end

#totalObject Also known as: count

The total number of available entities for that query



155
156
157
# File 'lib/netbox_client_ruby/entities.rb', line 155

def total
  data[self.class.count_key]
end