Class: DNSimple::TemplateRecord

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dnsimple/template_record.rb

Overview

A single record in a template

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ TemplateRecord

:nodoc:



28
29
30
31
32
33
# File 'lib/dnsimple/template_record.rb', line 28

def initialize(attributes)
  attributes.each do |key, value|
    m = "#{key}=".to_sym
    self.send(m, value) if self.respond_to?(m)
  end
end

Instance Attribute Details

#contentObject

The content for the record.



16
17
18
# File 'lib/dnsimple/template_record.rb', line 16

def content
  @content
end

#idObject

The id of the template record



7
8
9
# File 'lib/dnsimple/template_record.rb', line 7

def id
  @id
end

#nameObject

The name the record points to. This may be blank.



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

def name
  @name
end

#prioObject

The priority (only for MX records)



25
26
27
# File 'lib/dnsimple/template_record.rb', line 25

def prio
  @prio
end

#record_typeObject

The record type



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

def record_type
  @record_type
end

#templateObject

The template the record belongs to



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

def template
  @template
end

#ttlObject

The time-to-live



22
23
24
# File 'lib/dnsimple/template_record.rb', line 22

def ttl
  @ttl
end

Class Method Details

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

Get all of the template records for the template with the given short name.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dnsimple/template_record.rb', line 84

def self.all(short_name, options={})
  template = Template.find(short_name)
  options.merge!({:basic_auth => Client.credentials})
  response = self.get("#{Client.base_uri}/templates/#{template.id}/template_records.json", options)

  pp response if Client.debug?

  case response.code
  when 200
    response.map { |r| TemplateRecord.new({:template => template}.merge(r["dns_template_record"])) }
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise RuntimeError, "Error: #{response.code}"
  end
end

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dnsimple/template_record.rb', line 41

def self.create(short_name, name, record_type, content, options={})
  template = Template.find(short_name)

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

  options.merge!({:query => {:dns_template_record => record_hash}})
  options.merge!({:basic_auth => Client.credentials})

  response = self.post("#{Client.base_uri}/templates/#{template.id}/template_records.json", options)

  pp response if Client.debug?

  case response.code
  when 201
    return TemplateRecord.new({:template => template}.merge(response["dns_template_record"]))
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise DNSimple::Error.new("#{name}", response["errors"])
  end
end

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dnsimple/template_record.rb', line 65

def self.find(short_name, id, options={})
  template = Template.find(short_name)
  options.merge!(:basic_auth => Client.credentials)
  response = self.get("#{Client.base_uri}/templates/#{template.id}/template_records/#{id}.json", options)

  pp response if Client.debug?

  case response.code
  when 200
    return TemplateRecord.new({:template => template}.merge(response["dns_template_record"]))
  when 401
    raise RuntimeError, "Authentication failed"
  when 404
    raise RuntimeError, "Could not find template record #{id} for template #{short_name}"
  end
end

Instance Method Details

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



35
36
37
38
# File 'lib/dnsimple/template_record.rb', line 35

def delete(options={})
  options.merge!(:basic_auth => Client.credentials)
  self.class.delete("#{Client.base_uri}/templates/#{template.id}/template_records/#{id}.json", options)
end