diff --git a/README_API_GUIDE.md b/README_API_GUIDE.md index da3db7c46cca8348061c216868da2815e4493526..cf1a5ebc5b703b3d5fa6c9475d74d9b0a2c8b49f 100644 --- a/README_API_GUIDE.md +++ b/README_API_GUIDE.md @@ -463,16 +463,16 @@ IP Address Lookups * **Terms of Service**: https://ipdata.co/terms.html * **Limitations**: ? -### IP2Location Web Service (`:ip2location`) +### IP2Location (`:ip2location`) -* **API key**: optional, see: https://www.ip2location.com/web-service -* **Quota**: 20 query/day (up to 100k credits with paid API key) +* **API key**: optional (20 free demo queries per day) +* **Quota**: up to 100k credits with paid API key * **Region**: world * **SSL support**: yes * **Languages**: English * **Documentation**: https://www.ip2location.com/web-service * **Terms of Service**: https://www.ip2location.com/web-service -* **Notes**: To use IP2Location Web Service with API Key set `Geocoder.configure(:ip_lookup => :ip2location, :api_key => "IP2LOCATION_WEB_SERVICE_API_KEY")`. Supports the optional param :package with `Geocoder.configure(:ip2location => {:package => "WSX"})` (see API documentation for package offered in details). +* **Notes**: With the non-free version, specify your desired package: `Geocoder.configure(ip2location: {package: "WSX"})` (see API documentation for package details). Local IP Address Lookups diff --git a/lib/geocoder/lookups/ip2location.rb b/lib/geocoder/lookups/ip2location.rb index 019a6dad2da9c051c0847828c057e49af65ea6d4..56eab4a2e28c039920190b1354b7b16bb0e3d8f0 100644 --- a/lib/geocoder/lookups/ip2location.rb +++ b/lib/geocoder/lookups/ip2location.rb @@ -10,26 +10,24 @@ module Geocoder::Lookup def query_url(query) api_key = configuration.api_key ? configuration.api_key : "demo" - url_ = "#{protocol}://api.ip2location.com/?ip=#{query.sanitized_text}&key=#{api_key}&format=json" + url = "#{protocol}://api.ip2location.com/?ip=#{query.sanitized_text}&key=#{api_key}&format=json" - if (params = url_query_string(query)) && !params.empty? - url_ + "&" + params - else - url_ + if (params = url_query_string(query)) and !params.empty? + url << "&" + params end + + url end def supported_protocols [:http, :https] end - private + private # ---------------------------------------------------------------- def results(query) return [reserved_result(query.text)] if query.loopback_ip_address? - return [] unless doc = fetch_data(query) - if doc["response"] == "INVALID ACCOUNT" raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "INVALID ACCOUNT") return [] @@ -64,9 +62,11 @@ module Geocoder::Lookup end def query_url_params(query) - params = {} - params.merge!(package: configuration[:package]) if configuration.has_key?(:package) - params.merge(super) + params = super + if configuration.has_key?(:package) + params.merge!(package: configuration[:package]) + end + params end end diff --git a/lib/geocoder/results/ip2location.rb b/lib/geocoder/results/ip2location.rb index f6cbf74ef19119431648743772de9746ce9f5147..57d0c17853d8c241410df9f8beaab0cb1f193cd5 100644 --- a/lib/geocoder/results/ip2location.rb +++ b/lib/geocoder/results/ip2location.rb @@ -7,85 +7,16 @@ module Geocoder::Result "#{city_name} #{zip_code}, #{country_name}".sub(/^[ ,]*/, '') end - def country_code - @data['country_code'] + def self.response_attributes + %w[country_code country_name region_name city_name latitude longitude + zip_code time_zone isp domain net_speed idd_code area_code usage_type + weather_station_code weather_station_name mcc mnc mobile_brand elevation] end - def country_name - @data['country_name'] + response_attributes.each do |attr| + define_method attr do + @data[attr] || "" + end end - - def region_name - @data['region_name'] - end - - def city_name - @data['city_name'] - end - - def latitude - @data['latitude'] - end - - def longitude - @data['longitude'] - end - - def zip_code - @data['zip_code'] - end - - def time_zone - @data['time_zone'] - end - - def isp - @data['isp'] - end - - def domain - @data['domain'] - end - - def net_speed - @data['net_speed'] - end - - def idd_code - @data['idd_code'] - end - - def area_code - @data['area_code'] - end - - def weather_station_code - @data['weather_station_code'] - end - - def weather_station_name - @data['weather_station_name'] - end - - def mcc - @data['mcc'] - end - - def mnc - @data['mnc'] - end - - def mobile_brand - @data['mobile_brand'] - end - - def elevation - @data['elevation'] - end - - def usage_type - @data['usage_type'] - end - end end