Class: Dnsync::Nsone
- Inherits:
-
Object
- Object
- Dnsync::Nsone
- Defined in:
- lib/dnsync/nsone.rb
Instance Method Summary collapse
- #connection ⇒ Object
- #create_record(record) ⇒ Object
-
#initialize(api_key, domain) ⇒ Nsone
constructor
A new instance of Nsone.
- #record_for(fqdn, record_type) ⇒ Object
- #remove_record(record) ⇒ Object
- #update_record(record) ⇒ Object
- #zone ⇒ Object
Constructor Details
#initialize(api_key, domain) ⇒ Nsone
Returns a new instance of Nsone.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/dnsync/nsone.rb', line 3 def initialize(api_key, domain) unless api_key.present? raise ArgumentError, "api_key must be specified" end unless domain.present? raise ArgumentError, "domain must be specified" end @api_key = api_key @domain = domain end |
Instance Method Details
#connection ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dnsync/nsone.rb', line 16 def connection @connection ||= Faraday.new('https://api.nsone.net/v1/') do |conn| conn.request :json # conn.response :logger conn.response :raise_error conn.response :json, :content_type => /\bjson$/ conn.adapter Faraday.default_adapter conn.headers['X-NSONE-Key'] = @api_key conn..timeout = 5 conn..open_timeout = 5 end end |
#create_record(record) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dnsync/nsone.rb', line 69 def create_record(record) answers = record.answers.map do |answer| if answer.priority { :answer => [ answer.priority, answer.content ] } else { :answer => [ answer.content ] } end end connection.put("zones/#{@domain}/#{record.name}/#{record.type}") do |req| req.body = { :type => record.type, :zone => @domain, :domain => record.name, :ttl => record.ttl, :answers => answers } end end |
#record_for(fqdn, record_type) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dnsync/nsone.rb', line 43 def record_for(fqdn, record_type) record = connection.get("zones/#{@domain}/#{fqdn}/#{record_type}").body answers = record['answers'].map do |answer_record| case answer_record['answer'].length when 2 priority, content = *answer_record['answer'] when 1 content = answer_record['answer'].first else raise "Unknown answer format: #{answer_record.inspect}" end Answer.new(content, priority) end Record.new(record['domain'], record['type'], record['ttl'], answers) rescue Faraday::ClientError => ex if ex.response && ex.response[:status].to_i == 429 sleep 0.4 + rand retry else raise end end |
#remove_record(record) ⇒ Object
109 110 111 |
# File 'lib/dnsync/nsone.rb', line 109 def remove_record(record) connection.delete("zones/#{@domain}/#{record.name}/#{record.type}") end |
#update_record(record) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dnsync/nsone.rb', line 89 def update_record(record) answers = record.answers.map do |answer| if answer.priority { :answer => [ answer.priority, answer.content ] } else { :answer => [ answer.content ] } end end connection.post("zones/#{@domain}/#{record.name}/#{record.type}") do |req| req.body = { :type => record.type, :zone => @domain, :domain => record.name, :ttl => record.ttl, :answers => answers } end end |