Skip to content

Commit

Permalink
Refactor geo-location service
Browse files Browse the repository at this point in the history
* Removes sporadically failing tests relying on possibly outdated info.
* Renames GeoIPCountry to GeoLocation because we're not using geo_ip gem anymore.
  • Loading branch information
afred committed Dec 1, 2022
1 parent d7bc713 commit 19e06f7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 66 deletions.
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'set'
require 'ipaddr'
require_relative '../../lib/geo_i_p_country'
require 'geo_location'

class User
DARTMOUTH_HOST_RE = /^(.+\.)?meptest\.dartmouth\.edu$/
Expand All @@ -22,7 +22,7 @@ def onsite?
end

def usa?
%w(US PR WS GU MP VI).include?(::GeoIPCountry.instance.country_code(request.remote_ip)) || onsite?
%w(US PR WS GU MP VI).include?(GeoLocation.country_code(request.remote_ip)) || onsite?
# WGBH doesn't actually geocode to USA. No idea why.
end

Expand Down
7 changes: 2 additions & 5 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ class Application < Rails::Application
config.exceptions_app = routes
config.cache_store = :memory_store

# load that geocode ip!
config.after_initialize do
@mmdb = MaxMindDB.new(Rails.root + 'config/GeoLite2-Country.mmdb')
Rails.cache.write('maxmind_db', @mmdb)
end
# Call GeoLocation.mmdb to warm the cache.
config.after_initialize { GeoLocation.mmdb }
end
end
14 changes: 0 additions & 14 deletions lib/geo_i_p_country.rb

This file was deleted.

23 changes: 23 additions & 0 deletions lib/geo_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'maxminddb'

class GeoLocation
MMDB_PATH = Rails.root + 'config/GeoLite2-Country.mmdb'
CACHE_KEY = 'maxmind_db'

class << self
# Looks up an IP address to find out what country it's coming from.
# @return [String] capitalized, two-character ISO country code of the IP's
# origin according to our current copy of MaxMindDB's GeoLite2 Country db.
def country_code(id)
mmdb.lookup(ip).country.iso_code
end

# Caches a new instance of MaxMindDB::Client at CACHE_KEY loaded with data
# from MMDB_PATH and returns it.
# @return [MaxMindDB::Client] client instance used to look up country of
# origin for IP address.
def mmdb
Rails.cache.fetch(CACHE_KEY) { MaxMindDB.new(MMDB_PATH) }
end
end
end
45 changes: 0 additions & 45 deletions spec/lib/geo_i_p_country_spec.rb

This file was deleted.

18 changes: 18 additions & 0 deletions spec/lib/geo_location_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'geo_location'
require 'resolv'

describe GeoLocation do
describe '.country_code' do
let(:subject) { GeoLocation.country_code(ip) }

context 'when IP is invalid' do
let(:ip) { '0.0.0.0' }
it { is_expected.to be_nil }
end

context 'when IP is valid' do
let(:ip) { Resolv.getaddress('americanarchive.org') }
it { is_expected.to be_nil }
end
end
end

0 comments on commit 19e06f7

Please sign in to comment.