class WhoisXMLAPI::Client
Public Class Methods
send_rwhois_request(entity_name, since_dt=nil)
click to toggle source
Reverse Whois
# File lib/whoisxmlapi/client.rb, line 132 def self.send_rwhois_request(entity_name, since_dt=nil) params = { :apiKey => WhoisXMLAPI.api_key, :searchType => "current", :mode => WhoisXMLAPI.rwhois_mode, :responseFormat => 'json', :basicSearchTerms => { :include => [entity_name] # must be an array of strings! } } params[:createdDateFrom] = since_dt.strftime("%F") if since_dt begin # To DEBUG add ":debug_output => $stdout" r = HTTParty.post(WhoisXMLAPI.rwhois_path, :body => params.to_json, :timeout => 120, :headers => {'Content-Type' => 'application/json'}) rescue StandardError => e WhoisXMLAPI.logger.info "WhoisXMLAPI#rwhois - Error getting RWhois info for \'#{entity_name}\': #{e}" r = nil end if WhoisXMLAPI.callbacks[:rwhois] WhoisXMLAPI.callbacks[:rwhois].each do |cb| cb.call end end return r end
Public Instance Methods
account_balance()
click to toggle source
GET user.whoisxmlapi.com/service/account-balance?apiKey=YOUR_API_KEY wc.account_balance
>¶ ↑
{
"Email Verification API" => {:product_id => 7, :credits => 1000}, "IP Geolocation API" => {:product_id => 8, :credits => 1000}, "Domain Research Suite" => {:product_id => 14, :credits => 100}, "WHOIS API" => {:product_id => 1, :credits => 1000}, "Domain Reputation API" => {:product_id => 20, :credits => 100}, "IP Netblocks API" => {:product_id => 23, :credits => 1000}, "Domain Availability API" => {:product_id => 25, :credits => 100}, "Screenshot API" => {:product_id => 27, :credits => 500}, "Website Categorization API" => {:product_id => 21, :credits => 100}, "Website Contacts API" => {:product_id => 29, :credits => 100}, "DNS Lookup API" => {:product_id => 26, :credits => 500}
}
# File lib/whoisxmlapi/client.rb, line 232 def account_balance result = nil params = { :apiKey => WhoisXMLAPI.api_key } begin r = HTTParty.get(WhoisXMLAPI.account_path, :query => params, :timeout => 120) rescue StandardError => e WhoisXMLAPI.logger.info "WhoisXMLAPI#account_balance - Error getting account_balance: #{e}" r = nil end if r && r.response.is_a?(Net::HTTPOK) if r.parsed_response # WhoisXML and HTTParty are sometimes returning a Hash and not a JSON string as requested, # which is causing an error of "no implicit conversion of Hash into String" when we call JSON.parse. # Logger message is to monitor whether WhoisXML and/or HTTParty will still return a string on occasion. if r.parsed_response.is_a?(String) WhoisXMLAPI.logger.debug "WhoisXMLAPI#account_balance - passed back parsed_response as a String instead of a Hash." presponse = JSON.parse(r.parsed_response) rescue nil else presponse = r.parsed_response end if presponse.nil? result = {:error => {message: 'Failed to parse response from API.'}} elsif presponse["error"] result = {:error => {message: presponse["error"]}} elsif presponse["data"] result = {} presponse["data"].each do |datum| key = datum["product"]["name"] result[key] = {product_id: datum["product_id"], credits: datum["credits"]} end end end else result = {:error =>{code: r.response.code, message: r.response.message}} end result end
exists?(domain)
click to toggle source
# File lib/whoisxmlapi/client.rb, line 97 def exists?(domain) params = { :cmd => 'GET_DN_AVAILABILITY', :domainName => domain, :outputFormat => 'JSON', :apiKey => WhoisXMLAPI.api_key } begin r = HTTParty.get(WhoisXMLAPI.whois_path, :query => params, :timeout => 120) rescue StandardError => e WhoisXMLAPI.logger.info "WhoisXMLAPI#whois - Error getting Whois info for \'#{domain}\': #{e}" res = WhoisXMLAPI::Unavailable.new res.parse(domain) return res end # WhoisXML and HTTParty are sometimes returning a Hash and not a JSON string as requested, # which is causing an error of "no implicit conversion of Hash into String" when we call JSON.parse. # Logger message is to monitor whether WhoisXML and/or HTTParty will still return a string on occasion. if r.parsed_response.is_a?(String) WhoisXMLAPI.logger.debug "WhoisXMLAPI#exists? - passed back parsed_response as a String instead of a Hash for \'#{domain}\'." presponse = JSON.parse(r.parsed_response) rescue nil else presponse = r.parsed_response end presponse && presponse["DomainInfo"]["domainAvailability"] == "UNAVAILABLE" end
rwhois(entity_name, since_dt=nil, owner=nil)
click to toggle source
reverse-whois-api.whoisxmlapi.com/docs POST reverse-whois-api.whoisxmlapi.com/api/v2
# File lib/whoisxmlapi/client.rb, line 192 def rwhois(entity_name, since_dt=nil, owner=nil) # entity_name is really the domain being passed in for Rwhois not the actual entity name return nil if entity_name.nil? query = WhoisXMLAPI::RWhoisResult.where(:entity_name => entity_name, :rwhoisable => owner) query = query.where(:created_at.gte => (Time.now - WhoisXMLAPI.cache_length)) if WhoisXMLAPI.cache res = query.first if res.nil? WhoisXMLAPI.logger.debug "WhoisXMLAPI#rwhois - no cached RWhois data for \'#{entity_name}\': initiating API access." res = rwhois_cacheless(entity_name, since_dt, owner) else WhoisXMLAPI.logger.debug "WhoisXMLAPI#rwhois - using cached RWhois data for \'#{entity_name}\'" end res end
rwhois_cacheless(entity_name, since_dt=nil, owner=nil)
click to toggle source
reverse-whois-api.whoisxmlapi.com/docs POST reverse-whois-api.whoisxmlapi.com/api/v2
# File lib/whoisxmlapi/client.rb, line 164 def rwhois_cacheless(entity_name, since_dt=nil, owner=nil) # entity_name is really the domain being passed in for Rwhois not the actual entity name return nil unless entity_name.present? res = WhoisXMLAPI::RWhoisResult.new(:entity_name => entity_name, :rwhoisable => owner, :domains => []) r = WhoisXMLAPI::Client.send_rwhois_request(entity_name, since_dt) if r && r.parsed_response && r.parsed_response['domainsList'] res.domains = r.parsed_response['domainsList'] elsif PublicSuffix.valid?(entity_name) # if no luck with what was passed in and we have a valid domain with TLD, try just the second-level domain. domain_sld = PublicSuffix.parse(entity_name).sld WhoisXMLAPI.logger.debug "WhoisXMLAPI#rwhois_cacheless - no domains found for domain \'#{entity_name}\', trying #{domain_sld}" res.entity_name = domain_sld r = WhoisXMLAPI::Client.send_rwhois_request(domain_sld) if r && r.parsed_response && r.parsed_response['domainsList'] res.domains = r.parsed_response['domainsList'] end else WhoisXMLAPI.logger.debug "WhoisXMLAPI#rwhois_cacheless - no domains found for \'#{entity_name}\'!" end unless res.save WhoisXMLAPI.logger.debug "WhoisXMLAPI#rwhois_cacheless - ERROR saving RWhoisResult: #{res.errors.full_messages.join('; ')} - #{res}!" end res end
whois(domain, owner=nil)
click to toggle source
whoisapi.whoisxmlapi.com/docs GET www.whoisxmlapi.com/whoisserver/WhoisService?apiKey=YOUR_API_KEY&domainName=google.com
# File lib/whoisxmlapi/client.rb, line 76 def whois(domain, owner=nil) unless PublicSuffix.valid?(domain) res = WhoisXMLAPI::BadDomain.new res.parse(domain) return res end query = WhoisXMLAPI::Result.where(:domain => domain) query = query.where(:created_at.gte => (Time.now - WhoisXMLAPI.cache_length)) if WhoisXMLAPI.cache res = query.first if res.nil? || res._type == "WhoisXMLAPI::Unavailable" WhoisXMLAPI.logger.debug "WhoisXMLAPI#whois - no cached Whois data for \'#{domain}\': initiating API access." res = whois_cacheless(domain, owner) else WhoisXMLAPI.logger.debug "WhoisXMLAPI#whois - using cached Whois data for \'#{domain}\'" end res end
whois_cacheless(domain, owner=nil)
click to toggle source
whoisapi.whoisxmlapi.com/docs GET www.whoisxmlapi.com/whoisserver/WhoisService?apiKey=YOUR_API_KEY&domainName=google.com
# File lib/whoisxmlapi/client.rb, line 16 def whois_cacheless(domain, owner=nil) unless PublicSuffix.valid?(domain) res = WhoisXMLAPI::BadDomain.new(:whoisable => owner) res.parse(domain) return res end params = { :apiKey => WhoisXMLAPI.api_key, :domainName => domain, :outputFormat => 'JSON' } begin r = HTTParty.get(WhoisXMLAPI.whois_path, :query => params, :timeout => 120) rescue StandardError => e WhoisXMLAPI.logger.info "WhoisXMLAPI#whois - Error getting Whois info for \'#{domain}\': #{e}" res = WhoisXMLAPI::Unavailable.new(:whoisable => owner) res.parse(domain) # parse will save document for Unavailable else if WhoisXMLAPI.callbacks[:whois] WhoisXMLAPI.callbacks[:whois].each{ |cb| cb.call } end # WhoisXML and HTTParty are sometimes returning a Hash and not a JSON string as requested, # which is causing an error of "no implicit conversion of Hash into String" when we call JSON.parse. # Logger message is to monitor whether WhoisXML and/or HTTParty will still return a string on occasion. if r if r.parsed_response.is_a?(String) WhoisXMLAPI.logger.debug "WhoisXMLAPI#whois - WARNING: passed back parsed_response as a String instead of a Hash for \'#{domain}\'." presponse = JSON.parse(r.parsed_response) rescue nil else presponse = r.parsed_response end if presponse.nil? res = {:error => {message: 'Failed to parse response from API.'}} elsif presponse.key? 'ErrorMessage' WhoisXMLAPI.logger.info "WhoisXMLAPI#whois - Error getting Whois info: #{presponse['ErrorMessage']['msg']}" res = WhoisXMLAPI::Unavailable.new(:whoisable => owner) res.parse(domain) # parse will save document for Unavailable elsif (200..299).include? r.code.to_i res = WhoisXMLAPI::Good.new(:whoisable => owner) res.parse(presponse['WhoisRecord']) # parse will NOT save document for Good or UnParsable res.save end end end unless res WhoisXMLAPI.logger.info "WhoisXMLAPI#whois - Unable to get Whois info for domain: \'#{domain}\'" res = WhoisXMLAPI::Unavailable.new(:whoisable => owner) res.parse(domain) # parse will save document for Unavailable end return res end