Class: CountryLookup::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/country_lookup.rb

Instance Method Summary collapse

Constructor Details

#initializeLookup

Returns a new instance of Lookup.



4
5
6
# File 'lib/country_lookup.rb', line 4

def initialize
  initialize_from_file
end

Instance Method Details

#lookup_ip_number(ip_number) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/country_lookup.rb', line 25

def lookup_ip_number(ip_number)
  index = self.binary_search(ip_number)
  cc = @country_codes[index]
  if cc == '--'
    return nil
  end
  cc
end

#lookup_ip_string(ip_string) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/country_lookup.rb', line 8

def lookup_ip_string(ip_string)
  if ip_string.nil? || ip_string.empty?
    return nil
  end
  components = ip_string.split('.')
  if components.length < 4
    return nil
  end

  ip_number = components[0].to_i * 16777216 +
    components[1].to_i * 65536 +
    components[2].to_i * 256 +
    components[3].to_i

  self.lookup_ip_number(ip_number)
end