From 2859fbbdc7c1dc75b016b583252f5551029da9f9 Mon Sep 17 00:00:00 2001 From: Viktors Rotanovs Date: Tue, 20 Dec 2016 09:58:56 +0100 Subject: [PATCH 1/3] load! now accepts a path argument --- lib/ipcat.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ipcat.rb b/lib/ipcat.rb index 05ec3b3..3118d19 100644 --- a/lib/ipcat.rb +++ b/lib/ipcat.rb @@ -37,11 +37,11 @@ def load_csv!(path='https://raw.github.com/client9/ipcat/master/datacenters.csv' self.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 From 496e82de3331bc620f7a2026912bdd386a66afdb Mon Sep 17 00:00:00 2001 From: Viktors Rotanovs Date: Tue, 20 Dec 2016 10:15:14 +0100 Subject: [PATCH 2/3] Lazyload datacenter database --- lib/ipcat.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/ipcat.rb b/lib/ipcat.rb index 3118d19..161fa2f 100644 --- a/lib/ipcat.rb +++ b/lib/ipcat.rb @@ -10,6 +10,7 @@ class IPCat class << self def datacenter?(ip) + load! unless @loaded bsearch(ip_to_integer(ip)) end alias_method :classify, :datacenter? @@ -35,6 +36,8 @@ def load_csv!(path='https://raw.github.com/client9/ipcat/master/datacenters.csv' self.ranges << IPRange.new(first, last, name, url).freeze end self.ranges.freeze + + @loaded = true end def load!(path=nil) @@ -45,6 +48,8 @@ def load!(path=nil) @ranges = Marshal.load(File.read(path)) @ranges.each(&:freeze) @ranges.freeze + + @loaded = true rescue load_csv! end @@ -65,6 +70,3 @@ def bsearch(needle, haystack=ranges, first=0, last=ranges.size-1) end end end - -# Load dataset -IPCat.load! From a2c90457eb0ac9b91ea1c88a2c59f987f2130913 Mon Sep 17 00:00:00 2001 From: Viktors Rotanovs Date: Wed, 21 Dec 2016 09:47:18 +0100 Subject: [PATCH 3/3] Fix the warning, make tests pass --- lib/ipcat.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/ipcat.rb b/lib/ipcat.rb index 161fa2f..569cddd 100644 --- a/lib/ipcat.rb +++ b/lib/ipcat.rb @@ -10,7 +10,7 @@ class IPCat class << self def datacenter?(ip) - load! unless @loaded + load! if ranges.empty? bsearch(ip_to_integer(ip)) end alias_method :classify, :datacenter? @@ -36,8 +36,6 @@ def load_csv!(path='https://raw.github.com/client9/ipcat/master/datacenters.csv' self.ranges << IPRange.new(first, last, name, url).freeze end self.ranges.freeze - - @loaded = true end def load!(path=nil) @@ -48,8 +46,6 @@ def load!(path=nil) @ranges = Marshal.load(File.read(path)) @ranges.each(&:freeze) @ranges.freeze - - @loaded = true rescue load_csv! end