Skip to content

Instantly share code, notes, and snippets.

@hindenbug
Last active January 19, 2026 15:41
Show Gist options
  • Select an option

  • Save hindenbug/02933aca96401fcde7fcfac84f3fe6e0 to your computer and use it in GitHub Desktop.

Select an option

Save hindenbug/02933aca96401fcde7fcfac84f3fe6e0 to your computer and use it in GitHub Desktop.
# Here's my code with a few minor improvements
class IpLookupService
def self.lookup_country(ip_address)
result = self.lookup(ip_address)
# expecting result is a object/instance returned by @g.lookup method
raise GeoIpDB::Error unless result && result.found?
(result.country && result.country.iso_code) ? result.country.iso_code.downcase : raise GeoIpDB::Error
rescue GeoIpDB::Error
"en"
end
private
def self.init
@g ||= GeoIpDB.new
end
def self.lookup(ip_address)
self.init
@g.lookup(ip_address)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment