Module: IpGeoLookup

Defined in:
lib/ip_geo_lookup.rb,
lib/ip_geo_lookup/mmdb.rb,
lib/ip_geo_lookup/result.rb,
lib/ip_geo_lookup/version.rb,
lib/ip_geo_lookup/ip_address.rb

Defined Under Namespace

Modules: IpAddress Classes: ClosedError, Configuration, DatabaseFormatError, DatabaseNotFoundError, MMDB, Result

Constant Summary collapse

DEFAULT_DB_PATH =
File.expand_path("../../data/GeoLite2-City.mmdb", __FILE__).freeze
VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.closeObject



37
38
39
40
41
42
# File 'lib/ip_geo_lookup.rb', line 37

def close
  @mutex.synchronize do
    @reader&.close
    @reader = nil
  end
end

.configurationObject



51
52
53
# File 'lib/ip_geo_lookup.rb', line 51

def configuration
  @configuration ||= Configuration.new
end

.configureObject



44
45
46
47
48
49
# File 'lib/ip_geo_lookup.rb', line 44

def configure
  @mutex.synchronize do
    yield configuration
    @reader = MMDB.new(configuration.database_path, mode: configuration.mode)
  end
end

.lookup(ip_address) ⇒ Object

Returns a Result for the given IP string, or nil.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ip_geo_lookup.rb', line 15

def lookup(ip_address)
  return nil unless ip_address.is_a?(String)

  parsed = IpAddress.parse(ip_address)
  return nil unless parsed

  ip_int, version = parsed
  record = reader.find(ip_int, (version == 6) ? 128 : 32)
  return nil unless record

  build_result(record)
end

.metadataObject



28
29
30
# File 'lib/ip_geo_lookup.rb', line 28

def 
  reader.
end

.reload!Object

Reloads from disk; respects configured path.



33
34
35
# File 'lib/ip_geo_lookup.rb', line 33

def reload!
  @mutex.synchronize { @reader = nil }
end

.reset!Object



55
56
57
58
59
60
# File 'lib/ip_geo_lookup.rb', line 55

def reset!
  @mutex.synchronize do
    @reader = nil
    @configuration = nil
  end
end