Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/ipcat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class IPCat
class << self
def datacenter?(ip)
load! if ranges.empty?
bsearch(ip_to_integer(ip))
end
alias classify datacenter?
Expand Down Expand Up @@ -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
Expand All @@ -66,6 +67,3 @@ def bsearch(needle, haystack = ranges, first = 0, last = ranges.size - 1)
end
end
end

# Load dataset
IPCat.load!