Class: GlobodnsClient::Connection
- Inherits:
-
Object
- Object
- GlobodnsClient::Connection
- Defined in:
- lib/globodns_client/connection.rb
Instance Method Summary collapse
- #delete_record(key, kind, content = [], remove_all = true) ⇒ Object
- #get_domain(key) ⇒ Object
- #get_record(key, kind, zone) ⇒ Object
- #get_record_matching(key, kind, zone, content = []) ⇒ Object
- #get_zone(key, kind = 'A') ⇒ Object
- #get_zone_recursive(key, kind = 'A') ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #new_record(key, kind, value) ⇒ Object
- #set_token(token) ⇒ Object
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() @bearer_token = [:bearer_token] @host = [:host] @timeout = [: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, content = [], remove_all = true) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/globodns_client/connection.rb', line 112 def delete_record(key, kind, content = [], remove_all = true) if !remove_all and content.empty? raise GlobodnsClient::MissingContent, "Record not found for (#{key}) with content (#{content})" end zone = get_zone(key, kind) if remove_all and content.empty? records = get_record(key, kind, zone) else records = get_record_matching(key, kind, zone, content) end unless records 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_record_matching(key, kind, zone, content = []) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/globodns_client/connection.rb', line 77 def get_record_matching(key, kind, zone, content=[]) matching = [] records = get_record(key, kind, zone) return false unless records records.each do |record| matching << record if content.include? record[:content] end matching.empty? ? false : matching 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/globodns_client/connection.rb', line 90 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 and 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 |