Class: Dennis::Record

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, hash) ⇒ Record

Returns a new instance of Record.



82
83
84
85
# File 'lib/dennis/record.rb', line 82

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

Class Method Details

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



12
13
14
15
16
17
18
19
# File 'lib/dennis/record.rb', line 12

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



21
22
23
24
25
26
# File 'lib/dennis/record.rb', line 21

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dennis/record.rb', line 36

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



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dennis/record.rb', line 48

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



28
29
30
31
32
33
34
# File 'lib/dennis/record.rb', line 28

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

.properties_to_argument(hash, type: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dennis/record.rb', line 62

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

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

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

Instance Method Details

#contentObject



152
153
154
155
156
# File 'lib/dennis/record.rb', line 152

def content
  return nil if type.nil?

  @hash.dig('content', type.to_s.upcase)&.transform_keys(&:to_sym)
end

#created_atObject



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

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



170
171
172
173
174
175
# File 'lib/dennis/record.rb', line 170

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

#display_contentObject



148
149
150
# File 'lib/dennis/record.rb', line 148

def display_content
  @hash['display_content']
end

#external_referenceObject



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

def external_reference
  @hash['external_reference']
end

#full_nameObject



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

def full_name
  @hash['full_name']
end

#idObject



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

def id
  @hash['id']
end

#managed?Boolean

Returns:

  • (Boolean)


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

def managed?
  @hash['managed']
end

#nameObject



91
92
93
# File 'lib/dennis/record.rb', line 91

def name
  @hash['name']
end

#priorityObject



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

def priority
  @hash['priority']
end

#raw_contentObject



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

def raw_content
  @hash['raw_content']
end

#tagsObject



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

def tags
  @hash['tags']
end

#ttlObject



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

def ttl
  @hash['ttl']
end

#typeObject



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

def type
  @hash['type']
end

#update(properties) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dennis/record.rb', line 158

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



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

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



137
138
139
140
141
142
# File 'lib/dennis/record.rb', line 137

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

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