Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading datacenters from a custom location #14

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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!