Skip to content

Instantly share code, notes, and snippets.

@lauer
Created January 22, 2012 18:26
Show Gist options
  • Save lauer/1658042 to your computer and use it in GitHub Desktop.
Save lauer/1658042 to your computer and use it in GitHub Desktop.
GratisDNS.dk ruby class
# Version 0.1
# Updated: 22. January 2012
# Created by Jesper Grann Laursen - powerlauer@gmail.com
require 'net/http'
require 'net/https'
require 'rubygems'
require 'hpricot'
class GratisDNS
BACKEND_URL = 'ssl.gratisdns.dk'
PATH = '/editdomains4.phtml'
attr_accessor :username, :password
attr_accessor :domains, :templates
def initialize(username, password)
@username = username;
@password = password;
end
private
def request(action)
params = { 'user' => @username, 'password' => @password, 'action' => action }
http = Net::HTTP.new(BACKEND_URL, 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
result = http.post(PATH, params.map{|k,v| "#{k}=#{v}"}.join('&'))
return result.body
end
def getWebpage
# Debuging, use static html
# html = File.open('respons_primary.html').read
html = request('primarydns')
doc = Hpricot(html)
return doc
end
def parsePrimaryDNS
# Stop, if domains already fetched
if !@templates.nil? and !@domains.nil?
return
end
puts "fetch domains"
@templates = Array.new
@domains = Array.new
current_template = nil
doc = getWebpage()
doc.search('input [@value=deleteprimarydns]').each do |row|
domainrow = row.parent.search("input [@name=user_domain]")
domainname = domainrow.first.attributes['value']
text = row.parent.parent.parent.parent.parent.parent.search("td").first.inner_html
# Template
if (/\.template/.match(domainname))
id = /\((\d+)\)/.match(text)[1]
templates.push( Hash[ "name", text, "id", id, "hash", domainname, "domains", Array.new ] )
current_template = templates.size-1
next
end
# template domain
if / /.match(text)
templates[current_template]['domains'].push(domainname)
# Single domains
else
domains.push(text)
end
end
end
public
def getPrimaryDomains
parsePrimaryDNS()
return @domains
end
def getTemplates
parsePrimaryDNS()
return @templates
end
def getPrimaryDomainsInTemplate(templateid)
parsePrimaryDNS()
@templates.each do |template|
if template['id'] == templateid
return template['domains']
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment