Class: Porkbun::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/porkbun.rb,
lib/porkbun.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain) ⇒ Domain

Returns a new instance of Domain.



265
266
267
# File 'lib/porkbun.rb', line 265

def initialize(domain)
  @domain = domain.to_s.chomp('.')
end

Instance Attribute Details

#domain ⇒ Object (readonly)

Returns the value of attribute domain.



263
264
265
# File 'lib/porkbun.rb', line 263

def domain
  @domain
end

Class Method Details

.all ⇒ Object



269
270
271
# File 'lib/porkbun.rb', line 269

def self.all
  list_all[:domains].map { |item| new(item[:domain]) }
end

.create_record(hostname, options) ⇒ Object



349
350
351
# File 'lib/porkbun.rb', line 349

def create_record(hostname, options)
  Record.create_record(hostname, options)
end

.get_record(hostname) ⇒ Object



313
314
315
# File 'lib/porkbun.rb', line 313

def self.get_record(hostname)
  Record.find_record(hostname)
end

.import(file) ⇒ Object



354
355
356
# File 'lib/porkbun.rb', line 354

def import(file)
  Record.import_zone(file)
end

.list_all ⇒ Object



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

def self.list_all
  Porkbun.porkbun('domain/listAll')
end

.public_ip ⇒ Object



358
359
360
# File 'lib/porkbun.rb', line 358

def public_ip
  Record.public_ip
end

.update_dynamic(hostname, ip) ⇒ Object



318
319
320
# File 'lib/porkbun.rb', line 318

def self.update_dynamic(hostname, ip)
  Record.update_dynamic(hostname, ip)
end

Instance Method Details

#create_record(name, options) ⇒ Object



298
299
300
301
302
# File 'lib/porkbun.rb', line 298

def create_record(name, options)
  hostname = normalize_hostname(name)
  relative_name = hostname == domain ? '' : hostname.delete_suffix(".#{domain}")
  Record.create(options.merge(domain:, name: relative_name))
end

#delete_all_records ⇒ Object



305
306
307
# File 'lib/porkbun.rb', line 305

def delete_all_records
  Record.delete_all(domain)
end

#get_record(name) ⇒ Object

Raises:



290
291
292
293
294
295
296
# File 'lib/porkbun.rb', line 290

def get_record(name)
  matches = records_for(name)
  raise Error, 'No record found for hostname' if matches.empty?
  raise Error, 'Multiple records found for hostname' if matches.length > 1

  matches.first
end

#records ⇒ Object



277
278
279
# File 'lib/porkbun.rb', line 277

def records
  Record.list(domain)
end

#records_for(target) ⇒ Object



281
282
283
284
285
286
287
288
# File 'lib/porkbun.rb', line 281

def records_for(target)
  requested = normalize_hostname(target)
  records = Record.list(domain)
  raise records if records.is_a?(Error)
  return records if requested == domain

  records.select { |record| Record.record_hostname(record, domain) == requested }
end

#to_s ⇒ Object



273
274
275
# File 'lib/porkbun.rb', line 273

def to_s
  domain
end

#update_dynamic(name, ip) ⇒ Object



322
323
324
325
326
327
328
329
330
# File 'lib/porkbun.rb', line 322

def update_dynamic(name, ip)
  matches = records_for(name)
  return { created: true, record: create_record(name, type: 'A', content: ip) } if matches.empty?

  record = get_record(name)
  display = record.to_s
  record.update(content: ip)
  { created: false, display:, record: }
end

#zone_file ⇒ Object



309
310
311
# File 'lib/porkbun.rb', line 309

def zone_file
  records.map(&:to_s).join("\n") + "\n"
end