Class: PreviewAdd::Dns

Inherits:
Object
  • Object
show all
Defined in:
lib/preview_add/dns.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, host) ⇒ Dns

Returns a new instance of Dns.



7
8
9
10
11
12
13
14
15
# File 'lib/preview_add/dns.rb', line 7

def initialize(options, host)
  Zerigo::DNS::Base.user     = options[:zerigo][:user]
  Zerigo::DNS::Base.password = options[:zerigo][:api_key]
  Zerigo::DNS::Base.format   = :xml
  
  @zone_id = options[:zerigo][:zone_id]
  @host    = host
  @cname   = options[:zerigo][:cname]
end

Instance Method Details

#add_cnameObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/preview_add/dns.rb', line 17

def add_cname
  vals = {
    :hostname  => @host,
    :host_type => 'CNAME',
    :data      => @cname,
    :zone_id   => @zone_id
  }
  
  begin
    Zerigo::DNS::Host.create(vals)
  rescue Exception => e
    if e.to_s.include? 'wrong number of arguments (2 for 1)'
      puts "Successfully created #{@host}.#{@cname}"
    else
      puts e
    end
  end
  
end

#create_if_not_existsObject



47
48
49
50
51
# File 'lib/preview_add/dns.rb', line 47

def create_if_not_exists
  unless record_exists? @host
    add_cname
  end
end

#record_exists?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/preview_add/dns.rb', line 37

def record_exists?(hostname)
  begin
    hosts = Zerigo::DNS::Host.find(:all, :params=>{:zone_id => @zone_id})
    hosts.any? { |hash| hash.hostname == hostname }
  rescue Exception => e
    puts e
    exit 1
  end
end