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

city[R]

@return [String] The name of the city.

country[R]

@return [LOCATION_STRUCT] The country name and code.

full_response[R]

@return [Hash<String, Any>] The full parsed response given by the API.

isp[R]

@return [String] The name of the ISP that the IP is using.

ll[R]

@return [Pair<Float, Float>] The pair of the longitude and latitude.

organization[R]

@return [String] The name of the organization that the IP is within, or their ISP name.

region[R]

@return [LOCATION_STRUCT] The region name and code.

timezone[R]

@return [String] The name of the timezone, e.g., America/Los Angeles.

zip[R]

@return [String] The zip code.

Public Class Methods

new(response = {}) click to toggle source

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

mobile?() click to toggle source

@return [Boolean] Whether the IP is on a mobile device.

# File lib/simple_geolocator/ipapi_response.rb, line 74
def mobile?
  @mobile
end
proxy?() click to toggle source

@return [Boolean] Whether the IP is on a proxy.

# File lib/simple_geolocator/ipapi_response.rb, line 79
def proxy?
  @proxy
end
successful?() click to toggle source

@return [Boolean] Whether the request was successful.

# File lib/simple_geolocator/ipapi_response.rb, line 69
def successful?
  @status == 'success'
end