Sha256: 4464f06b4f195b296a7ef4d3320a012caca44584c1de14f75316984dfe25dc0c

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require "zonesync/record"
require "zonesync/http"

module Zonesync
  class Cloudflare < Provider
    def read
      http.get("/export")
    end

    def remove record
      id = records.fetch(record)
      http.delete("/#{id}")
    end

    def change old_record, new_record
      id = records.fetch(old_record)
      http.patch("/#{id}", {
        name: new_record[:name],
        type: new_record[:type],
        ttl: new_record[:ttl],
        content: new_record[:rdata],
      })
    end

    def add record
      http.post(nil, {
        name: record[:name],
        type: record[:type],
        ttl: record[:ttl],
        content: record[:rdata],
      })
    end

    def records
      @records ||= begin
        response = http.get(nil)
        response["result"].reduce({}) do |map, attrs|
          map.merge attrs["id"] => Record.new(
            attrs["name"] + ".", # normalize to trailing period
            attrs["type"],
            attrs["ttl"].to_i,
            attrs["content"],
          ).to_h
        end.invert
      end
    end

    private

    def http
      return @http if @http
      @http = HTTP.new("https://api.cloudflare.com/client/v4/zones/#{credentials[:zone_id]}/dns_records")
      @http.before_request do |request|
        request["Content-Type"] = "application/json"
        request["X-Auth-Email"] = credentials[:email]
        request["X-Auth-Key"] = credentials[:key]
      end
      @http.after_response do |response|
      end
      @http
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zonesync-0.4.0 lib/zonesync/cloudflare.rb
zonesync-0.3.0 lib/zonesync/cloudflare.rb
zonesync-0.2.0 lib/zonesync/cloudflare.rb
zonesync-0.1.2 lib/zonesync/cloudflare.rb