Class: Ip2Location::Request

Inherits:
Base
  • Object
show all
Defined in:
lib/ip2location/request.rb

Class Method Summary collapse

Class Method Details

.check_response_for_errors(body) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/ip2location/request.rb', line 25

def self.check_response_for_errors(body)
  case body
  when /Invalid user name or API key/
    raise InvalidApiKeyError, "The API key [#{Ip2Location.api_key}] or Username [#{Ip2Location.user}] is invalid!"
  when /NO DATA/
    raise NoLocationDataError, "The IP you requested has no associated location data."
  else 
    body
  end
end

.parse_response(body, ip) ⇒ Object



19
20
21
22
23
# File 'lib/ip2location/request.rb', line 19

def self.parse_response(body, ip)
  check_response_for_errors(body)
  location_parts = body.split(/,/)
  Location.new(ip, location_parts[0], location_parts[1], location_parts[2], location_parts[3], location_parts[4], location_parts[5])
end