Class: DnsMadeEasy::Api::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret, sandbox = false, options = {}) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dnsmadeeasy/api/client.rb', line 26

def initialize(api_key, api_secret, sandbox = false, options = {})
  fail 'api_key is undefined' unless api_key
  fail 'api_secret is undefined' unless api_secret

  @api_key            = api_key
  @api_secret         = api_secret
  @options            = options
  @requests_remaining = -1
  @request_limit      = -1

  sandbox ? on_sandbox : on_production
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



20
21
22
# File 'lib/dnsmadeeasy/api/client.rb', line 20

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



20
21
22
# File 'lib/dnsmadeeasy/api/client.rb', line 20

def api_secret
  @api_secret
end

#base_uriObject

Returns the value of attribute base_uri.



19
20
21
# File 'lib/dnsmadeeasy/api/client.rb', line 19

def base_uri
  @base_uri
end

#request_limitObject (readonly)

Returns the value of attribute request_limit.



20
21
22
# File 'lib/dnsmadeeasy/api/client.rb', line 20

def request_limit
  @request_limit
end

#requests_remainingObject (readonly)

Returns the value of attribute requests_remaining.



20
21
22
# File 'lib/dnsmadeeasy/api/client.rb', line 20

def requests_remaining
  @requests_remaining
end

Class Method Details

.public_operationsObject



14
15
16
# File 'lib/dnsmadeeasy/api/client.rb', line 14

def public_operations
  (new('a', 'b').methods - Object.methods).map(&:to_s).reject { |r| r =~ /[=]$|^(api_|on_|request)/ }.sort
end

Instance Method Details

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

Specific record types



143
144
145
146
# File 'lib/dnsmadeeasy/api/client.rb', line 143

def create_a_record(domain_name, name, value, options = {})
  # TODO: match IPv4 for value
  create_record domain_name, name, 'A', value, options
end

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



149
150
151
152
# File 'lib/dnsmadeeasy/api/client.rb', line 149

def create_aaaa_record(domain_name, name, value, options = {})
  # TODO: match IPv6 for value
  create_record domain_name, name, 'AAAA', value, options
end

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



167
168
169
170
# File 'lib/dnsmadeeasy/api/client.rb', line 167

def create_cname_record(domain_name, name, value, options = {})
  # TODO: match CNAME value
  create_record domain_name, name, 'CNAME', value, options
end

#create_domain(domain_name) ⇒ Object



79
80
81
# File 'lib/dnsmadeeasy/api/client.rb', line 79

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

#create_domains(names) ⇒ Object



74
75
76
# File 'lib/dnsmadeeasy/api/client.rb', line 74

def create_domains(names)
  post('/dns/managed/', names: names)
end

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



197
198
199
200
# File 'lib/dnsmadeeasy/api/client.rb', line 197

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)
  create_record domain_name, name, 'HTTPRED', value, options
end

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



184
185
186
187
188
# File 'lib/dnsmadeeasy/api/client.rb', line 184

def create_mx_record(domain_name, name, priority, value, options = {})
  options.merge!('mxLevel' => priority)

  create_record domain_name, name, 'MX', value, options
end

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



173
174
175
176
# File 'lib/dnsmadeeasy/api/client.rb', line 173

def create_ns_record(domain_name, name, value, options = {})
  # TODO: match domainname for value
  create_record domain_name, name, 'NS', value, options
end

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



155
156
157
158
# File 'lib/dnsmadeeasy/api/client.rb', line 155

def create_ptr_record(domain_name, name, value, options = {})
  # TODO: match PTR value
  create_record domain_name, name, 'PTR', value, options
end

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



135
136
137
138
# File 'lib/dnsmadeeasy/api/client.rb', line 135

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

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



179
180
181
# File 'lib/dnsmadeeasy/api/client.rb', line 179

def create_spf_record(domain_name, name, value, options = {})
  create_record domain_name, name, 'SPF', value, options
end

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



191
192
193
194
# File 'lib/dnsmadeeasy/api/client.rb', line 191

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

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



161
162
163
164
# File 'lib/dnsmadeeasy/api/client.rb', line 161

def create_txt_record(domain_name, name, value, options = {})
  # TODO: match TXT value
  create_record domain_name, name, 'TXT', value, options
end

#delete_all_records(domain_name) ⇒ Object



129
130
131
132
# File 'lib/dnsmadeeasy/api/client.rb', line 129

