Class: Dapi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dapi/client.rb,
lib/dapi/client/error.rb,
lib/dapi/client/version.rb

Defined Under Namespace

Classes: Error, OptimisticLockFailure

Constant Summary collapse

OPTIMISTIC_LOCKING_CHECK_WAIT =
3.0
OPTIMISTIC_LOCKING_RETRIES =
5
VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(url:, token:, http: HTTP, check_wait: OPTIMISTIC_LOCKING_CHECK_WAIT, retries: OPTIMISTIC_LOCKING_RETRIES) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
# File 'lib/dapi/client.rb', line 11

def initialize(url:, token:, http: HTTP, check_wait: OPTIMISTIC_LOCKING_CHECK_WAIT, retries: OPTIMISTIC_LOCKING_RETRIES)
  @url = url
  @http = http
  @token = token
  @check_wait = check_wait
  @retries = retries
end

Instance Method Details

#delete_record(zone_name, lvalue, type) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/dapi/client.rb', line 19

def delete_record(zone_name, lvalue, type)
  modify_zone_records(
    zone_name,
    modify: ->(records) { records.reject { |r| r["name"] == lvalue && r["type"] == type } },
    check: ->(records) { not records.detect { |r| r["name"] == lvalue && r["type"] == type } }
  )
end

#get_zone(zone_name) ⇒ Object



27
28
29
# File 'lib/dapi/client.rb', line 27

def get_zone(zone_name)
  unwrap { http.get(@url + "zone/" + zone_name) }
end

#truncate_zone(zone_name) ⇒ Object

Useful for test suites, and staging domains



32
33
34
35
36
37
38
# File 'lib/dapi/client.rb', line 32

def truncate_zone(zone_name)
  modify_zone_records(
    zone_name,
    modify: ->(records) { records.reject { |r| r["type"] == "A" && r["name"] != "@" } },
    check: ->(records) { not records.detect { |r| r["type"] == "A" && r["name"] != "@" } }
  )
end

#upsert_record(zone_name, lvalue, type, rvalues, ttl = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dapi/client.rb', line 40

def upsert_record(zone_name, lvalue, type, rvalues, ttl = nil)
  rvalues = [rvalues].flatten
  modify_zone_records(
    zone_name,
    modify: ->(records) {
      records.
        reject { |r| r["name"] == lvalue && r["type"] == type }.
        concat(
          rvalues.map { |rvalue| {"name" => lvalue, "type" => type, "value" => rvalue, "ttl" => ttl} }
        )
    },
    check: ->(records) {
      rvalues.all? { |rvalue|
        records.detect { |r| r["name"] == lvalue && r["type"] == type && r["value"] == rvalue && r["ttl"] == ttl }
      }
    }
  )
end