Sha256: 3c7f627e3fb140162fb9af69799b6777ee025c86f140f2e082ed29fc397e5e6f

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require 'cloudflare'

module CloudFlare
  module DynamicDNS
    # Update DNS zone / domain information in cloudflare with the new IP address if it has changed.
    class Updater
      # @param [String] key
      # @param [String] email
      # @param [String] zone
      # @param [String] hostname
      # @param [String] ipv4
      def initialize(key:, email:, zone:, hostname:, ipv4:)
        @key = key
        @email = email
        @zone = zone
        @hostname = hostname
        @ip = ipv4
      end

      # Update Cloudflare with the new IP addr
      def update
        current_config
        if changed?
          logger.info "=> updating #{dns_record.record[:name]} to #{ip}"
          dns_record.update_content(ip)
        else
          logger.info '=> No changes detected.'
        end
      end

      private

      def current_config
        logger.info "=> #{dns_record.record[:name]} currently pointing to -> #{dns_record.record[:content]}"
        logger.info "=> current public IP of your network is #{ip}."
      end

      def changed?
        ip != dns_record.record[:content]
      end

      def dns_record
        @dns_record ||= dns_zone.dns_records.find_by_name(hostname)
      end

      def dns_zone
        @dns_zone ||= connection.zones.find_by_name(zone)
      end

      def connection
        @connection ||= Cloudflare.connect(key: key, email: email)
      end

      def logger
        Logger.new(STDOUT)
      end

      attr_reader :key, :email, :zone, :hostname, :ip
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudflare-dynamic-dns-0.1.0 lib/cloud_flare/dynamic_dns/updater.rb