diff --git a/lib/ipcat.rb b/lib/ipcat.rb index c201d43..2c57f6c 100644 --- a/lib/ipcat.rb +++ b/lib/ipcat.rb @@ -11,6 +11,7 @@ class IPCat class << self def datacenter?(ip) + load! if ranges.empty? bsearch(ip_to_integer(ip)) end alias classify datacenter? @@ -38,11 +39,11 @@ def load_csv!(path = 'https://raw.github.com/client9/ipcat/master/datacenters.cs ranges.freeze end - def load! + def load!(path=nil) reset_ranges! # NB: loading an array of marshaled ruby objects takes ~15ms; # versus ~100ms to load a CSV file - path = File.join(File.dirname(__FILE__), '..', 'data', 'datacenters') + path ||= File.join(File.dirname(__FILE__), '..', 'data', 'datacenters') @ranges = Marshal.load(File.read(path)) @ranges.each(&:freeze) @ranges.freeze @@ -66,6 +67,3 @@ def bsearch(needle, haystack = ranges, first = 0, last = ranges.size - 1) end end end - -# Load dataset -IPCat.load!