Sha256: 705973783169839d3ebe88bb7c069663bfbe5cf669b942b6373f7e18253f71be

Contents?: true

Size: 865 Bytes

Versions: 5

Compression:

Stored size: 865 Bytes

Contents

module WorldFlags
  class GeoIPError < StandardError; end

  module Helper
  	module Geo
  		def self.country_code_from_ip ip = nil
        ip ||= request.remote_ip
        raise WorldFlags::GeoIPError, "IP address #{ip} is a localhost address" if local_ip?(ip)
        
        puts "find country code for ip: #{ip}" if debug?

        @geoip ||= ::GeoIP.new WorldFlags.geo_ip_db_path
        country = @geoip.country(ip)
        return country[2] unless country.nil?
      rescue Exception => e
        raise WorldFlags::GeoIPError, "No country code could be found for IP: #{ip} - #{e}"
      end

      def self.local_ip? ip
        WorldFlags.localhost_list.include?(ip)
      end

      def self.debug?
        WorldFlags.debug?
      end

  		def country_code_from_ip ip = nil
      	WorldFlags::Helper::Geo.country_code_from_ip ip
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
world-flags-0.6.0 lib/world_flags/helper/geo.rb
world-flags-0.5.1 lib/world_flags/helper/geo.rb
world-flags-0.5.0 lib/world_flags/helper/geo.rb
world-flags-0.4.9 lib/world_flags/helper/geo.rb
world-flags-0.4.8.3 lib/world_flags/helper/geo.rb