Class: DnsMadeEasy::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsmadeeasy/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, sandbox = false) ⇒ Api

Initialize the class with the api key, the secret key, and an optional flag to run the queries in DNSMadeEasy’s sandbox mode



56
57
58
59
60
# File 'lib/dnsmadeeasy/api.rb', line 56

def initialize(api_key, secret_key, sandbox=false)
  @api_key = api_key
  @secret_key = secret_key
  @sandbox = sandbox
end

Instance Method Details

#create_domain!(domain) ⇒ Object

Create a new domain entry. Returns a hash of the newly created domain entry. Throws DnsMadeEasy::InvalidDomainNameError, DnsMadeEasy::DuplicateDomainError, or DnsMadeEasy::AuthorizationFailedError



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dnsmadeeasy/api.rb', line 90

def create_domain!(domain)
  begin
    response = RestClient.put request_url(:path => "#{domain}"), {}, headers
    return JSON.parse(response)
  rescue RestClient::BadRequest => e
    error = JSON.parse(e.http_body)
    if error["error"][0] == "Invalid domain name."
      raise DnsMadeEasy::InvalidDomainNameError, domain
    else
      raise DnsMadeEasy::DuplicateDomainError, domain
    end
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#create_record!(domain, record) ⇒ Object

Create a new dns record record should be a hash with the following values:

:name        => hostname, e.g. www
:type        => e.g. :A, :CNAME, :MX, :TXT, :SRV, :NS, :AAAA, :HTTPRED, :PTR
:data        => publicname, e.g. "66.88.99.44" or "ec2-gibberish.amazonaws.com"
:gtdLocation => "DEFAULT", (optional, defaults to DEFAULT)
:ttl         => time to live, e.g. 300

for :CNAME record type, the domain name is automatically added to the entry unless the :data field ends in a . e.g., for domain sigfig.com, :data => “blog.google.com” for a cname entry resolves to blog.google.com.sigfig.com but :data => “blog.google.com.” resolves to blog.google.com.

Returns a hash of the newly created dns entry. Throws DnsMadeEasy::InvalidRecordError, DnsMadeEasy::UnknownDomainError, or DnsMadeEasy::AuthorizationFailedError



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dnsmadeeasy/api.rb', line 168

def create_record!(domain, record)
  validate_record(record)
  request_data = {
    :gtdLocation => "DEFAULT"
  }.merge(record)
  begin
    response = RestClient.post request_url(:path => "#{domain}/records"), request_data.to_json, headers(:content_type => "application/json")
    return JSON.parse(response)
  rescue RestClient::BadRequest => e
    raise DnsMadeEasy::InvalidRecordError, e.http_body
  rescue RestClient::ResourceNotFound => e
    raise DnsMadeEasy::UnknownDomainError, domain
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#delete_domain!(domain) ⇒ Object

Delete a domain entry. Returns true or throws an exception. Throws DnsMadeEasy::UnknownDomainError, DnsMadeEasy::RequestFailedError, or DnsMadeEasy::AuthorizationFailedError



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dnsmadeeasy/api.rb', line 109

def delete_domain!(domain)
  begin
    response = RestClient.delete request_url(:path => "#{domain}"), headers
    return true
  rescue RestClient::ResourceNotFound => e
    raise DnsMadeEasy::UnknownDomainError, domain
  rescue RestClient::BadRequest => e
    error = JSON.parse(e.http_body)
    raise DnsMadeEasy::RequestFailedError, error["error"][0]
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#delete_domains!Object

Delete all domains in the account. Returns true or throws an exception. Throws DnsMadeEasy::AuthorizationFailedError



78
79
80
81
82
83
84
85
# File 'lib/dnsmadeeasy/api.rb', line 78

def delete_domains!
  begin
    response = RestClient.delete request_url, headers
    return true
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#delete_record!(domain, record_id) ⇒ Object

Delete a dns record. Returns true or throws an exception. Throws DnsMadeEasy::UnknownRecordError, or DnsMadeEasy::AuthorizationFailedError



188
189
190
191
192
193
194
195
196
197
# File 'lib/dnsmadeeasy/api.rb', line 188

def delete_record!(domain, record_id)
  begin
    response = RestClient.delete request_url(:path => "#{domain}/records/#{record_id}"), headers
    return true
  rescue RestClient::ResourceNotFound => e
    raise DnsMadeEasy::UnknownRecordError.new(domain, record_id)
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#describe_domain(domain) ⇒ Object

Describe a domain entry. Returns a hash of the domain entry. Throws DnsMadeEasy::UnknownDomainError or DnsMadeEasy::AuthorizationFailedError



126
127
128
129
130
131
132
133
134
135
# File 'lib/dnsmadeeasy/api.rb', line 126

def describe_domain(domain)
  begin
    response = RestClient.get request_url(:path => "#{domain}"), headers
    return JSON.parse(response)
  rescue RestClient::ResourceNotFound => e
    raise DnsMadeEasy::UnknownDomainError, domain
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#describe_record(domain, record_id) ⇒ Object

Describe an existing dns record. Returns a hash of the dns entry. Throws DnsMadeEasy::UnknownRecordError, or DnsMadeEasy::AuthorizationFailedError



218
219
220
221
222
223
224
225
226
227
# File 'lib/dnsmadeeasy/api.rb', line 218

def describe_record(domain, record_id)
  begin
    response = RestClient.get request_url(:path => "#{domain}/records/#{record_id}"), headers
    return JSON.parse(response)
  rescue RestClient::ResourceNotFound => e
    raise DnsMadeEasy::UnknownRecordError.new(domain, record_id)
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#list_domainsObject

List all domains in the account. Returns an array of domain names in the account. Throws DnsMadeEasy::AuthorizationFailedError



65
66
67
68
69
70
71
72
73
# File 'lib/dnsmadeeasy/api.rb', line 65

def list_domains
  begin
    response = RestClient.get request_url, headers
    response_hash = JSON.parse(response)
    return response_hash.has_key?("list") ? response_hash["list"] : []
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#list_records(domain, filter = {}) ⇒ Object

List records for a domain. Returns an array of hashes, each hash representing a dns entry. Throws DnsMadeEasy::UnknownDomainError or DnsMadeEasy::AuthorizationFailedError



140
141
142
143
144
145
146
147
148
149
# File 'lib/dnsmadeeasy/api.rb', line 140

def list_records(domain, filter={})
  begin
    response = RestClient.get request_url(:path => "#{domain}/records", :query => filter), headers
    return JSON.parse(response)
  rescue RestClient::ResourceNotFound => e
    raise DnsMadeEasy::UnknownDomainError, domain
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end

#update_record!(domain, record_id, record) ⇒ Object

Update an existing dns record Returns the latest hash of the dns entry. Throws DnsMadeEasy::InvalidRecordError, DnsMadeEasy::UnknownRecordError, or DnsMadeEasy::AuthorizationFailedError



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/dnsmadeeasy/api.rb', line 202

def update_record!(domain, record_id, record)
  begin
    response = RestClient.put request_url(:path => "#{domain}/records/#{record_id}"), record.to_json, headers(:content_type => "application/json")
    return describe_record(domain, record_id)
  rescue RestClient::BadRequest => e
    raise DnsMadeEasy::InvalidRecordError, e.http_body
  rescue RestClient::ResourceNotFound => e
    raise DnsMadeEasy::UnknownRecordError.new(domain, record_id)
  rescue RestClient::Forbidden => e
    raise DnsMadeEasy::AuthorizationFailedError
  end
end