Last active
January 19, 2026 15:41
-
-
Save hindenbug/02933aca96401fcde7fcfac84f3fe6e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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