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.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/dnsimple/domain.rb', line 203

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dnsimple/domain.rb', line 114

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.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dnsimple/domain.rb', line 133

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.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/dnsimple/domain.rb', line 184

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.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dnsimple/domain.rb', line 154

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



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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dnsimple/domain.rb', line 56

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



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dnsimple/domain.rb', line 70

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



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dnsimple/domain.rb', line 99

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:



47
48
49
50
51
52
53
54
# File 'lib/dnsimple/domain.rb', line 47

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