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



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

def self.send_rwhois_request(entity_name)
  params = {
    :apiKey           => WhoisXMLAPI.api_key,
    :searchType       => "current",
    :mode             => WhoisXMLAPI.rwhois_mode,
    :responseFormat   => 'json',
    :basicSearchTerms => {
      :include => [entity_name]  # must be an array of strings!
    },
  }
  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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/whoisxmlapi/client.rb', line 181

def 
  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

  # 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

  r ? r.parsed_response : nil
end

#exists?(domain) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/whoisxmlapi/client.rb', line 149

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



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

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)
  query = query.where(:created_at.gte => (Time.now - WhoisXMLAPI.cache_length)) if WhoisXMLAPI.cache
  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)

    r = WhoisXMLAPI::Client.send_rwhois_request(entity_name)
    if r && r.parsed_response && r.parsed_response['domainsList']
      res.domains = r.parsed_response['domainsList']
      res.save
    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 = PublicSuffix.parse(entity_name)
      domain_sld = domain.sld
      WhoisXMLAPI.logger.debug "WhoisXMLAPI#rwhois - no domains found for domain #{entity_name}, trying #{domain_sld}"
      res = WhoisXMLAPI::RWhoisResult.create(: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']
        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



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

def whois(domain)
  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"
    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
      res.parse(domain)
      return res
    end

    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
      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
        res.parse(domain)
      elsif (200..299).include? r.code.to_i
        res = WhoisXMLAPI::Good.new
        res.parse(presponse['WhoisRecord'])
        res.save
      end
    end

    unless res
      WhoisXMLAPI.logger.info "WhoisXMLAPI#whois - Unable to get Whois info for : #{domain}"
      res = WhoisXMLAPI::Unavailable.new
      res.parse(domain)
    end

  end
  return res
end