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.



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

def content
  @content
end

#domainObject

Returns the value of attribute domain.



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

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.



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

def name
  @name
end

#prioObject

Returns the value of attribute prio.



21
22
23
# File 'lib/dnsimple/record.rb', line 21

def prio
  @prio
end

#record_typeObject

Returns the value of attribute record_type.



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

def record_type
  @record_type
end

#ttlObject

Returns the value of attribute ttl.



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

def ttl
  @ttl
end

Class Method Details

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/dnsimple/record.rb', line 87

def self.all(domain, options={})
  response = DNSimple::Client.get("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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dnsimple/record.rb', line 54

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("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



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dnsimple/record.rb', line 74

def self.find(domain, id, options={})
  response = DNSimple::Client.get("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



50
51
52
# File 'lib/dnsimple/record.rb', line 50

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

Instance Method Details

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



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

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

#fqdnObject



23
24
25
# File 'lib/dnsimple/record.rb', line 23

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

#save(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dnsimple/record.rb', line 27

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("domains/#{domain.id}/records/#{id}", options)

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