Class: DNSApp::Record

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

check_required_keys, delete, get, #initialize, #parent_id, #parent_id=, post, put

Constructor Details

This class inherits a constructor from DNSApp::Object

Instance Attribute Details

#contentObject

content for the record



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

def content
  @content
end

#created_atObject

When the record was created



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

def created_at
  @created_at
end

#domain_idObject

domain_id for the record



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

def domain_id
  @domain_id
end

#idObject

id for the record



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

def id
  @id
end

#nameObject

name for the record



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

def name
  @name
end

#prioObject

priority for the record. Only needed for MX and SRV records.



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

def prio
  @prio
end

#ttlObject

time-to-live for the record



16
17
18
# File 'lib/dnsapp/record.rb', line 16

def ttl
  @ttl
end

#typeObject

type for the record Must be one of the following A AAAA NS SOA CNAME TXT SPF SRV MX PTR



26
27
28
# File 'lib/dnsapp/record.rb', line 26

def type
  @type
end

#updated_atObject

When the record was last updated



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

def updated_at
  @updated_at
end

Class Method Details

.allObject

Get all the records



31
32
33
34
# File 'lib/dnsapp/record.rb', line 31

def all 
  response = get("/domains/#{@@parent_id}/records")["records"]        
  response.collect { |attributes| Record.new attributes }
end

.create(options = {}) ⇒ Object

Creates a new record. options must contain at least the following: :name, :content, :type, :tll For SRV and MX records :prio can also be specified.

Valid types

A AAAA NS SOA CNAME TXT SPF SRV MX PTR

Example

DNSApp::Domain.all.first.records.create :name => '%d', :content => '1.2.3.4', :type => 'A', :ttl => 3600


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

def create(options={})
  check_required_keys options, :name, :content, :type, :ttl
  
  r = post("/domains/#{@@parent_id}/records", :query => { "record[name]" => options[:name],
                                                          "record[ttl]" => options[:ttl],
                                                          "record[content]" => options[:content],
                                                          "record[prio]" => options[:prio],
                                                          "record[type]" => options[:type] })
  r["errors"] and raise StandardError, r["errors"]["error"].to_a.join(", ")
  if r.code == 201
    Record.new r["record"]
  else
    raise StandardError, 'Could not create the record'
  end        
end

.find(id) ⇒ Object

Find a specific record by id

Example

DNSApp::Domain.all.first.find(243)
=> #<DNSApp::Record:0x101ad5e88 @name="%d", @domain_id=131, @ttl=3600, @created_at=Mon Jan 10 15:48:02 UTC 2011, @content="1.2.3.4", @type="A", @id=1821, @prio=nil, @updated_at=Mon Jan 10 15:55:29 UTC 2011>


40
41
42
43
44
# File 'lib/dnsapp/record.rb', line 40

def find(id)
  response = get("/domains/#{@@parent_id}/records/#{id}")["record"]
  response or return nil
  Record.new response
end

Instance Method Details

#destroyObject Also known as: delete

Deletes the record. Returns the deleted record.

Example

r = DNSApp::Domain.all.first.record.first.destroy
=> #<DNSApp::Record:0x101ad5e88 @name="%d", @domain_id=131, @ttl=3600, @created_at=Mon Jan 10 15:48:02 UTC 2011, @content="1.2.3.4", @type="A", @id=1821, @prio=nil, @updated_at=Mon Jan 10 15:55:29 UTC 2011>


96
97
98
99
100
101
102
103
# File 'lib/dnsapp/record.rb', line 96

def destroy
  r = self.class.delete("/domains/#{@@parent_id}/records/#{self.id}")
  if r.code == 200
    self
  else
    raise StandardError, 'Could not delete the record'
  end      
end

#saveObject

Saves the record.

Example

r = DNSApp::Domain.all.first.record.first
=> #<DNSApp::Record:0x101ad5e88 @name="%d", @domain_id=131, @ttl=3600, @created_at=Mon Jan 10 15:48:02 UTC 2011, @content="1.2.3.4", @type="A", @id=1821, @prio=nil, @updated_at=Mon Jan 10 15:55:29 UTC 2011> 
r.content = '9.9.9.9'
=> #<DNSApp::Record:0x101ad5e88 @name="%d", @domain_id=131, @ttl=3600, @created_at=Mon Jan 10 15:48:02 UTC 2011, @content="9.9.9.9", @type="A", @id=1821, @prio=nil, @updated_at=Mon Jan 10 17:52:09 UTC 2011>


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dnsapp/record.rb', line 77

def save
  r = self.class.put("/domains/#{@@parent_id}/records/#{self.id}", :query => { "record[name]" => self.name,
                                                                               "record[ttl]" => self.ttl,
                                                                               "record[content]" => self.content,
                                                                               "record[prio]" => self.prio,
                                                                               "record[type]" => self.type })
  r["errors"] and raise StandardError, r["errors"]["error"].to_a.join(", ")
  if r.code == 200
    self.class.find(self.id)
  else
    raise StandardError, 'Could not update the record'
  end
end