Class: ZerigoDNS::Host

Inherits:
Base
  • Object
show all
Defined in:
lib/zerigodns/host.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#load

Class Method Details

.find_all_by_hostname(zone, hostname) ⇒ Object



14
15
16
# File 'lib/zerigodns/host.rb', line 14

def find_all_by_hostname zone, hostname
  find_by_zone_and_hostname(:all, zone, hostname)
end

.find_by_zone_and_hostname(which, zone, hostname) ⇒ Object

Find host record(s) by zone and hostname

Parameters:

Returns:

  • Host records, or an empty list if no records found.



9
10
11
12
# File 'lib/zerigodns/host.rb', line 9

def find_by_zone_and_hostname which, zone, hostname
  fqdn = [hostname, zone.domain].select(&:present?).join('.')
  find(which, params: {fqdn: fqdn, zone_id: zone.id})
end

.find_first_by_hostname(zone, hostname) ⇒ Host

Returns The record found, or nil.

Returns:

  • (Host)

    The record found, or nil.



19
20
21
# File 'lib/zerigodns/host.rb', line 19

def find_first_by_hostname zone, hostname
  find_by_zone_and_hostname(:all, zone, hostname).try(:first)
end

.update_or_create(zone, hostname, type, ttl, data) ⇒ Host

Update or Create Host for a zone This method will only update the first record.

Parameters:

  • zone (Zone, #read)

    The zone to which the host belongs

  • hostname (String, #read)

    The hostname

  • type (String, #read)

    The type of record (e.g ‘CNAME’)

  • ttl (Fixnum, #read)

    The TTL of the record, in seconds.

  • data (String, #read)

    The data field of the record.

Returns:

  • (Host)

    The created or updated host.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zerigodns/host.rb', line 31

def update_or_create(zone, hostname, type, ttl, data)
  host = find_first_by_hostname(zone, hostname)
  if host
    host.update_record(type,ttl,data)
  else
    host = create(
      :zone_id    => zone.id, 
      :hostname   => hostname,
      :host_type  => type,
      :data       => data,
      :ttl        => ttl
    )
  end  
  host
end

Instance Method Details

#update_record(type, ttl, data) ⇒ Boolean, #read

Convienence method to update the record.

Parameters:

  • type (String, #read)
  • ttl (String, #read)
  • data (String, #read)

Returns:

  • (Boolean, #read)

    True if saved, false otherwise.



53
54
55
56
57
58
# File 'lib/zerigodns/host.rb', line 53

def update_record type, ttl, data
  self.host_type  = type
  self.data       = data
  self.ttl        = ttl
  save
end