Class: DNSimple::Domain

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

Overview

Class representing a single domain in DNSimple.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Domain

:nodoc:



22
23
24
25
26
27
# File 'lib/dnsimple/domain.rb', line 22

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

#created_atObject

When the domain was created in DNSimple



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

def created_at
  @created_at
end

#idObject

The domain ID in DNSimple



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

def id
  @id
end

#nameObject

The domain name



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

def name
  @name
end

#name_server_statusObject

The current known name server status



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

def name_server_status
  @name_server_status
end

#updated_atObject

When the domain was last update in DNSimple



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

def updated_at
  @updated_at
end

Class Method Details

.all(options = {}) ⇒ Object

Get all domains for the account.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/dnsimple/domain.rb', line 229

def self.all(options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.get("#{DNSimple::Client.base_uri}/domains", options)
  
  pp response if DNSimple::Client.debug?

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

.check(name, options = {}) ⇒ Object

Check the availability of a name



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dnsimple/domain.rb', line 140

def self.check(name, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.get("#{DNSimple::Client.base_uri}/domains/#{name}/check", options)
  pp response if DNSimple::Client.debug?
  case response.code
  when 200
    "registered"
  when 401
    raise RuntimeError, "Authentication failed"
  when 404
    "available"
  else
    raise "Error: #{response.code}" 
  end
end

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

Create the domain with the given name in DNSimple. This method returns a Domain instance if the name is created and raises an error otherwise.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/dnsimple/domain.rb', line 159

def self.create(name, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)

  domain_hash = {:name => name}
  options.merge!({:body => {:domain => domain_hash}})

  response = self.post("#{DNSimple::Client.base_uri}/domains", options)
  
  pp response if DNSimple::Client.debug?
  
  case response.code
  when 201
    return DNSimple::Domain.new(response["domain"])
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise DNSimple::DomainError.new(name, response["errors"])
  end
end

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

Find a specific domain in the account either by the numeric ID or by the fully-qualified domain name.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/dnsimple/domain.rb', line 210

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

.register(name, registrant = {}, extended_attributes = {}, options = {}) ⇒ Object

Purchase a domain name.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/dnsimple/domain.rb', line 180

def self.register(name, registrant={}, extended_attributes={}, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)

  body = {:domain => {:name => name}}
  if registrant
    if registrant[:id]
      body[:domain][:registrant_id] = registrant[:id]
    else
      body.merge!(:contact => Contact.resolve_attributes(registrant))
    end
  end
  body.merge!(:extended_attribute => extended_attributes)
  options.merge!({:body => body})
  
  response = self.post("#{DNSimple::Client.base_uri}/domain_registrations", options)
  
  pp response if DNSimple::Client.debug?
  
  case response.code
  when 201
    return DNSimple::Domain.new(response["domain"])
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise DNSimple::DomainError.new(name, response["errors"])
  end
end

Instance Method Details

#add_service(id_or_short_name, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dnsimple/domain.rb', line 110

def add_service(id_or_short_name, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  options.merge!(:body => {:service => {:id => id_or_short_name}})
  response = self.class.post("#{DNSimple::Client.base_uri}/domains/#{name}/applied_services", options)
  pp response if DNSimple::Client.debug?
  case response.code
  when 200
    true
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise "Error: #{response.code}"
  end
end

#applied_services(options = {}) ⇒ Object



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

def applied_services(options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.class.get("#{Client.base_uri}/domains/#{name}/applied_services", options)
  pp response if DNSimple::Client.debug?
  case response.code
  when 200
    response.map { |r| DNSimple::Service.new(r["service"]) }
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise RuntimeError, "Error: #{response.code}"
  end
end

#apply(template, options = {}) ⇒ Object

Apply the given named template to the domain. This will add all of the records in the template to the domain.



39
40
41
42
43
44
# File 'lib/dnsimple/domain.rb', line 39

def apply(template, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  options.merge!(:body => {})
  template = resolve_template(template)
  self.class.post("#{DNSimple::Client.base_uri}/domains/#{name}/templates/#{template.id}/apply", options)
end

#available_services(options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dnsimple/domain.rb', line 96

def available_services(options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.class.get("#{DNSimple::Client.base_uri}/domains/#{name}/available_services", options)
  pp response if DNSimple::Client.debug?
  case response.code
  when 200
    response.map { |r| DNSimple::Service.new(r["service"]) }
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise RuntimeError, "Error: #{response.code}"
  end
end

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

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



31
32
33
34
# File 'lib/dnsimple/domain.rb', line 31

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

#remove_service(id, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dnsimple/domain.rb', line 125

def remove_service(id, options={})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  response = self.class.delete("#{DNSimple::Client.base_uri}/domains/#{name}/applied_services/#{id}", options)
  pp response if DNSimple::Client.debug?
  case response.code
  when 200
    true
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise "Error: #{response.code}"
  end
end

#resolve_template(template) ⇒ Object

:nodoc:



73
74
75
76
77
78
79
80
# File 'lib/dnsimple/domain.rb', line 73

def resolve_template(template)
  case template
  when DNSimple::Template
    template
  else
    DNSimple::Template.find(template)
  end
end

#update_name_servers(name_servers, options = {}) ⇒ Object

Overwrite the nameservers for a given domain.



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

def update_name_servers(name_servers, options = {})
  options.merge!(DNSimple::Client.standard_options_with_credentials)
  
  name_servers_hash = {}
  ns_index = 1
  
  name_servers.each do |name_server|
    name_servers_hash["ns#{ns_index}"] = name_server
    ns_index += 1
  end
  
  options.merge!(:body => { :name_servers => name_servers_hash })
  
  response = self.class.post("#{DNSimple::Client.base_uri}/domains/#{name}/name_servers.json", options)
  pp response if DNSimple::Client.debug?
  
  case response.code
    
  when 200
    name_servers
  else
    raise RuntimeError, "Error: #{response.code}"
  end
end