class RIPE::Object

Public Class Methods

find(client, type, key) click to toggle source
# File lib/ripe/object.rb, line 8
def self.find(client, type, key)
  request = HTTPRequest.new(client, type).lookup(key)
  self.new(client, type, request)
end
new(client, type, data = nil) click to toggle source
# File lib/ripe/object.rb, line 13
def initialize(client, type, data = nil)
  @client = client
  @type = type
  @data = data
end

Public Instance Methods

[](key) click to toggle source
# File lib/ripe/object.rb, line 37
def [](key)
  attributes[key]
end
[]=(key, value) click to toggle source
# File lib/ripe/object.rb, line 41
def []=(key, value)
  attributes[key] = AttributeSet.new(key) if attributes[key].nil?
  attributes[key].update(value)
end
attributes() click to toggle source
# File lib/ripe/object.rb, line 102
def attributes
  @attributes ||= new? ? {} : @data['attributes']['attribute'].each_with_object({}) do |attr, hash|
    hash[attr['name']] ||= AttributeSet.new(@client, attr['name'])
    hash[attr['name']] << Attribute.new(@client, attr)
  end
end
create() click to toggle source
# File lib/ripe/object.rb, line 69
def create
  if !new?
    raise RIPE::Error, "This object has already been created, it cannot be created again"
  end

  request = HTTPRequest.new(@client, @type).create(@client.password, self.to_api_hash)
  @data = request
  self
end
delete(reason = nil) click to toggle source
# File lib/ripe/object.rb, line 93
def delete(reason = nil)
  if key = primary_key&.value
    request = HTTPRequest.new(@client, @type).delete(@client.password, key, reason)
    @deleted = true
  else
    raise RIPE::Error, "Object does not have a primary key therefore cannot be deleted"
  end
end
deleted?() click to toggle source
# File lib/ripe/object.rb, line 23
def deleted?
  !!@deleted
end
new?() click to toggle source
# File lib/ripe/object.rb, line 19
def new?
  @data.nil?
end
primary_key() click to toggle source
# File lib/ripe/object.rb, line 46
def primary_key
  if key = @data.dig('primary-key', 'attribute')&.first
    attributes[key['name']].first
  end
end
source() click to toggle source
# File lib/ripe/object.rb, line 27
def source
  @data['source']['id']
end
to_api_hash() click to toggle source
# File lib/ripe/object.rb, line 52
def to_api_hash
  {
    'objects' => {
      'object' => [
        {
          'source' => {
            'id' => @client.mode == :test ? 'test' : 'ripe'
          },
          'attributes' => {
            'attribute' => attributes.values.map(&:to_api_hash).flatten
          }
        }
      ]
    }
  }
end
update() click to toggle source
# File lib/ripe/object.rb, line 79
def update
  if new?
    raise RIPE::Error, "This object has not been created yet, it cannot be updated until it exists"
  end

  if key = primary_key&.value
    request = HTTPRequest.new(@client, @type).update(@client.password, key, self.to_api_hash)
    @data = request
    self
  else
    raise RIPE::Error, "Object does not have a primary key therefore cannot be updated"
  end
end