Class: DNSimple::Template

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Template

:nodoc:



18
19
20
21
22
23
# File 'lib/dnsimple/template.rb', line 18

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

#descriptionObject

The template description



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

def description
  @description
end

#idObject

The template ID in DNSimple



6
7
8
# File 'lib/dnsimple/template.rb', line 6

def id
  @id
end

#nameObject

The template name



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

def name
  @name
end

#short_nameObject

The template short name



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

def short_name
  @short_name
end

Class Method Details

.all(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dnsimple/template.rb', line 75

def self.all(options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.get("#{Client.base_uri}/templates", options)

  pp response if Client.debug?

  case response.code
  when 200
    response.map { |r| Template.new(r["dns_template"]) }
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise RuntimeError, "Error: #{response.code}"
  end
end

.create(name, short_name, description = nil, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dnsimple/template.rb', line 33

def self.create(name, short_name, description=nil, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  template_hash = {
    :name => name, 
    :short_name => short_name,
    :description => description
  }

  options.merge!(:body => {:dns_template => template_hash})

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

  pp response if Client.debug?

  case response.code
  when 201
    return Template.new(response["dns_template"])
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise DNSimple::Error.new(name, response["errors"])
  end
end

.find(id_or_short_name, options = {}) ⇒ Object



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

def self.find(id_or_short_name, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.get("#{Client.base_uri}/templates/#{id_or_short_name}", options)
  
  pp response if Client.debug?
  
  case response.code
  when 200
    return Template.new(response["dns_template"])
  when 401
    raise RuntimeError, "Authentication failed"
  when 404
    raise RuntimeError, "Could not find template #{id_or_short_name}"
  else
    raise DNSimple::Error.new(id_or_short_name, response["errors"])
  end
end

Instance Method Details

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

Delete the template from DNSimple. WARNING: this cannot be undone.



27
28
29
30
# File 'lib/dnsimple/template.rb', line 27

def delete(options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  self.class.delete("#{Client.base_uri}/templates/#{id}", options)
end