def delete_all_records(domain_name)
  domain_id = get_id_by_domain(domain_name)
  delete "/dns/managed/#{domain_id}/records"
end

#delete_domain(domain_name) ⇒ Object



69
70
71
# File 'lib/dnsmadeeasy/api/client.rb', line 69

def delete_domain(domain_name)
  delete "/dns/managed/#{get_id_by_domain(domain_name)}"
end

#delete_record(domain_name, record_id) ⇒ Object



116
117
118
# File 'lib/dnsmadeeasy/api/client.rb', line 116

def delete_record(domain_name, record_id)
  delete "/dns/managed/#{get_id_by_domain(domain_name)}/records/#{record_id}/"
end

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



121
122
123
124
125
126
# File 'lib/dnsmadeeasy/api/client.rb', line 121

def delete_records(domain_name, ids = [])
  return if ids.empty?
  domain_id = get_id_by_domain(domain_name)

  delete "/dns/managed/#{domain_id}/records?ids=#{ids.join(',')}"
end

#domain(domain_name) ⇒ Object



64
65
66
# File 'lib/dnsmadeeasy/api/client.rb', line 64

def domain(domain_name)
  get "/dns/managed/#{get_id_by_domain(domain_name)}"
end

#domainsObject



59
60
61
# File 'lib/dnsmadeeasy/api/client.rb', line 59

def domains
  get '/dns/managed/'
end

#find_all(domain_name, name, type) ⇒ Object



96
97
98
99
100
# File 'lib/dnsmadeeasy/api/client.rb', line 96

def find_all(domain_name, name, type)
  records = records_for(domain_name)
  return nil unless records
  records['data'].select { |r| r['name'] == name && r['type'] == type }
end

#find_first(domain_name, name, type) ⇒ Object



103
104
105
106
107
# File 'lib/dnsmadeeasy/api/client.rb', line 103

def find_first(domain_name, name, type)
  records = records_for(domain_name)
  return nil unless records
  records['data'].detect { |r| r['name'] == name && r['type'] == type }
end

#find_record_ids(domain_name, name, type) ⇒ Object



110
111
112
113
# File 'lib/dnsmadeeasy/api/client.rb', line 110

def find_record_ids(domain_name, name, type)
  records = records_for(domain_name)
  records['data'].select { |r| r['name'] == name && r['type'] == type }.map { |r| r['id'] }
end

#get_id_by_domain(domain_name) ⇒ Object


————- DOMAINS ————-




54
55
56
# File 'lib/dnsmadeeasy/api/client.rb', line 54

def get_id_by_domain(domain_name)
  get("/dns/managed/id/#{domain_name}")['id']
end

#on_production(&block) ⇒ Object



45
46
47
# File 'lib/dnsmadeeasy/api/client.rb', line 45

def on_production(&block)
  with_url(::DnsMadeEasy::API_BASE_URL_PRODUCTION, &block)
end

#on_sandbox(&block) ⇒ Object



40
41
42
# File 'lib/dnsmadeeasy/api/client.rb', line 40

def on_sandbox(&block)
  with_url(::DnsMadeEasy::API_BASE_URL_SANDBOX, &block)
end

#records_for(domain_name) ⇒ Object Also known as: all


————- RECORDS ————-




88
89
90
# File 'lib/dnsmadeeasy/api/client.rb', line 88

def records_for(domain_name)
  get "/dns/managed/#{get_id_by_domain(domain_name)}/records"
end

#update_record(domain, record_id, name, type, value, options = {}) ⇒ Object



203
204
205
206
# File 'lib/dnsmadeeasy/api/client.rb', line 203

def update_record(domain, record_id, name, type, value, options = {})
  body = { 'name' => name, 'type' => type, 'value' => value, 'ttl' => 3600, 'gtdLocation' => 'DEFAULT', 'id' => record_id }
  put "/dns/managed/#{get_id_by_domain(domain)}/records/#{record_id}/", body.merge(options)
end

#update_records(domain, records, options = {}) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/dnsmadeeasy/api/client.rb', line 209

def update_records(domain, records, options = {})
  body = records.map do |record|
    {
      'id'          => record['id'],
      'name'        => record['name'],
      'type'        => record['type'],
      'value'       => record['value'],
      'gtdLocation' => record['gtdLocation'],
      'ttl'         => record['ttl']
    }.merge(options)
  end
  put "/dns/managed/#{get_id_by_domain(domain)}/records/updateMulti/", body
end