Class: DatacenterLookup::Parser
- Inherits:
-
Object
- Object
- DatacenterLookup::Parser
- Defined in:
- lib/datacenter_lookup/parser.rb
Instance Method Summary collapse
- #add(start, stop, name, url = nil) ⇒ Object
- #find(ipstring) ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #length ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
6 7 8 9 10 11 12 13 |
# 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 |
Instance Method Details
#add(start, stop, name, url = nil) ⇒ Object
15 16 17 18 19 |
# 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) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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 ⇒ Object
21 22 23 |
# File 'lib/datacenter_lookup/parser.rb', line 21 def length @starts.length end |