2022-03-31 16:03:11 +02:00
|
|
|
require "dnsimple"
|
2022-04-09 12:59:36 +02:00
|
|
|
class DnsUpdateJob
|
2022-03-31 16:03:11 +02:00
|
|
|
|
2022-03-31 19:48:14 +02:00
|
|
|
def client
|
|
|
|
@client ||= Dnsimple::Client.new(access_token: Rails.application.credentials.simple_token)
|
|
|
|
end
|
|
|
|
def simple_id
|
|
|
|
Rails.application.credentials.simple_id
|
|
|
|
end
|
|
|
|
def my_ip
|
2022-04-09 12:59:36 +02:00
|
|
|
@ip ||= HTTParty.get("https://jsonip.com/")["ip"]
|
2022-03-31 19:48:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_record(name , host)
|
|
|
|
zones = client.zones.list_zone_records(simple_id, host, filter: { type: 'A' , name: name })
|
|
|
|
zones.data.first
|
|
|
|
end
|
|
|
|
|
2022-04-09 12:59:36 +02:00
|
|
|
def same_ip(name , host)
|
|
|
|
my_ip == find_record(name , host).content
|
2022-04-02 20:49:32 +02:00
|
|
|
end
|
|
|
|
|
2022-04-09 12:59:36 +02:00
|
|
|
def update_ip(name , host)
|
|
|
|
record = find_record(name , host)
|
|
|
|
client.zones.update_zone_record(simple_id, host , record.id , content: my_ip)
|
2022-03-31 16:03:11 +02:00
|
|
|
end
|
|
|
|
end
|