Class: WhoisXMLAPI::Client
- Inherits:
-
Object
- Object
- WhoisXMLAPI::Client
- Defined in:
- lib/whoisxmlapi/client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #account_balance ⇒ Object
- #exists?(domain) ⇒ Boolean
- #rwhois(entity_name) ⇒ Object
- #whois(domain) ⇒ Object
Class Method Details
.create_and_send_rwhois_request(params_basic) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/whoisxmlapi/client.rb', line 193 def self.create_and_send_rwhois_request(params_basic) uri = URI.parse('https://reverse-whois-api.whoisxmlapi.com/api/v2') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(uri.request_uri) request.add_field('Content-Type', 'application/json') request.add_field('Accept', 'application/json') request.body = params_basic.to_json r = http.request(request) # used for debugging # puts JSON.parse(r.body).to_yaml return r end |
Instance Method Details
#account_balance ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/whoisxmlapi/client.rb', line 173 def account_balance aquery = { :servicetype => 'accountbalance', :output_format => 'JSON', :apiKey => WhoisXMLAPI.api_key } r = HTTParty.get("#{WhoisXMLAPI.domain}/accountServices.php", :query => aquery, :timeout => 120) # 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) Rails.logger.debug "WhoisXML passed back parsed_response as a String instead of a Hash. Account_balance method" JSON.parse(r.parsed_response) else r.parsed_response end end |
#exists?(domain) ⇒ Boolean
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/whoisxmlapi/client.rb', line 150 def exists?(domain) xquery = { :cmd => 'GET_DN_AVAILABILITY', :domainName => domain, :outputFormat => 'JSON', :apiKey => WhoisXMLAPI.api_key } r = HTTParty.get("#{WhoisXMLAPI.domain}/whoisserver/WhoisService", :query => xquery, :timeout => 120) # 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) Rails.logger.debug "WhoisXML passed back parsed_response as a String instead of a Hash for #{domain}. Exists? method" resp = JSON.parse(r.parsed_response) else resp = r.parsed_response end resp["DomainInfo"]["domainAvailability"] == "UNAVAILABLE" end |
#rwhois(entity_name) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/whoisxmlapi/client.rb', line 75 def rwhois(entity_name) # 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) if WhoisXMLAPI.cache query = query.where(:created_at.gte => (Time.now - WhoisXMLAPI.cache_length)) end res = query.first if res WhoisXMLAPI.logger.debug "WhoisXMLAPI rwhois - using cached rwhois data for #{entity_name}" else WhoisXMLAPI.logger.debug "WhoisXMLAPI rwhois - no cached rwhois data for #{entity_name}" res = WhoisXMLAPI::RWhoisResult.create(:entity_name => entity_name) params_basic = { basicSearchTerms: { :include => [entity_name], # must be an array of strings! :search_type => "current" }, :mode => WhoisXMLAPI.rwhois_mode, :apiKey => WhoisXMLAPI.api_key, } r = WhoisXMLAPI::Client.create_and_send_rwhois_request(params_basic) if WhoisXMLAPI.callbacks[:rwhois] WhoisXMLAPI.callbacks[:rwhois].each do |cb| cb.call end end if r && r.parsed_response && r.parsed_response['domains'] res.domains = r.parsed_response['domains'] res.save elsif PublicSuffix.valid?(entity_name) domain = PublicSuffix.parse(entity_name) domain_sld = domain.sld Rails.logger.debug "WhoisXMLAPI rwhois - no domains found for domain #{entity_name}, trying #{domain_sld}" res = WhoisXMLAPI::RWhoisResult.create(:entity_name => domain_sld) params_basic = { basicSearchTerms: { :include => [domain_sld], #must be an array of strings! :search_type => "current" }, :mode => WhoisXMLAPI.rwhois_mode, :apiKey => WhoisXMLAPI.api_key, } r = WhoisXMLAPI::Client.create_and_send_rwhois_request(params_basic) if WhoisXMLAPI.callbacks[:rwhois] WhoisXMLAPI.callbacks[:rwhois].each do |cb| cb.call end end if r && r.parsed_response && r.parsed_response['domains'] res.domains = r.parsed_response['domains'] res.save else res.domains = [] res.save end else WhoisXMLAPI.logger.debug "WhoisXMLAPI rwhois - no domains found for #{entity_name}!" res.domains = [] res.save end end res end |
#whois(domain) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/whoisxmlapi/client.rb', line 10 def whois(domain) unless PublicSuffix.valid?(domain) res = WhoisXMLAPI::BadDomain.new res.parse(domain) return res end query = WhoisXMLAPI::Result.where(:domain => domain) if WhoisXMLAPI.cache query = query.where(:created_at.gte => (Time.now - WhoisXMLAPI.cache_length)) end res = query.first if res && res._type != "WhoisXMLAPI::Unavailable" return res else xquery = { :apiKey => WhoisXMLAPI.api_key, :domainName => domain, :outputFormat => 'JSON' } begin r = HTTParty.get("#{WhoisXMLAPI.domain}/whoisserver/WhoisService", :query => xquery, :timeout => 120) rescue Exception => e WhoisXMLAPI.logger.info("Error getting Whois info for #{domain}: #{e}") res = WhoisXMLAPI::Unavailable.new res.parse(domain) return res end res = WhoisXMLAPI::Good.new if WhoisXMLAPI.callbacks[:whois] WhoisXMLAPI.callbacks[:whois].each do |cb| cb.call end 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 "WhoisXML Whois method passed back parsed_response as a String instead of a Hash for #{domain}." presponse = JSON.parse(r.parsed_response) else presponse = r.parsed_response end if (200..299).include? r.code.to_i and not presponse.key? 'ErrorMessage' res.parse(presponse['WhoisRecord']) res.save return res else WhoisXMLAPI.logger.info("Error getting Whois info: #{e}") res = WhoisXMLAPI::Unavailable.new res.parse(domain) return res end end end |