class Dennis::Record

Public Class Methods

all(client, zone, **options) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
new(client, hash) click to toggle source
# File lib/dennis/record.rb, line 82
def initialize(client, hash)
  @client = client
  @hash = hash
end
properties_to_argument(hash, type: nil) click to toggle source
# 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

Public Instance Methods

content() click to toggle source
# 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_at() click to toggle source
# 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
delete() click to toggle source
# 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_content() click to toggle source
# File lib/dennis/record.rb, line 148
def display_content
  @hash['display_content']
end
external_reference() click to toggle source
# File lib/dennis/record.rb, line 111
def external_reference
  @hash['external_reference']
end
full_name() click to toggle source
# File lib/dennis/record.rb, line 95
def full_name
  @hash['full_name']
end
id() click to toggle source
# File lib/dennis/record.rb, line 87
def id
  @hash['id']
end
managed?() click to toggle source
# File lib/dennis/record.rb, line 115
def managed?
  @hash['managed']
end
name() click to toggle source
# File lib/dennis/record.rb, line 91
def name
  @hash['name']
end
priority() click to toggle source
# File lib/dennis/record.rb, line 107
def priority
  @hash['priority']
end
raw_content() click to toggle source
# File lib/dennis/record.rb, line 144
def raw_content
  @hash['raw_content']
end
tags() click to toggle source
# File lib/dennis/record.rb, line 119
def tags
  @hash['tags']
end
ttl() click to toggle source
# File lib/dennis/record.rb, line 103
def ttl
  @hash['ttl']
end
type() click to toggle source
# File lib/dennis/record.rb, line 99
def type
  @hash['type']
end
update(properties) click to toggle source
# 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_at() click to toggle source
# 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
zone() click to toggle source
# 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