Class: WhoisXMLAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/whoisxmlapi/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.send_rwhois_request(entity_name, since_dt = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/whoisxmlapi/client.rb', line 129

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

Instance Method Details

#account_balanceObject

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"         => => 8,  :credits => 1000,
"Domain Research Suite"      => => 14, :credits => 100,
"WHOIS API"                  => => 1,  :credits => 1000,
"Domain Reputation API"      => => 20, :credits => 100,
"IP Netblocks API"           => => 23, :credits => 1000,
"Domain Availability API"    => => 25, :credits => 100,
"Screenshot API"             => => 27, :credits => 500,
"Website Categorization API" => => 21, :credits => 100,
"Website Contacts API"       => => 29, :credits => 100,
"DNS Lookup API"             => => 26, :credits => 500

}



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/whoisxmlapi/client.rb', line 229

def 
  result = nil
  params = {
    :apiKey => WhoisXMLAPI.api_key
  }

  begin
    r = HTTParty.get(WhoisXMLAPI., :query => params, :timeout => 120)
  rescue StandardError => e
    WhoisXMLAPI.logger.info "WhoisXMLAPI#account_balance - Error getting account_balance: #{e}"
    r = nil
  end

  if r.response.is_a? Net::HTTPOK
    if r && 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."
        JSON.parse(r.parsed_response)
      end

      if r.parsed_response["error"]
        result = {:error => {message: r.parsed_response["error"]}}
      elsif r.parsed_response["data"]
        result = {}
        r.parsed_response["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) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/whoisxmlapi/client.rb', line 94

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}\'."
    resp = JSON.parse(r.parsed_response)
  else
    resp = r.parsed_response
  end

  resp["DomainInfo"]["domainAvailability"] == "UNAVAILABLE"
end

#rwhois(entity_name, since_dt = nil, owner = nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/whoisxmlapi/client.rb', line 189

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) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/whoisxmlapi/client.rb', line 161

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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/whoisxmlapi/client.rb', line 73

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) ⇒ Object



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
# 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)
      else
        presponse = r.parsed_response
      end
      if 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