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.

Raises:

  • (ArgumentError)


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

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

Instance Method Details

#delete_record(key, kind) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/globodns_client/connection.rb', line 99

def delete_record(key, kind)
  zone = get_zone(key, kind)
  unless records = get_record(key, kind, zone)
    raise GlobodnsClient::NotFound, "Record not found for (#{key})"
  end
  response=[]
  records.each do |record|
    response << request('delete', 'record', nil, record[:id])
  end
  begin
    schedule_export
  rescue Exception
  ensure
    response
  end
end

#get_domain(key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/globodns_client/connection.rb', line 17

def get_domain(key)
  domain = key.dup
  domain.chomp!('.')
  res = {}
  while domain.count('.') >= 1
    res = request('get','domain', domain)
    if !res.empty?
      domain = ""
    else
      domain.gsub!(/^([a-zA-Z0-9\-_]*.)/,'')
    end
  end
  res unless res.empty?
end

#get_record(key, kind, zone) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/globodns_client/connection.rb', line 65

def get_record(key, kind, zone)
  zone.flatten!
  res = []
  zone = get_zone(key, kind) if zone.nil?
  host = get_host(key, zone, kind)
  response = request('get', 'record', host, zone[0][:domain][:id], kind)
  response.each do |r|
    res << r[kind.downcase.to_sym] unless r[kind.downcase.to_sym].nil?
  end
  res.empty? ? false : res
end

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



32
33
34
35
36
37
38
39
# File 'lib/globodns_client/connection.rb', line 32

def get_zone(key, kind = 'A')
  res = request('get','domain', key, nil, kind)
  if kind.eql?('A') && !res.empty?
    res
  else
    get_zone_recursive(key, kind)
  end
end

#get_zone_recursive(key, kind = 'A') ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/globodns_client/connection.rb', line 41

def get_zone_recursive(key, kind = 'A')
  if kind.eql?('A') or kind.eql?('CNAME') or kind.eql?('TXT')
    domain = key.split('.', 2).last
  elsif kind.eql?('PTR')
    if key.include?('in-addr.arpa')
      domain = key.split('.', 2).last
    else
      match = key.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' or kind == 'CNAME') or kind.eql?('TXT') || domain.count('.') > 2 && kind == 'PTR'
      res = get_zone_recursive(domain, kind)
    else
      raise GlobodnsClient::NotFound, "Couldn't find a proper zone for '#{key}'"
    end
  end
  res
end

#new_record(key, kind, value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/globodns_client/connection.rb', line 77

def new_record(key, kind, value)
  zone = get_domain key
  unless zone.nil?
    a_records = get_record(key, 'A', zone)
    case kind
    when 'A'
      raise GlobodnsClient::AlreadyExists, "Item (#{key}) already exists with reference (#{a_records[0][:content]})" if a_records.collect{|r| r[:content]}.include? value
    when 'CNAME'
      cname_records = get_record(key, 'CNAME', zone)
      raise GlobodnsClient::AlreadyExists, "Item (#{key}) already exists" if a_records or cname_records
    end

    host = get_host(key, zone, kind)
    response = request('post', 'record', host, zone[0][:domain][:id], kind, value)
  end
  begin
    schedule_export
  rescue Exception
  end
  response['record']
end

#set_token(token) ⇒ Object



13
14
15
# File 'lib/globodns_client/connection.rb', line 13

def set_token token
  @bearer_token = token
end