Created
May 29, 2011 16:11
-
-
Save razum2um/997894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/bin/ruby-whois b/bin/ruby-whois | |
index 46cc9af..7499de1 100755 | |
--- a/bin/ruby-whois | |
+++ b/bin/ruby-whois | |
@@ -5,7 +5,7 @@ $:.unshift(File.expand_path("../../lib", __FILE__)) | |
require 'optparse' | |
require 'ostruct' | |
require 'whois' | |
- | |
+require 'json' | |
options = OpenStruct.new | |
OptionParser.new do |opts| | |
@@ -48,8 +48,9 @@ end | |
qstring = ARGV.shift | |
begin | |
- @client = Whois::Client.new(:timeout => options.timeout) | |
- puts @client.query(qstring) | |
+ #@client = Whois::Client.new(:timeout => options.timeout) | |
+ #puts @client.query(qstring) | |
+ puts JSON.dump(Whois.whois(qstring).to_json()) | |
rescue Whois::Error => e | |
abort(e.message) | |
rescue Timeout::Error => e | |
diff --git a/lib/whois/record.rb b/lib/whois/record.rb | |
index f4c8790..1ef8d42 100644 | |
--- a/lib/whois/record.rb | |
+++ b/lib/whois/record.rb | |
@@ -43,6 +43,52 @@ module Whois | |
super || Parser::PROPERTIES.include?(symbol) || Parser::METHODS.include?(symbol) | |
end | |
+ def bool_to_int(bool) | |
+ if bool == true | |
+ return 1 | |
+ else | |
+ return 0 | |
+ end | |
+ end | |
+ | |
+ # Returns a json representation of this record. | |
+ # | |
+ # @return [String] The record content. | |
+ def to_json() | |
+ nss = [] | |
+ self.nameservers.each do |ns| | |
+ nss << ns.to_s | |
+ end | |
+ jregistrar = self.registrar.to_json() | |
+ jregistrart_contact = self.registrant_contact.to_json() | |
+ jadmin_contact = self.admin_contact.to_json() | |
+ jtechnical_contact = self.technical_contact.to_json() | |
+ #jnameservers = self.nameservers | |
+ | |
+ return { | |
+ 'domain' => self.domain.to_s, | |
+ 'id' => self.object_id.to_s, | |
+ 'created_on' => self.created_on.to_i, | |
+ 'updated_on' => self.updated_on.to_i, | |
+ 'expires_on' => self.expires_on.to_i, | |
+ 'technical_contact' => { | |
+ 'id' => self.technical_contact.id.to_s, | |
+ 'name' => self.technical_contact.name.to_s }, | |
+ 'registered' => self.bool_to_int(self.registered?).to_s, | |
+ 'available' => self.bool_to_int(self.available?).to_s, | |
+ 'nameservers' => nss, | |
+ 'disclaimer' => self.disclaimer.to_s, | |
+ 'domain' => self.domain.to_s, | |
+ 'domain_id' => self.domain_id.to_s, | |
+ 'referral_url' => self.referral_url.to_s, | |
+ 'status' => self.status.to_s, | |
+ 'registrar' => jregistrar, | |
+ 'technical_contact' => jtechnical_contact, | |
+ 'admin_contact' => jadmin_contact, | |
+ 'registrart_contact' => jregistrart_contact | |
+ } | |
+ end | |
+ | |
# Returns a String representation of this record. | |
# | |
# @return [String] The record content. | |
diff --git a/lib/whois/record/contact.rb b/lib/whois/record/contact.rb | |
index 1278e72..b10e74f 100644 | |
--- a/lib/whois/record/contact.rb | |
+++ b/lib/whois/record/contact.rb | |
@@ -47,6 +47,25 @@ module Whois | |
TYPE_ADMIN = 2 | |
TYPE_TECHNICAL = 3 | |
+ def to_json() | |
+ return { | |
+ 'id' => self.id.to_s, | |
+ 'name' => self.name.to_s, | |
+ 'organization' => self.organization.to_s, | |
+ 'address' => self.address.to_s, | |
+ 'city' => self.city.to_s, | |
+ 'zip' => self.zip.to_s, | |
+ 'fax' => self.fax.to_s, | |
+ 'state' => self.state.to_s, | |
+ 'country' => self.country.to_s, | |
+ 'country_code' => self.country_code.to_s, | |
+ 'phone' => self.phone.to_s, | |
+ 'fax' => self.fax.to_s, | |
+ 'email' => self.email.to_s, | |
+ 'created_on' => self.created_on.to_i, | |
+ 'updated_on' => self.updated_on.to_i, | |
+ } | |
+ end | |
end | |
end | |
diff --git a/lib/whois/record/registrar.rb b/lib/whois/record/registrar.rb | |
index 4093bf0..494360e 100644 | |
--- a/lib/whois/record/registrar.rb | |
+++ b/lib/whois/record/registrar.rb | |
@@ -28,7 +28,16 @@ module Whois | |
# @attr [String] url | |
# | |
class Registrar < SuperStruct.new(:id, :name, :organization, :url) | |
+ def to_json() | |
+ return { | |
+ 'id' => self.id.to_s, | |
+ 'name' => self.name.to_s, | |
+ 'organization' => self.organization.to_s, | |
+ 'url' => self.url.to_s, | |
+ } | |
+ end | |
end | |
+ | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment