Class: GeoipWhoisAsnCountry::GeoIpCountry
- Inherits:
-
Object
- Object
- GeoipWhoisAsnCountry::GeoIpCountry
- Includes:
- Singleton
- Defined in:
- lib/geoip_whois_asn_country.rb
Class Attribute Summary collapse
-
.cache_path ⇒ Object
Returns the value of attribute cache_path.
-
.cache_time ⇒ Object
Returns the value of attribute cache_time.
-
.ipv4_num_csv_path ⇒ Object
Returns the value of attribute ipv4_num_csv_path.
-
.ipv6_num_csv_path ⇒ Object
Returns the value of attribute ipv6_num_csv_path.
Class Method Summary collapse
Instance Method Summary collapse
Class Attribute Details
.cache_path ⇒ Object
Returns the value of attribute cache_path.
21 22 23 |
# File 'lib/geoip_whois_asn_country.rb', line 21 def cache_path @cache_path end |
.cache_time ⇒ Object
Returns the value of attribute cache_time.
21 22 23 |
# File 'lib/geoip_whois_asn_country.rb', line 21 def cache_time @cache_time end |
.ipv4_num_csv_path ⇒ Object
Returns the value of attribute ipv4_num_csv_path.
21 22 23 |
# File 'lib/geoip_whois_asn_country.rb', line 21 def ipv4_num_csv_path @ipv4_num_csv_path end |
.ipv6_num_csv_path ⇒ Object
Returns the value of attribute ipv6_num_csv_path.
21 22 23 |
# File 'lib/geoip_whois_asn_country.rb', line 21 def ipv6_num_csv_path @ipv6_num_csv_path end |
Class Method Details
._load(map_str) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/geoip_whois_asn_country.rb', line 50 def self._load(map_str) map = Marshal.load(map_str) instance.instance_variable_set(:@ipv4_map, map[:ipv4_map]) instance.instance_variable_set(:@ipv4_sorted_keys, map[:ipv4_sorted_keys]) instance.instance_variable_set(:@ipv6_map, map[:ipv6_map]) instance.instance_variable_set(:@ipv6_sorted_keys, map[:ipv6_sorted_keys]) instance end |
._lookup(ip) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/geoip_whois_asn_country.rb', line 28 def self._lookup(ip) if !@singleton__instance__ || @singleton__instance__.instance_variables.length == 0 if File.exist?(cache_path) && File.mtime(cache_path) > Time.now - cache_time @singleton__instance__ = load_from_cache else # puts "GeoIP-Whois-ASN-Country: first load + cache" instance.load_csvs cache! end end instance._lookup(ip) end |
.cache! ⇒ Object
59 60 61 |
# File 'lib/geoip_whois_asn_country.rb', line 59 def self.cache! File.open(cache_path, "wb+") { |f| f.write Marshal.dump(instance) } end |
.load_from_cache ⇒ Object
63 64 65 |
# File 'lib/geoip_whois_asn_country.rb', line 63 def self.load_from_cache Marshal.load(File.read(cache_path)) end |
Instance Method Details
#_dump(depth) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/geoip_whois_asn_country.rb', line 41 def _dump(depth) Marshal.dump({ ipv4_map: @ipv4_map, ipv4_sorted_keys: @ipv4_sorted_keys, ipv6_map: @ipv6_map, ipv6_sorted_keys: @ipv6_sorted_keys, }, depth) end |
#_lookup(ip) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/geoip_whois_asn_country.rb', line 89 def _lookup(ip) ip = IPAddr.new(ip) if ip.is_a?(String) ip_i = ip.to_i if ip.ipv4? key = @ipv4_sorted_keys.bsearch_index { |x| x > ip_i } @ipv4_map[@ipv4_sorted_keys[key - 1]] else key = @ipv6_sorted_keys.bsearch_index { |x| x > ip_i } @ipv6_map[@ipv6_sorted_keys[key - 1]] end end |
#load_csvs ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/geoip_whois_asn_country.rb', line 67 def load_csvs require 'csv' @ipv4_map = {} File.open(self.class.ipv4_num_csv_path) do |file| CSV.foreach(file, headers: false) do |row| from, _, country = row @ipv4_map[from.to_i] = country.to_sym end end @ipv4_sorted_keys = @ipv4_map.keys.sort.freeze @ipv6_map = {} File.open(self.class.ipv6_num_csv_path) do |file| CSV.foreach(file, headers: false) do |row| from, _, country = row @ipv6_map[from.to_i] = country.to_sym end end @ipv6_sorted_keys = @ipv6_map.keys.sort.freeze nil end |