lib/reversed.rb in reversed-0.2.0 vs lib/reversed.rb in reversed-0.2.1
- old
+ new
@@ -1,13 +1,24 @@
require "reversed/version"
require "net/dns"
module Reversed
- def self.lookup(ip)
+ def self.lookup(ip, timeout: 5, nameservers: nil)
ip = ip.to_s
unless ip.empty?
- resolver = Net::DNS::Resolver.new(retry: 3).search(ip)
- answer = resolver.answer.first || resolver.authority.first
- answer.value.split(" ").first[0..-2] if answer
+ options = {
+ retry: 3,
+ udp_timeout: timeout
+ }
+ options[:nameservers] = nameservers if nameservers
+ begin
+ resolver = Net::DNS::Resolver.new(options).search(ip)
+ answer = resolver.answer.first || resolver.authority.first
+ if answer && !answer.value.empty?
+ answer.value.split(" ").first[0..-2]
+ end
+ rescue Net::DNS::Resolver::NoResponseError
+ nil
+ end
end
end
end