Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor geo-location service #2497

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/controllers/embed_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require_relative '../../lib/geo_i_p_country'
require_relative '../../lib/aapb'

class EmbedController < CatalogController
Expand Down
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'.freeze

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(ip)
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 eq 'US' }
end
end
end