Class: SimpleGeolocator::IPAPIResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_geolocator/ipapi_response.rb

Defined Under Namespace

Classes: LOCATION_STRUCT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = {}) ⇒ IPAPIResponse

Creates a new IPAPIResponse object.

Parameters:

  • response (Hash) (defaults to: {})

    The response given by the IP API.

Raises:

  • (RuntimeError)

    When the request fails, raises a RuntimeError depending on the error message.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Attribute Details

#cityString (readonly)

Returns The name of the city.

Returns:

  • (String)

    The name of the city.



14
15
16
# File 'lib/simple_geolocator/ipapi_response.rb', line 14

def city
  @city
end

#countryLOCATION_STRUCT (readonly)

Returns The country name and code.

Returns:



8
9
10
# File 'lib/simple_geolocator/ipapi_response.rb', line 8

def country
  @country
end

#full_responseHash<String, Any> (readonly)

Returns The full parsed response given by the API.

Returns:

  • (Hash<String, Any>)

    The full parsed response given by the API.



5
6
7
# File 'lib/simple_geolocator/ipapi_response.rb', line 5

def full_response
  @full_response
end

#ispString (readonly)

Returns The name of the ISP that the IP is using.

Returns:

  • (String)

    The name of the ISP that the IP is using.



26
27
28
# File 'lib/simple_geolocator/ipapi_response.rb', line 26

def isp
  @isp
end

#llPair<Float, Float> (readonly)

Returns The pair of the longitude and latitude.

Returns:

  • (Pair<Float, Float>)

    The pair of the longitude and latitude.



20
21
22
# File 'lib/simple_geolocator/ipapi_response.rb', line 20

def ll
  @ll
end

#organizationString (readonly)

Returns The name of the organization that the IP is within, or their ISP name.

Returns:

  • (String)

    The name of the organization that the IP is within, or their ISP name.



29
30
31
# File 'lib/simple_geolocator/ipapi_response.rb', line 29

def organization
  @organization
end

#regionLOCATION_STRUCT (readonly)

Returns The region name and code.

Returns:



11
12
13
# File 'lib/simple_geolocator/ipapi_response.rb', line 11

def region
  @region
end

#timezoneString (readonly)

Returns The name of the timezone, e.g., America/Los Angeles.

Returns:

  • (String)

    The name of the timezone, e.g., America/Los Angeles.



23
24
25
# File 'lib/simple_geolocator/ipapi_response.rb', line 23

def timezone
  @timezone
end

#zipString (readonly)

Returns The zip code.

Returns:

  • (String)

    The zip code.



17
18
19
# File 'lib/simple_geolocator/ipapi_response.rb', line 17

def zip
  @zip
end

Instance Method Details

#mobile?Boolean

Returns Whether the IP is on a mobile device.

Returns:

  • (Boolean)

    Whether the IP is on a mobile device.



74
75
76
# File 'lib/simple_geolocator/ipapi_response.rb', line 74

def mobile?
  @mobile
end

#proxy?Boolean

Returns Whether the IP is on a proxy.

Returns:

  • (Boolean)

    Whether the IP is on a proxy.



79
80
81
# File 'lib/simple_geolocator/ipapi_response.rb', line 79

def proxy?
  @proxy
end

#successful?Boolean

Returns Whether the request was successful.

Returns:

  • (Boolean)

    Whether the request was successful.



69
70
71
# File 'lib/simple_geolocator/ipapi_response.rb', line 69

def successful?
  @status == 'success'
end