module NetboxClientRuby::Entities

Public Class Methods

included(other_klass) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 9
def self.included(other_klass)
  other_klass.extend ClassMethods
end

Public Instance Methods

[](index) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 138
def [](index)
  return nil if length <= index

  as_entity raw_data_array[index]
end
all() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 106
def all
  @instance_limit = NetboxClientRuby.config.netbox.pagination.max_limit
  reset
  self
end
count()
Alias for: total
each() { |as_entity(raw_entity)| ... } click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 144
def each
  raw_data_array.each { |raw_entity| yield as_entity(raw_entity) }
end
filter(filter) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 98
def filter(filter)
  fail ArgumentError, '"filter" expects a hash' unless filter.is_a? Hash

  @filter = filter
  reset
  self
end
find_by(attributes) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 78
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
get!()
Alias for: reload
length() click to toggle source

The number of entities that have been fetched

# File lib/netbox_client_ruby/entities.rb, line 150
def length
  raw_data_array.length
end
Also aliased as: size
limit(limit) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 112
def limit(limit)
  self.class.check_limit limit unless limit.nil?

  @instance_limit = limit
  reset
  self
end
offset(offset) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 120
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) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 129
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!() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 165
def raw_data!
  data
end
reload() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 160
def reload
  @data = get
  self
end
Also aliased as: get!
size()
Alias for: length
total() click to toggle source

The total number of available entities for that query

# File lib/netbox_client_ruby/entities.rb, line 156
def total
  data[self.class.count_key]
end
Also aliased as: count

Private Instance Methods

as_entity(raw_entity) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 213
def as_entity(raw_entity)
  entity_creator_method = method self.class.entity_creator
  entity = entity_creator_method.call raw_entity
  entity.data = raw_entity
  entity
end
data() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 183
def data
  @data ||= get
end
get() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 187
def get
  response connection.get path_with_parameters
end
join_path_parameters(params) click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 205
def join_path_parameters(params)
  return '' if params.empty?

  '?' + params.compact.map do |param_obj|
    URI.encode_www_form param_obj
  end.join('&')
end
path_parameters() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 195
def path_parameters
  params = []

  params << @filter
  params << { limit: @instance_limit || self.class.limit }
  params << { offset: @offset } if @offset

  join_path_parameters(params)
end
path_with_parameters() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 191
def path_with_parameters
  self.class.path + path_parameters
end
raw_data_array() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 179
def raw_data_array
  data[self.class.data_key] || []
end
reset() click to toggle source
# File lib/netbox_client_ruby/entities.rb, line 175
def reset
  @data = nil
end