Class: RequestInfo::GeoIP
- Inherits:
-
Object
- Object
- RequestInfo::GeoIP
- Includes:
- Singleton
- Defined in:
- lib/request_info/geoip.rb
Overview
Brigdes MaxmindGeoIP2.
Examples: RequestInfo::GeoIP.instance.lookup(‘116.49.226.82’) RequestInfo::GeoIP.instance.lookup(‘24.24.24.24’)
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
Instance Method Summary collapse
- #ensure_maxmind_geoip2_availability ⇒ Object
- #geoip2_db_path ⇒ Object
-
#initialize ⇒ GeoIP
constructor
Sets up the GeoIPCity database for upcoming queries.
-
#lookup(ip) ⇒ Object
Looks up the specified IP address (string) and returns information about the IP address.
- #setup_database ⇒ Object
Constructor Details
#initialize ⇒ GeoIP
Sets up the GeoIPCity database for upcoming queries
20 21 22 23 24 25 |
# File 'lib/request_info/geoip.rb', line 20 def initialize unless geoip2_db_path.nil? ensure_maxmind_geoip2_availability self.database = setup_database end end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
17 18 19 |
# File 'lib/request_info/geoip.rb', line 17 def database @database end |
Instance Method Details
#ensure_maxmind_geoip2_availability ⇒ Object
44 45 46 47 48 49 |
# File 'lib/request_info/geoip.rb', line 44 def ensure_maxmind_geoip2_availability MaxmindGeoIP2 rescue LoadError raise "RequestInfo requires maxmind_geoip2 gem for GeoIP2 database " + "lookup. Refer to README for configuration details." end |
#geoip2_db_path ⇒ Object
40 41 42 |
# File 'lib/request_info/geoip.rb', line 40 def geoip2_db_path RequestInfo.configuration.geoip2_db_path end |
#lookup(ip) ⇒ Object
Looks up the specified IP address (string) and returns information about the IP address.
35 36 37 38 |
# File 'lib/request_info/geoip.rb', line 35 def lookup(ip) return nil if database.nil? || ip.blank? database.locate(ip) end |
#setup_database ⇒ Object
27 28 29 30 31 |
# File 'lib/request_info/geoip.rb', line 27 def setup_database MaxmindGeoIP2.file(geoip2_db_path) MaxmindGeoIP2.locale("en") MaxmindGeoIP2 end |