Class: DNSimple::Record

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

Constant Summary collapse

Aliases =
{
  'priority'     => 'prio',
  'time-to-live' => 'ttl'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from DNSimple::Base

Instance Attribute Details

#contentObject

Returns the value of attribute content.



12
13
14
# File 'lib/dnsimple/record.rb', line 12

def content
  @content
end

#domainObject

Returns the value of attribute domain.



10
11
12
# File 'lib/dnsimple/record.rb', line 10

def domain
  @domain
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/dnsimple/record.rb', line 9

def id
  @id
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/dnsimple/record.rb', line 11

def name
  @name
end

#prioObject

Returns the value of attribute prio.



14
15
16
# File 'lib/dnsimple/record.rb', line 14

def prio
  @prio
end

#record_typeObject

Returns the value of attribute record_type.



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

def record_type
  @record_type
end

#ttlObject

Returns the value of attribute ttl.



13
14
15
# File 'lib/dnsimple/record.rb', line 13

def ttl
  @ttl
end

Class Method Details

.all(domain, options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/dnsimple/record.rb', line 82

def self.all(domain, options={})
  response = DNSimple::Client.get("/v1/domains/#{domain.name}/records", options)

  case response.code
  when 200
    response.map { |r| new({:domain => domain}.merge(r["record"])) }
  else
    raise RequestError.new("Error listing records", response)
  end
end

.create(domain, name, record_type, content, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dnsimple/record.rb', line 49

def self.create(domain, name, record_type, content, options={})
  record_hash = {:name => name, :record_type => record_type, :content => content}
  record_hash[:ttl] = options.delete(:ttl) || 3600
  record_hash[:prio] = options.delete(:priority)
  record_hash[:prio] = options.delete(:prio) || ''

  options.merge!({:body => {:record => record_hash}})

  response = DNSimple::Client.post("/v1/domains/#{domain.name}/records", options)

  case response.code
  when 201
    new({:domain => domain}.merge(response["record"]))
  when 406
    raise RecordExists, "Record #{name}.#{domain.name} already exists"
  else
    raise RequestError.new("Error creating record", response)
  end
end

.find(domain, id, options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dnsimple/record.rb', line 69

def self.find(domain, id, options={})
  response = DNSimple::Client.get("/v1/domains/#{domain.name}/records/#{id}", options)

  case response.code
  when 200
    new({:domain => domain}.merge(response["record"]))
  when 404
    raise RecordNotFound, "Could not find record #{id} for domain #{domain.name}"
  else
    raise RequestError.new("Error finding record", response)
  end
end

.resolve(name) ⇒ Object



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

def self.resolve(name)
  DNSimple::Record::Aliases[name] || name
end

Instance Method Details

#delete(options = {}) ⇒ Object Also known as: destroy



40
41
42
# File 'lib/dnsimple/record.rb', line 40

def delete(options={})
  DNSimple::Client.delete("/v1/domains/#{domain.id}/records/#{id}", options)
end

#fqdnObject



18
19
20
# File 'lib/dnsimple/record.rb', line 18

def fqdn
  [name, domain.name].delete_if { |v| v !~ DNSimple::BLANK_REGEX }.join(".")
end

#save(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dnsimple/record.rb', line 22

def save(options={})
  record_hash = {}
  %w(name content ttl prio).each do |attribute|
    record_hash[DNSimple::Record.resolve(attribute)] = self.send(attribute)
  end

  options.merge!(:body => {:record => record_hash})

  response = DNSimple::Client.put("/v1/domains/#{domain.id}/records/#{id}", options)

  case response.code
  when 200
    self
  else
    raise RequestError.new("Error updating record", response)
  end
end