Class: RIPE::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/ripe/object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, type, data = nil) ⇒ Object

Returns a new instance of Object.



13
14
15
16
17
# File 'lib/ripe/object.rb', line 13

def initialize(client, type, data = nil)
  @client = client
  @type = type
  @data = data
end

Class Method Details

.find(client, type, key) ⇒ Object



8
9
10
11
# 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

Instance Method Details

#[](key) ⇒ Object



37
38
39
# File 'lib/ripe/object.rb', line 37

def [](key)
  attributes[key]
end

#[]=(key, value) ⇒ Object



41
42
43
44
# File 'lib/ripe/object.rb', line 41

def []=(key, value)
  attributes[key] = AttributeSet.new(key) if attributes[key].nil?
  attributes[key].update(value)
end

#attributesObject



102
103
104
105
106
107
# 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

#createObject



69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



93
94
95
96
97
98
99
100
# 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?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/ripe/object.rb', line 23

def deleted?
  !!@deleted
end


31
32
33
34
35
# File 'lib/ripe/object.rb', line 31

def link
  if @data['link'] && @data['link']['type'] == 'locator'
    @data['link']['href']
  end
end

#new?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ripe/object.rb', line 19

def new?
  @data.nil?
end

#primary_keyObject



46
47
48
49
50
# File 'lib/ripe/object.rb', line 46

def primary_key
  if key = @data.dig('primary-key', 'attribute')&.first
    attributes[key['name']].first
  end
end

#sourceObject



27
28
29
# File 'lib/ripe/object.rb', line 27

def source
  @data['source']['id']
end

#to_api_hashObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

#updateObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# 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