lib/world_flags/helper/geo.rb in world-flags-0.4.5 vs lib/world_flags/helper/geo.rb in world-flags-0.4.7
- old
+ new
@@ -1,19 +1,25 @@
module WorldFlags
+ class GeoIPError < StandardError; end
+
module Helper
module Geo
- def self.ip_json
- require "httparty"
-
- HTTParty.get('http://freegeoip.net/json/')
+ def self.ip_country_code ip = nil
+ ip ||= request.remote_ip
+ raise WorldFlags::GeoIPError, "IP address #{ip} is a localhost address" if local_ip?(ip)
+
+ @geoip ||= GeoIP.new WorldFlags.geo_ip_db_path
+ country = @geoip.country(ip)
+ return country[2] unless country.nil?
+ raise WorldFlags::GeoIPError, "No country code could be found for IP: #{ip}"
end
- def self.ip_country_code
- @ip_country_code ||= ip_json.parsed_response['country_code']
+ def self.local_ip? ip
+ WorldFlags.localhost_list.include?(ip)
end
- def ip_country_code
- WorldFlags::Helper::Geo.ip_country_code
+ def ip_country_code ip = nil
+ WorldFlags::Helper::Geo.ip_country_code ip
end
end
end
end
\ No newline at end of file