Class: DnsMadeEasy

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret, sandbox = false) ⇒ DnsMadeEasy

Returns a new instance of DnsMadeEasy.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dnsmadeeasyapi.rb', line 12

def initialize (api_key, api_secret, sandbox = false)
  raise "api_key is undefined" unless api_key
  raise "api_secret is undefined" unless api_secret
  
  @api_key = api_key
  @api_secret = api_secret
  
  self.class.headers request_headers
  
  if sandbox
    self.class.base_uri 'https://api.sandbox.dnsmadeeasy.com/V2.0'
  else
    self.class.base_uri 'https://api.dnsmadeeasy.com/V2.0'
  end
end

Instance Method Details

#create_a_record(domain_name, name, value, options = {}) ⇒ Object



112
113
114
115
# File 'lib/dnsmadeeasyapi.rb', line 112

def create_a_record(domain_name, name, value, options = {})
  # todo: match IPv4 for value
  self.create_record(domain_name, name, "A", value, options)
end

#create_aaaa_record(domain_name, name, value, options = {}) ⇒ Object



117
118
119
120
# File 'lib/dnsmadeeasyapi.rb', line 117

def create_aaaa_record(domain_name, name, value, options = {})
  # todo: match IPv6 for value
  self.create_record(domain_name, name, "AAAA", value, options)
end

#create_cname_record(domain_name, name, value, options = {}) ⇒ Object



132
133
134
135
# File 'lib/dnsmadeeasyapi.rb', line 132

def create_cname_record(domain_name, name, value, options = {})
  # todo: match CNAME value
  self.create_record(domain_name, name, "CNAME", value, options)
end

#create_domain(domain_name) ⇒ Object



66
67
68
# File 'lib/dnsmadeeasyapi.rb', line 66

def create_domain(domain_name)
  self.create_domains([domain_name])
end

#create_domains(names = Array()) ⇒ Object



62
63
64
# File 'lib/dnsmadeeasyapi.rb', line 62

def create_domains(names = Array())
  self.class.post('/dns/managed/', :body => { "names" => names }.to_json )
end

#create_httpred_record(domain_name, name, value, redirectType = "STANDARD - 302", description = "", keywords = "", title = "", options = {}) ⇒ Object



158
159
160
161
162
# File 'lib/dnsmadeeasyapi.rb', line 158

def create_httpred_record(domain_name, name, value, redirectType = "STANDARD - 302", description = "", keywords = "", title = "", options = {})
  options.merge!({ "redirectType" => redirectType, "description" => description, "keywords" => keywords, "title" => title })
  
  self.create_record(domain_name, name, "HTTPRED", value, options)
end

#create_mx_record(domain_name, name, priority, value, options = {}) ⇒ Object



146
147
148
149
150
# File 'lib/dnsmadeeasyapi.rb', line 146

def create_mx_record(domain_name, name, priority, value, options = {})    
  options.merge!({ "mxLevel" => priority })
  
  self.create_record(domain_name, name, "MX", value, options)
end

#create_ns_record(domain_name, name, value, options = {}) ⇒ Object



137
138
139
140
# File 'lib/dnsmadeeasyapi.rb', line 137

def create_ns_record(domain_name, name, value, options = {})
  # todo: match domainname for value
  self.create_record(domain_name, name, "NS", value, options)
end

#create_ptr_record(domain_name, name, value, options = {}) ⇒ Object



122
123
124
125
# File 'lib/dnsmadeeasyapi.rb', line 122

def create_ptr_record(domain_name, name, value, options = {})
  # todo: match PTR value
  self.create_record(domain_name, name, "PTR", value, options)
end

#create_record(domain_name, name, type, value, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/dnsmadeeasyapi.rb', line 103

def create_record(domain_name, name, type, value, options = {})
  id = get_id_by_domain(domain_name)
  
  body = { "name" => name, "type" => type, "value" => value, "ttl" => 3600, "gtdLocation" => "DEFAULT" }
  result = body.merge(options)
  
  self.class.post("/dns/managed/#{id}/records/", :body => result.to_json )
end

#create_spf_record(domain_name, name, value, options = {}) ⇒ Object



142
143
144
# File 'lib/dnsmadeeasyapi.rb', line 142

def create_spf_record(domain_name, name, value, options = {})
  self.create_record(domain_name, name, "SPF", value, options)
end

#create_srv_record(domain_name, name, priority, weight, port, value, options = {}) ⇒ Object



152
153
154
155
156
# File 'lib/dnsmadeeasyapi.rb', line 152

def create_srv_record(domain_name, name, priority, weight, port, value, options = {})
  options.merge!({ "priority" => priority, "weight" => weight, "port" => port })
  
  self.create_record(domain_name, name, "SRV", value, options)
end

#create_txt_record(domain_name, name, value, options = {}) ⇒ Object



127
128
129
130
# File 'lib/dnsmadeeasyapi.rb', line 127

def create_txt_record(domain_name, name, value, options = {})
  # todo: match TXT value
  self.create_record(domain_name, name, "TXT", value, options)
end

#delete_domain(domain_name) ⇒ Object



57
58
59
60
# File 'lib/dnsmadeeasyapi.rb', line 57

def delete_domain(domain_name)
  id = get_id_by_domain(domain_name)
  self.class.delete("/dns/managed/#{id}")
end

#delete_record(domain_name, record_id) ⇒ Object



97
98
99
100
101
# File 'lib/dnsmadeeasyapi.rb', line 97

def delete_record(domain_name, record_id)
  id = get_id_by_domain(domain_name)
  
  self.class.delete("/dns/managed/#{id}/records/#{record_id}/")
end

#delete_records(domain_name, ids = []) ⇒ Object



91
92
93
94
95
# File 'lib/dnsmadeeasyapi.rb', line 91

def delete_records(domain_name, ids = [])
  id = get_id_by_domain(domain_name)
  
  self.class.delete("/dns/managed/#{id}/records/", :body => ids.to_json)
end

#domain(domain_name) ⇒ Object



52
53
54
55
# File 'lib/dnsmadeeasyapi.rb', line 52

def domain(domain_name)
  id = get_id_by_domain(domain_name)
  self.class.get("/dns/managed/#{id}")
end

#domainsObject



48
49
50
# File 'lib/dnsmadeeasyapi.rb', line 48

def domains
  self.class.get('/dns/managed/')
end

#find_record_id(domain_name, name, type) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dnsmadeeasyapi.rb', line 79

def find_record_id(domain_name, name, type)
  records = self.records_for(domain_name)
  
  ids = records['data'].collect do |r|
    if r['name'] == name && r['type'] == type
      r['id']
    end
  end.compact
  
  ids
end

#get_id_by_domain(domain_name) ⇒ Object


————- DOMAINS ————-




43
44
45
46
# File 'lib/dnsmadeeasyapi.rb', line 43

def get_id_by_domain(domain_name)
  domain = self.class.get("/dns/managed/id/#{domain_name}")
  return domain['id']
end

#records_for(domain_name) ⇒ Object


————- RECORDS ————-




74
75
76
77
# File 'lib/dnsmadeeasyapi.rb', line 74

def records_for(domain_name)
  id = get_id_by_domain(domain_name)
  self.class.get("/dns/managed/#{id}/records")
end

#request_headersObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/dnsmadeeasyapi.rb', line 28

def request_headers
  request_date = Time.now.httpdate
  hmac = OpenSSL::HMAC.hexdigest('sha1', @api_secret, request_date)
  {
    'Accept' => 'application/json',
    'x-dnsme-apiKey' => @api_key,
    'x-dnsme-requestDate' => request_date,
    'x-dnsme-hmac' => hmac
  }
end