Class: IPGeo::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(hostname, options = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/ip_geo/client.rb', line 9

def initialize(hostname, options = {})
  @hostname = hostname
  @options = options
end

Instance Method Details

#lookup(ip) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ip_geo/client.rb', line 14

def lookup(ip)
  Timeout.timeout(@options[:timeout] || 2) do
    Net::HTTP.start(@hostname, @options[:port] || (@options[:ssl] == false ? 80 : 443), :use_ssl => @options[:ssl] != false) do |http|
      request = Net::HTTP::Get.new("/#{ip}")
      response = http.request(request)
      if response.is_a?(Net::HTTPOK)
        Result.new(JSON.parse(response.body))
      else
        false
      end
    end
  end
rescue Exception, Timeout::Error => e
  STDERR.puts "Error during IP lookup of #{ip} with #{@hostname}: #{e.class} (#{e.message})"
  false
end