Class: GlobodnsClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/globodns_client/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
# File 'lib/globodns_client/connection.rb', line 6

def initialize(options)
  @auth_token = options[:auth_token]
  @host = options[:host]
  @timeout = options[:timeout] || 30
  raise "You must inform the auth_token and host for GloboDNS" unless @auth_token && @host
end

Instance Method Details

#delete_record(fqdn, kind) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/globodns_client/connection.rb', line 58

def delete_record(fqdn, kind)
  zone = get_zone(fqdn, kind)
  unless record = get_record(fqdn, kind, zone)
    raise "Record not found for (#{fqdn})"
  end
  response = request('delete', 'record', nil, record['id'])
end

#get_record(fqdn, kind, zone = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/globodns_client/connection.rb', line 37

def get_record(fqdn, kind, zone = nil)
  zone = get_zone(fqdn, kind) if zone.nil?
  host = get_host(fqdn, zone, kind)
  response = request('get', 'record', host, zone['id'], kind)
  response.each do |r|
    return r[kind.downcase] unless r[kind.downcase].nil?
  end
  false
end

#get_zone(fqdn, kind = 'A') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/globodns_client/connection.rb', line 13

def get_zone(fqdn, kind = 'A')
  if kind.eql?('A')
    domain = fqdn.split('.', 2).last
  elsif kind.eql?('PTR')
    if fqdn.include?('in-addr.arpa')
      domain = fqdn.split('.', 2).last
    else
      match = fqdn.match(/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/)
      domain = (match[1..3]+["0"]).reverse.join('.')+'.in-addr.arpa'
    end
  else
    raise "Not implemented"
  end
  res = request('get','domain', domain, nil, kind)
  if res.empty?
    if domain.count('.') > 1 && kind == 'A' || domain.count('.') > 2 && kind == 'PTR'
      res = get_zone(domain, kind)
    else
      raise "Couldn't find a proper zone for '#{@fqdn}'"
    end
  end
  res.is_a?(Array) ? res[0]['domain'] : res
end

#new_record(fqdn, kind, value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/globodns_client/connection.rb', line 47

def new_record(fqdn, kind, value)
  zone = get_zone(fqdn, kind)
  if record = get_record(fqdn, kind, zone)
    raise "Address already (#{fqdn}) exists with ip (#{record['content']})"
  else
    host = get_host(fqdn, zone, kind)
    response = request('post', 'record', host, zone['id'], kind, value)
  end
  response['record']
end