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.

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)



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

def city
  @city
end

#countryLOCATION_STRUCT (readonly)



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

def country
  @country
end

#full_responseHash<String, Any> (readonly)



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

def full_response
  @full_response
end

#ispString (readonly)



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

def isp
  @isp
end

#llPair<Float, Float> (readonly)



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

def ll
  @ll
end

#organizationString (readonly)



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

def organization
  @organization
end

#regionLOCATION_STRUCT (readonly)



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

def region
  @region
end

#timezoneString (readonly)



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

def timezone
  @timezone
end

#zipString (readonly)



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

def zip
  @zip
end

Instance Method Details

#mobile?Boolean



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

def mobile?
  @mobile
end

#proxy?Boolean



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

def proxy?
  @proxy
end

#successful?Boolean



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

def successful?
  @status == 'success'
end