Class: Dennis::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/dennis/record.rb

Constant Summary collapse

IRREGULAR_RECORD_TYPE_NAMES =
{
  'HTTPREDIRECT' => 'HTTPRedirect'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, hash) ⇒ Record

Returns a new instance of Record.



94
95
96
97
# File 'lib/dennis/record.rb', line 94

def initialize(client, hash)
  @client = client
  @hash = hash
end

Class Method Details

.all(client, zone, **options) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/dennis/record.rb', line 16

def all(client, zone, **options)
  request = client.api.create_request(:get, 'zones/:zone/records')
  request.arguments[:zone] = zone
  options.each do |field, value|
    request.arguments[field] = value
  end
  request.perform.hash['records'].map { |hash| new(client, hash) }
end

.all_by_tag(client, tags, group: nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/dennis/record.rb', line 25

def all_by_tag(client, tags, group: nil)
  request = client.api.create_request(:get, 'records/tagged')
  request.arguments[:tags] = tags
  request.arguments[:group] = group if group
  request.perform.hash['records'].map { |hash| new(client, hash) }
end

.create(client, zone:, **properties) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dennis/record.rb', line 40

def create(client, zone:, **properties)
  request = client.api.create_request(:post, 'records')
  request.arguments[:zone] = zone
  request.arguments[:properties] = properties_to_argument(properties)
  new(client, request.perform.hash['record'])
rescue ApiaClient::RequestError => e
  raise ZoneNotFoundError if e.code == 'zone_not_found'
  raise ValidationError, e.detail['errors'] if e.code == 'validation_error'

  raise
end

.create_or_update(client, zone_id, **properties) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dennis/record.rb', line 52

def create_or_update(client, zone_id, **properties)
  if properties[:external_reference].nil?
    raise Dennis::ExternalReferenceRequiredError, 'An external_reference must be provided to use create_or_update'
  end

  record = find_by(client, :external_reference, properties[:external_reference])
  if record.nil?
    create(client, zone: { id: zone_id }, **properties)
  else
    record.update(properties)
    record
  end
end

.find_by(client, field, value) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/dennis/record.rb', line 32

def find_by(client, field, value)
  request = client.api.create_request(:get, 'records/:record')
  request.arguments[:record] = { field => value }
  new(client, request.perform.hash['record'])
rescue ApiaClient::RequestError => e
  e.code == 'record_not_found' ? nil : raise
end

.normalize_type(type) ⇒ Object



86
87
88
89
90
# File 'lib/dennis/record.rb', line 86

def normalize_type(type)
  type = type.to_s.upcase
  type = IRREGULAR_RECORD_TYPE_NAMES[type] if IRREGULAR_RECORD_TYPE_NAMES.key?(type)
  type
end

.properties_to_argument(hash, type: nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dennis/record.rb', line 66

def properties_to_argument(hash, type: nil)
  arguments = {}
  [:name, :type, :ttl, :priority, :external_reference, :tags].each do |field_name|
    arguments[field_name] = hash[field_name] if hash.key?(field_name)

    type = hash[field_name] if field_name == :type && hash.key?(field_name)
  end

  type = normalize_type(type)

  if hash.key?(:content)
    if type.nil?
      raise Error, 'Cannot generate record properties without a type'
    end

    arguments[:content] = { type => hash[:content] }
  end
  arguments
end

Instance Method Details

#contentObject



164
165
166
167
168
# File 'lib/dennis/record.rb', line 164

def content
  return nil if type.nil?

  @hash.dig('content', self.class.normalize_type(type))&.transform_keys(&:to_sym)
end

#created_atObject



135
136
137
138
139
140
# File 'lib/dennis/record.rb', line 135

def created_at
  return nil if @hash['created_at'].nil?
  return @hash['created_at'] if @hash['created_at'].is_a?(Time)

  Time.at(@hash['created_at'])
end

#deleteObject



182
183
184
185
186
187
# File 'lib/dennis/record.rb', line 182

def delete
  req = @client.api.create_request(:delete, 'records/:record')
  req.arguments['record'] = { id: id }
  req.perform
  true
end

#display_contentObject



160
161
162
# File 'lib/dennis/record.rb', line 160

def display_content
  @hash['display_content']
end

#external_referenceObject



123
124
125
# File 'lib/dennis/record.rb', line 123

def external_reference
  @hash['external_reference']
end

#full_nameObject



107
108
109
# File 'lib/dennis/record.rb', line 107

def full_name
  @hash['full_name']
end

#idObject



99
100
101
# File 'lib/dennis/record.rb', line 99

def id
  @hash['id']
end

#managed?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/dennis/record.rb', line 127

def managed?
  @hash['managed']
end

#nameObject



103
104
105
# File 'lib/dennis/record.rb', line 103

def name
  @hash['name']
end

#priorityObject



119
120
121
# File 'lib/dennis/record.rb', line 119

def priority
  @hash['priority']
end

#raw_contentObject



156
157
158
# File 'lib/dennis/record.rb', line 156

def raw_content
  @hash['raw_content']
end

#tagsObject



131
132
133
# File 'lib/dennis/record.rb', line 131

def tags
  @hash['tags']
end

#ttlObject



115
116
117
# File 'lib/dennis/record.rb', line 115

def ttl
  @hash['ttl']
end

#typeObject



111
112
113
# File 'lib/dennis/record.rb', line 111

def type
  @hash['type']
end

#update(properties) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dennis/record.rb', line 170

def update(properties)
  req = @client.api.create_request(:patch, 'records/:record')
  req.arguments['record'] = { id: id }
  req.arguments['properties'] = self.class.properties_to_argument(properties, type: type)
  @hash = req.perform.hash['record']
  true
rescue ApiaClient::RequestError => e
  raise ValidationError, e.detail['errors'] if e.code == 'validation_error'

  raise
end

#updated_atObject



142
143
144
145
146
147
# File 'lib/dennis/record.rb', line 142

def updated_at
  return nil if @hash['updated_at'].nil?
  return @hash['updated_at'] if @hash['updated_at'].is_a?(Time)

  Time.at(@hash['updated_at'])
end

#zoneObject



149
150
151
152
153
154
# File 'lib/dennis/record.rb', line 149

def zone
  return @zone if @zone
  return nil unless @hash['zone']

  @zone = Zone.new(@client, @hash['zone'])
end