Class: RecordStore::Record

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/record_store/record.rb

Direct Known Subclasses

A, AAAA, ALIAS, CAA, CNAME, MX, NS, PTR, SRV, SSHFP, TXT

Defined Under Namespace

Classes: A, AAAA, ALIAS, CAA, CNAME, MX, NS, PTR, SPF, SRV, SSHFP, TXT

Constant Summary collapse

FQDN_REGEX =
/\A(\*\.)?([a-z0-9_]+(-[_a-z0-9]+)*\._?)+[a-z]{2,}\.\Z/i
CNAME_REGEX =
/\A(\*\.)?([a-z0-9_]+((-|--)?[a-z0-9]+)*\._?)+[a-z]{2,}\.\Z/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Record

Returns a new instance of Record.



58
59
60
61
62
# File 'lib/record_store/record.rb', line 58

def initialize(record)
  @fqdn = Record.ensure_ends_with_dot(record.fetch(:fqdn)).downcase
  @ttl  = record.fetch(:ttl)
  @id   = record.fetch(:record_id, nil)
end

Instance Attribute Details

#fqdnObject

Returns the value of attribute fqdn.



8
9
10
# File 'lib/record_store/record.rb', line 8

def fqdn
  @fqdn
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/record_store/record.rb', line 8

def id
  @id
end

#ttlObject

Returns the value of attribute ttl.



8
9
10
# File 'lib/record_store/record.rb', line 8

def ttl
  @ttl
end

Class Method Details

.build_from_yaml_definition(yaml_definition) ⇒ Object



65
66
67
68
# File 'lib/record_store/record.rb', line 65

def build_from_yaml_definition(yaml_definition)
  record_type = yaml_definition.fetch(:type)
  Record.const_get(record_type).new(yaml_definition)
end

.ensure_ends_with_dot(fqdn) ⇒ Object



45
46
47
# File 'lib/record_store/record.rb', line 45

def ensure_ends_with_dot(fqdn)
  fqdn.end_with?(".") ? fqdn : "#{fqdn}."
end

.ensure_ends_without_dot(fqdn) ⇒ Object



49
50
51
# File 'lib/record_store/record.rb', line 49

def ensure_ends_without_dot(fqdn)
  fqdn.sub(/\.$/, '')
end

.escape(value) ⇒ Object



15
16
17
# File 'lib/record_store/record.rb', line 15

def escape(value)
  value.gsub('"', '\"')
end

.long_quote(value) ⇒ Object



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

def long_quote(value)
  result = value
  if needs_long_quotes?(value)
    result = unquote(value).scan(/.{1,255}/).join('" "')
    result = %("#{result}")
  end
  result
end

.needs_long_quotes?(value) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/record_store/record.rb', line 53

def needs_long_quotes?(value)
  value.length > 255 && value !~ /^((\\)?"((\\"|[^"])){1,255}(\\)?"\s*)+$/
end

.quote(value) ⇒ Object



19
20
21
22
# File 'lib/record_store/record.rb', line 19

def quote(value)
  result = escape(value)
  %("#{result}")
end

.unescape(value) ⇒ Object



37
38
39
# File 'lib/record_store/record.rb', line 37

def unescape(value)
  value.gsub('\"', '"')
end

.unlong_quote(value) ⇒ Object



33
34
35
# File 'lib/record_store/record.rb', line 33

def unlong_quote(value)
  value.length > 255 ? value.scan(/.{1,258}/).map { |x| x.sub(/^\"/, "").sub(/\" ?$/, "") }.join : unquote(value)
end

.unquote(value) ⇒ Object



41
42
43
# File 'lib/record_store/record.rb', line 41

def unquote(value)
  unescape(value.sub(/\A"(.*)"\z/, '\1'))
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



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

def ==(other)
  other.class == self.class && other.to_hash == to_hash
end

#hashObject



93
94
95
# File 'lib/record_store/record.rb', line 93

def hash
  to_hash.hash
end

#keyObject



101
102
103
# File 'lib/record_store/record.rb', line 101

def key
  "#{type},#{fqdn}"
end

#log!(logger = $stdout) ⇒ Object



71
72
73
# File 'lib/record_store/record.rb', line 71

def log!(logger = $stdout)
  logger.puts to_s
end

#rdataObject

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/record_store/record.rb', line 105

def rdata
  raise NotImplementedError
end

#rdata_txtObject

Raises:

  • (NotImplementedError)


109
110
111
# File 'lib/record_store/record.rb', line 109

def rdata_txt
  raise NotImplementedError
end

#to_hashObject



75
76
77
78
79
80
81
# File 'lib/record_store/record.rb', line 75

def to_hash
  {
    type: type,
    fqdn: fqdn,
    ttl: ttl,
  }.merge(rdata)
end

#to_jsonObject



97
98
99
# File 'lib/record_store/record.rb', line 97

def to_json
  { ttl: ttl, rdata: rdata }
end

#to_sObject



113
114
115
# File 'lib/record_store/record.rb', line 113

def to_s
  "[#{type}Record] #{fqdn} #{ttl} IN #{type} #{rdata_txt}"
end

#typeObject



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

def type
  self.class.name.demodulize
end

#wildcard?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/record_store/record.rb', line 117

def wildcard?
  fqdn.match?(/^\*\./)
end