Module: DbipUtil

Extended by:
DbipUtil
Included in:
DbipUtil
Defined in:
lib/dbip_util.rb,
lib/dbip_util/version.rb,
sig/dbip_util.rbs,
sig/dbip_util/version.rbs

Overview

DbipUtil includes DB-IP Lite databases and a simple interface to them, based on maxmind-db gem.

Constant Summary collapse

DB_VERSION =

Returns:

  • ("2025.07")
"2026.08"
VERSION =

Returns:

  • (::String)
"0.1.2.#{DB_VERSION}".freeze
AST =

Returns:

  • (MaxMind::DB)
City =

Returns:

  • (MaxMind::DB)
Country =

Returns:

  • (MaxMind::DB)

Instance Method Summary collapse

Instance Method Details

#const_missing(const) ⇒ MaxMind::DB

Parameters:

  • const (Symbol)

Returns:

  • (MaxMind::DB)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dbip_util.rb', line 18

def const_missing(const)
  case const
  when :Country
    const_set(const, open(:country))
  when :City
    const_set(const, open(:city))
  when :ASN
    const_set(const, open(:asn))
  else
    super
  end
end

#db_path(db) ⇒ String

Parameters:

  • db (Symbol)

Returns:

  • (String)


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

def db_path(db)
  __dir__ + "/dbip_util/db/dbip-#{db}-lite.mmdb"
end

#ensure_provisioned!Object

Returns:

  • (Object)


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

def ensure_provisioned!
  # Check for a sample database. If not present, provision it from Rubygems.
  system "#{__dir__}/../bin/dbiputil-provision" unless File.exist?(db_path(:country))
rescue StandardError
  warn "Couldn't provision a database! Use a `dbip_util` gem from Rubygems, as it includes a database."
  raise
end

#open(db, options = {}) ⇒ MaxMind::DB

Parameters:

  • db (Symbol)
  • options (::Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (MaxMind::DB)


10
11
12
13
14
15
16
# File 'lib/dbip_util.rb', line 10

def open(db, options = {})
  ensure_provisioned!

  options = { mode: MaxMind::DB::MODE_FILE }.merge(options)
  fn = db_path(db)
  MaxMind::DB.new(fn, options)
end