Class: WhoisXMLAPI::Client

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

Instance Method Summary collapse

Instance Method Details

#account_balanceObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/whoisxmlapi/client.rb', line 142

def 
  aquery = {
      :servicetype => 'accountbalance',
      :output_format => 'JSON',
      :username => WhoisXMLAPI.username,
      :password => WhoisXMLAPI.password
  }
  r = HTTParty.get("#{WhoisXMLAPI.domain}/accountServices.php", :query => aquery, :timeout => 120)
  JSON.parse(r.parsed_response)
end

#exists?(domain) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/whoisxmlapi/client.rb', line 128

def exists?(domain)
  xquery = {
      :cmd => 'GET_DN_AVAILABILITY',
      :domainName => domain,
      :outputFormat => 'json',
      :userName => WhoisXMLAPI.username,
      :password => WhoisXMLAPI.password
  }
  r = HTTParty.get("#{WhoisXMLAPI.domain}/whoisserver/WhoisService", :query => xquery, :timeout => 120)

  resp = JSON.parse(r.parsed_response)
  resp["DomainInfo"]["domainAvailability"] == "UNAVAILABLE"
end

#rwhois(entity_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
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
# File 'lib/whoisxmlapi/client.rb', line 65

def rwhois(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
    Rails.logger.debug "WhoisXMLAPI rwhois - have cached rwhois data for #{entity_name}"
  else
    Rails.logger.debug "WhoisXMLAPI rwhois - no cached rwhois data for #{entity_name}"
    res = WhoisXMLAPI::RWhoisResult.create(:entity_name => entity_name)
    rquery = {
        :term1 => entity_name,
        :search_type => 'current',
        :mode => WhoisXMLAPI.rwhois_mode,
        :username => WhoisXMLAPI.username,
        :password => WhoisXMLAPI.password
    }
    r = HTTParty.get("#{WhoisXMLAPI.domain}/reverse-whois-api/search.php", :query => rquery, :timeout => 120)
    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)
      rquery = {
          :term1 => domain_sld,
          :search_type => 'current',
          :mode => WhoisXMLAPI.rwhois_mode,
          :username => WhoisXMLAPI.username,
          :password => WhoisXMLAPI.password
      }
      r = HTTParty.get("#{WhoisXMLAPI.domain}/reverse-whois-api/search.php", :query => rquery, :timeout => 120)
      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
      Rails.logger.debug "WhoisXMLAPI rwhois - no domains found for #{entity_name}!"
      res.domains = []
      res.save
    end
  end
  res
end

#whois(domain) ⇒ Object



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

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 = {
        :domainName => domain,
        :outputFormat => 'json', # TODO: make into a config setting. see the JSON.parse line below for the rest of this change
        :userName => WhoisXMLAPI.username,
        :password => WhoisXMLAPI.password
    }
    begin
      r = HTTParty.get("#{WhoisXMLAPI.domain}/whoisserver/WhoisService", :query => xquery, :timeout => 120)
    rescue Exception => e
      WhoisXMLAPI.logger.info("Error getting Whois info: #{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

    # TODO: with the above outputFormat change, use a Parser plugin instead of this
    presponse = JSON.parse(r.parsed_response)

    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