Class: Whoxy::Client

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

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.

Raises:



20
21
22
# File 'lib/whoxy.rb', line 20

def initialize
  raise MissingAPIKeyError unless configuration && configuration.key
end

Instance Method Details

#lookup(uri, &block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/whoxy.rb', line 24

def lookup(uri, &block)
  domain = parse_domain(uri)
  response = connection.get(whois_endpoint, whois: domain, key: configuration.key) do |request|
    yield request if block_given?
  end

  response.body
end

#parse_domain(uri) ⇒ Object



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
# File 'lib/whoxy.rb', line 33

def parse_domain(uri)
  parsed_uri = URI.parse(uri)
  parsed_uri = URI.parse("http://#{uri}") if parsed_uri.scheme.nil?

  host = parsed_uri.host

  # Handle mailto scheme
  if parsed_uri.scheme == "mailto"
    _, address_host = uri.split("@")
    host = address_host
  end

  fail ArgumentError.new("Could not parse #{uri} for a domain") if host.nil?

  # Check for subdomains or combined TLD with ccTLD
  num_separators = host.count "."
  domain = if num_separators > 1
    # if this is a combined TLD, one or both of the last two have to be an
    # international ccTLD (like uk or gm) which are only 2 characters long.
    possible_tlds = host.split(".").last(2)
    cctld_detected = possible_tlds.map { |tld| tld.size == 2 }.reduce(:|)
    num_to_select = if cctld_detected
                      3
                    else # The were no ccTLDs
                      2
                    end
    host.split(".").last(num_to_select).join(".")
  else # there's no subdomain or combined TLD
    host
  end

  domain
end