Tweetegy On the edge of chaos with Ruby, Rails, JavaScript and AngularJS.

| About | Search | Archive | Github | RSS |

Update ZoneEdit NameServers via a Restful API in Ruby

I recently had to programmatically alter entries in ZoneEdit. While there is some documentation, it is difficult to get started quickly. This is especially true if you want to use Ruby since there are no examples written in Ruby that I could find. So I wrote a simple class to do just what I need and here it is:

 1 require 'net/http'
 2 require 'net/https'
 3 require 'uri'
 4 
 5 class ZoneEdit
 6 
 7  #Initialize with the ZoneEdit username and password credentials
 8  def initialize(username, password)
 9      @@username = username
10      @@password = password
11  end
12 
13  #This method can be used to change a dns record in ZoneEdit
14  #Pass in the account username and zone you want to manage as well as the dnsfrom (sub-domain) forward address and 0 or 1 to shadow (cloak) or not
15  def web_forward(user,zone,dnsfrom,forward,shadow)
16      return send_command("command=ChangeRecord&user=#{user}&zone=#{zone}&type=WF&dnsfrom=#{dnsfrom}&forward=#{forward}&shadow=#{shadow}")
17  end
18 
19  private
20  def send_command(command)
21      puts "https://www.zoneedit.com/auth/admin/command.html?#{command}"
22      @http=Net::HTTP.new('www.zoneedit.com', 443)
23      @http.use_ssl = true
24      @http.start() {|http|
25          req = Net::HTTP::Get.new("/auth/admin/command.html?#{command}")
26          req.basic_auth @@username, @@password
27          response = http.request(req)
28          print response.body
29      }
30  end
31 
32 end

It's usage is simply as follows:

1 ze = ZoneEdit.new("someuser", "apassword")
2 ze.web_forward "someuser", "somedomain.com", "somesubdomain", "http://anewdomain.com", "1"