class DatacenterLookup::Parser

Public Class Methods

new() click to toggle source
# File lib/datacenter_lookup/parser.rb, line 6
def initialize
  @starts = Array.new
  @ends = Array.new
  @urls = Array.new
  File.open(DatacenterLookup::DefaultDatacentersPath, "r").readlines.map do |line|
    add(*CSV.parse_line(line))
  end
end

Public Instance Methods

add(start, stop, name, url=nil) click to toggle source
# File lib/datacenter_lookup/parser.rb, line 15
def add(start, stop, name, url=nil)
  @starts << IPAddr.new(start).to_i
  @ends << IPAddr.new(stop).to_i
  @urls << url
end
find(ipstring) click to toggle source
# File lib/datacenter_lookup/parser.rb, line 25
def find(ipstring)
  ip = IPAddr.new(ipstring).to_i
  high = length
  low = 0
  while high >= low do
    probe = ((high+low)/2).floor.to_i
    if @starts[probe] > ip
      high = probe - 1
    elsif @ends[probe] < ip
      low = probe + 1
    else
      return @urls[probe]
    end
  end
  return nil
end
length() click to toggle source
# File lib/datacenter_lookup/parser.rb, line 21
def length
  @starts.length
end