Skip to content

Commit

Permalink
Ruby sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisxmlapi committed Jul 31, 2018
1 parent 7b6dba9 commit cad9c77
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions ruby/bulk_whois_api_sample.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require 'json'
require 'net/https'
require 'openssl'
require 'uri'

domains = %w[
google.com
example.com
whoisxmlapi.com
twitter.com
]

csv_filename = 'whois_records.csv'

########################
# Fill in your details #
########################

def call_api(path, data)
base_data = {
username: 'Your bulk whois api username',
password: 'Your bulk whois api password',
outputFormat: 'json'
}

url = 'https://www.whoisxmlapi.com/BulkWhoisLookup/bulkServices' + path

body = base_data.merge(data).to_json

http_post(url, body)
end

def create_request(domains)
data = {
domains: domains
}

response = JSON.parse(call_api('/bulkWhois', data))

code = response['messageCode']
message = response['message']

raise "#{code}: #{message}" if code != 200

response['requestId']
end

def download(id, file_name)
data = {
requestId: id,
startIndex: 1
}

File.write(file_name, call_api('/download', data))
end

def get_records(id, start, max)
data = {
requestId: id,
startIndex: start,
maxRecords: max
}

JSON.parse(call_api('/getRecords', data))
end

def http_post(url, data)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)

# Connect using ssl
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)

# Set headers
request.add_field('Content-Type', 'application/json')
request.add_field('Accept', 'application/json')
request.body = data

http.request(request).body
end

puts 'Requesting bulk whois lookup...'

id = create_request(domains)

puts "Request created: #{id}"

puts 'Waiting for processing...'

loop do
response = get_records(id, domains.count + 1, 0)
records_left = response['recordsLeft']

puts "Records left: #{records_left}"

break if records_left.zero?

sleep 3
end

puts 'Downloading results...'

download(id, csv_filename)

puts "Saved to #{csv_filename}"

0 comments on commit cad9c77

Please sign in to comment.