Class: IPDB

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/geoipdb.rb

Defined Under Namespace

Classes: IPDBError

Constant Summary collapse

DATA_FILES =
[
  'cities.csv',
  'ip_ranges.csv',
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIPDB

Returns a new instance of IPDB.



35
36
37
38
39
40
41
42
# File 'lib/geoipdb.rb', line 35

def initialize
  @version = Gem.loaded_specs['geoipdb'].version.to_s
  @cache_path = File.expand_path("~/.cache/geoipdb/v#{@version}")
  # by default load sample data
  sample_path = File.expand_path('../../sample_data', __FILE__)
  files = DATA_FILES.map { |file_name| File.join(sample_path, file_name) }
  self.load(*files)
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



17
18
19
# File 'lib/geoipdb.rb', line 17

def base_url
  @base_url
end

Class Method Details

.init(base_url) ⇒ Object



27
28
29
# File 'lib/geoipdb.rb', line 27

def self.init(base_url)
  instance.update!(base_url)
end

.lookup(ip) ⇒ Object



31
32
33
# File 'lib/geoipdb.rb', line 31

def self.lookup(ip)
  instance.lookup(ip)
end

Instance Method Details

#load(*files) ⇒ Object



44
45
46
# File 'lib/geoipdb.rb', line 44

def load(*files)
  @db = Java::IPDB.new(*files)
end

#load_data_filesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/geoipdb.rb', line 67

def load_data_files
  files = DATA_FILES.map { |file_name| File.join(@cache_path, file_name) }
  self.load(*files)
  self_test
  make_backup if @updating
  $log.info("ipdb:init", rt: Time.now.to_f - @start)
rescue IPDBError => e
  $log.exception(e)
  $log.info("ipdb:init", revert: true)
  restore_backup
  load_data_files
  self_test
end

#lookup(ip) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/geoipdb.rb', line 81

def lookup(ip)
  return nil unless @db
  ip = InetAddress.get_by_name(ip).address if ip.is_a?(String)
  range = @db.find_range_for_ip(ip)
  return nil unless range
  city = @db.find_city_for_ip_range(range)
  return nil unless city
  isp = range.isp_name
  info = IpInformation.new
  info.country_iso_code = city.country_iso2
  info.city_name = city.name
  info.city_code = city.city_code
  info.lng = city.lng
  info.lat = city.lat
  info.is_mobile  = range.is_mobile
  info.isp_name = isp && isp.to_sym
  info
rescue EncodingError, NumberFormatException
  # TODO: do we really want to silence these exceptions?
  return nil
end

#update!(base_url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/geoipdb.rb', line 48

def update!(base_url)
  return nil if @initialized
  @start = Time.now.to_f
  @base_url = base_url
  @initialized = true

  $log.info("ipdb:load", cache_path: @cache_path)
  FileUtils.mkdir_p(@cache_path)

  @updating = false
  if !uptodate?
    @updating = true
    $log.info("ipdb:init", update: true)
    download_update
  end

  load_data_files
end