Class: IpLookupService
- Inherits:
-
Object
- Object
- IpLookupService
- Defined in:
- lib/ip-world-map/ip_lookup_service.rb
Instance Method Summary collapse
- #coordinates_for_host(host) ⇒ Object
- #coordinates_for_hosts(hosts) ⇒ Object
- #extract_longitude_and_latitude(string) ⇒ Object
-
#initialize(filename = nil) ⇒ IpLookupService
constructor
A new instance of IpLookupService.
- #load_coordinates ⇒ Object
- #reset ⇒ Object
- #save_coordinates ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(filename = nil) ⇒ IpLookupService
Returns a new instance of IpLookupService.
7 8 9 10 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 7 def initialize filename = nil @filename = filename || File.join(File.dirname(__FILE__), '..', '..', 'resources', 'coordinates.yml') reset end |
Instance Method Details
#coordinates_for_host(host) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 39 def coordinates_for_host host unless @host_coordinates[host] @host_ips[host] ||= IPSocket.getaddress(host) rescue nil response = Net::HTTP.get('api.hostip.info', "/get_html.php?position=true&ip=#{@host_ips[host]}") @host_coordinates[host] = extract_longitude_and_latitude(response) end @host_coordinates[host] end |
#coordinates_for_hosts(hosts) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 17 def coordinates_for_hosts hosts uniq_hosts = hosts.uniq uniq_hosts.each do |host| @host_ips[host] ||= IPSocket.getaddress(host) rescue nil end hydra = Typhoeus::Hydra.new uniq_hosts.each do |host| unless @host_coordinates[host] request = Typhoeus::Request.new("http://api.hostip.info/get_html.php?position=true&ip=#{@host_ips[host]}") request.on_complete do |response| @host_coordinates[host] = extract_longitude_and_latitude(response.body) end hydra.queue(request) end end hydra.run @host_coordinates end |
#extract_longitude_and_latitude(string) ⇒ Object
49 50 51 52 53 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 49 def extract_longitude_and_latitude string latitude = string.match(/Latitude: (-?[0-9.]+)/)[1].to_f rescue nil longitude = string.match(/Longitude: (-?[0-9.]+)/)[1].to_f rescue nil [longitude, latitude] end |
#load_coordinates ⇒ Object
59 60 61 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 59 def load_coordinates @host_coordinates = YAML.load_file(@filename) if File.readable? @filename end |
#reset ⇒ Object
12 13 14 15 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 12 def reset @host_coordinates = {} @host_ips = {} end |
#save_coordinates ⇒ Object
55 56 57 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 55 def save_coordinates File.open(@filename, 'w'){ |f| f.write @host_coordinates.to_yaml } end |
#stats ⇒ Object
63 64 65 66 67 |
# File 'lib/ip-world-map/ip_lookup_service.rb', line 63 def stats unknown_coordinates, known_coordinates = @host_coordinates.partition{ |ip, coords| coords.include? nil } { :unknown_coordinates => unknown_coordinates.size, :known_coordinates => known_coordinates.size } end |