Class: MaxMind::GeoIP2::Client
- Inherits:
-
Object
- Object
- MaxMind::GeoIP2::Client
- Defined in:
- lib/maxmind/geoip2/client.rb
Overview
This class provides a client API for all the GeoIP2 web services. The services are Country, City Plus, and Insights. Each service returns a different set of data about an IP address, with Country returning the least data and Insights the most.
Each web service is represented by a different model class, and these model classes in turn contain multiple record classes. The record classes have attributes which contain data about the IP address.
If the web service does not return a particular piece of data for an IP address, the associated attribute is not populated.
The web service may not return any information for an entire record, in which case all of the attributes for that record class will be empty.
Usage
The basic API for this class is the same for all of the web service end points. First you create a web service client object with your MaxMind account ID and license key, then you call the method corresponding to a specific end point, passing it the IP address you want to look up.
If the request succeeds, the method call will return a model class for the service you called. This model in turn contains multiple record classes, each of which represents part of the data returned by the web service.
If the request fails, the client class throws an exception.
Example
require 'maxmind/geoip2'
client = MaxMind::GeoIP2::Client.new(
account_id: 42,
license_key: 'abcdef123456',
)
# Replace 'city' with the method corresponding to the web service you
# are using, e.g., 'country', 'insights'.
record = client.city('128.101.101.101')
puts record.country.iso_code
Instance Method Summary collapse
-
#city(ip_address = 'me') ⇒ MaxMind::GeoIP2::Model::City
This method calls the City Plus web service.
-
#country(ip_address = 'me') ⇒ MaxMind::GeoIP2::Model::Country
This method calls the Country web service.
-
#initialize(account_id:, license_key:, locales: ['en'], host: 'geoip.maxmind.com', timeout: 0, proxy_address: '', proxy_port: 0, proxy_username: '', proxy_password: '', pool_size: 5) ⇒ Client
constructor
Create a Client that may be used to query a GeoIP2 web service.
-
#insights(ip_address = 'me') ⇒ MaxMind::GeoIP2::Model::Insights
This method calls the Insights web service.
Constructor Details
#initialize(account_id:, license_key:, locales: ['en'], host: 'geoip.maxmind.com', timeout: 0, proxy_address: '', proxy_port: 0, proxy_username: '', proxy_password: '', pool_size: 5) ⇒ Client
Create a Client that may be used to query a GeoIP2 web service.
Once created, the Client is safe to use for lookups from multiple threads.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/maxmind/geoip2/client.rb', line 93 def initialize( account_id:, license_key:, locales: ['en'], host: 'geoip.maxmind.com', timeout: 0, proxy_address: '', proxy_port: 0, proxy_username: '', proxy_password: '', pool_size: 5 ) @account_id = account_id @license_key = license_key @locales = locales || ['en'] @host = host || 'geoip.maxmind.com' @timeout = timeout || 0 @proxy_address = proxy_address || '' @proxy_port = proxy_port || 0 @proxy_username = proxy_username || '' @proxy_password = proxy_password || '' @pool_size = pool_size || 5 @connection_pool = ConnectionPool.new(size: @pool_size) do make_http_client.persistent("https://#{@host}") end end |
Instance Method Details
#city(ip_address = 'me') ⇒ MaxMind::GeoIP2::Model::City
This method calls the City Plus web service.
157 158 159 |
# File 'lib/maxmind/geoip2/client.rb', line 157 def city(ip_address = 'me') response_for('city', MaxMind::GeoIP2::Model::City, ip_address) end |
#country(ip_address = 'me') ⇒ MaxMind::GeoIP2::Model::Country
This method calls the Country web service.
194 195 196 |
# File 'lib/maxmind/geoip2/client.rb', line 194 def country(ip_address = 'me') response_for('country', MaxMind::GeoIP2::Model::Country, ip_address) end |
#insights(ip_address = 'me') ⇒ MaxMind::GeoIP2::Model::Insights
This method calls the Insights web service.
Insights is only supported by the GeoIP2 web service. The GeoLite2 web service does not support it.
234 235 236 |
# File 'lib/maxmind/geoip2/client.rb', line 234 def insights(ip_address = 'me') response_for('insights', MaxMind::GeoIP2::Model::Insights, ip_address) end |