Class: IPCat

Inherits:
Object
  • Object
show all
Defined in:
lib/ipcat.rb,
lib/ipcat/iprange.rb,
lib/ipcat/version.rb

Defined Under Namespace

Classes: IPRange

Constant Summary collapse

VERSION =
'2.0.0'

Class Method Summary collapse

Class Method Details

.datacenter?(ip) ⇒ Boolean Also known as: classify

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/ipcat.rb', line 12

def datacenter?(ip)
  ip_as_int = ip_to_integer(ip)
  ranges.bsearch {|range| range <=> ip_as_int }
end

.ip_to_integer(ip) ⇒ Object



18
19
20
# File 'lib/ipcat.rb', line 18

def ip_to_integer(ip)
  Integer === ip ? ip : IPAddr.new(ip).to_i
end

.load!Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ipcat.rb', line 41

def load!
  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')
  @ranges = Marshal.load(File.read(path))
  @ranges.each(&:freeze)
  @ranges.freeze
rescue
  load_csv!
end

.load_csv!(path = 'https://raw.github.com/client9/ipcat/master/datacenters.csv') ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ipcat.rb', line 30

def load_csv!(path='https://raw.github.com/client9/ipcat/master/datacenters.csv')
  reset_ranges!

  require 'open-uri'
  open(path).readlines[1..-1].each do |line|
    first, last, name, url = line.split(',')
    self.ranges << IPRange.new(first, last, name, url).freeze
  end
  self.ranges.freeze
end

.rangesObject



22
23
24
# File 'lib/ipcat.rb', line 22

def ranges
  @ranges ||= []
end

.reset_ranges!(new_ranges = []) ⇒ Object



26
27
28
# File 'lib/ipcat.rb', line 26

def reset_ranges!(new_ranges = [])
  @ranges = new_ranges
end