Class: DNSRecordUpdater::DNSUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/dns_record_updater/dns_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(provider, notification_service: nil) ⇒ DNSUpdater

Returns a new instance of DNSUpdater.



5
6
7
8
# File 'lib/dns_record_updater/dns_updater.rb', line 5

def initialize(provider, notification_service: nil)
  @provider = provider
  @notification_service = notification_service
end

Instance Method Details

#update(send_notification: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dns_record_updater/dns_updater.rb', line 10

def update(send_notification: false)
  ip = fetch_public_ip
  puts "Detected public IP: #{ip}"

  dns_record = @provider.fetch_dns_record
  record_id = dns_record[:record_id]
  current_ip = dns_record[:current_ip]

  if ip == current_ip
    puts "IP has not changed: #{ip}"
    return true
  end

  success = @provider.update_dns_record(ip, record_id)
  if send_notification && success
    @notification_service&.notify("DNS record updated: #{@provider.record_name} -> #{ip}")
  end
  success
rescue StandardError => e
  puts "Error during update: #{e.message}"
  false
end