class SimpleGeolocator::IPAPIResponse
Constants
- LOCATION_STRUCT
A simple struct that stores the name and code for the location. @param name [String] The name of the location. @param code [String] The location code.
Attributes
@return [String] The name of the city.
@return [LOCATION_STRUCT] The country name and code.
@return [Hash<String, Any>] The full parsed response given by the API.
@return [String] The name of the ISP that the IP is using.
@return [Pair<Float, Float>] The pair of the longitude and latitude.
@return [String] The name of the organization that the IP is within, or their ISP name.
@return [LOCATION_STRUCT] The region name and code.
@return [String] The name of the timezone, e.g., America/Los Angeles.
@return [String] The zip code.
Public Class Methods
Creates a new IPAPIResponse
object. @param response [Hash] The response given by the IP API. @raise [RuntimeError] When the request fails, raises a RuntimeError depending on the error message.
# File lib/simple_geolocator/ipapi_response.rb, line 39 def initialize(response = {}) @full_response = response @status = response['status'] if successful? @country = LOCATION_STRUCT.new(response['country'], response['countryCode']) @region = LOCATION_STRUCT.new(response['regionName'], response['region']) @city = response['city'] @zip = response['zip'] @ll = Pair.new(response['lat'], response['lon']) @isp = response['isp'] @timezone = response['timezone'] @organization = response['org'] @mobile = response['mobile'] @proxy = response['proxy'] else case response['message'] when 'private range' fail 'The IP address is part of a private range.' when 'reserved range' fail 'The IP address is part of a reserved range.' when 'invalid query' fail 'The IP address or domain name is invalid.' when 'quota' fail 'You have reached the IP API rate limit.' else end end end
Public Instance Methods
@return [Boolean] Whether the IP is on a mobile device.
# File lib/simple_geolocator/ipapi_response.rb, line 74 def mobile? @mobile end
@return [Boolean] Whether the IP is on a proxy.
# File lib/simple_geolocator/ipapi_response.rb, line 79 def proxy? @proxy end
@return [Boolean] Whether the request was successful.
# File lib/simple_geolocator/ipapi_response.rb, line 69 def successful? @status == 'success' end