Skip to content
Browse files

Cleaned up the look of the code.

Those comments were pretty bad; they just re-iterated what the
code does, rather than why the code is doing it. We'll reintroduce
comments in another commit, but first, let's just clean up the look
and some minor spacing issues.
  • Loading branch information...
1 parent 664ec18 commit 57c4a1dff73f7c27768cb8c194fca4aeebc699ce @steveklabnik committed Jan 12, 2012
Showing with 3 additions and 11 deletions.
  1. +3 −11 lib/geolocator.rb
View
14 lib/geolocator.rb
@@ -10,13 +10,10 @@ def initialize(ip_address)
end
def ip_lookup
- #IP Regex check
if /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/.match(ip_address).nil?
raise "Not a valid IPv4 address"
- #Localhost Check
elsif ip_address == "127.0.0.1"
raise "Can't lookup localhost address. Please use an external IP address!"
- #If valid, pass the ip address to the 'geolocate' method
else
FreeGeoIp.new(ip_address).geolocate_ip
end
@@ -32,22 +29,17 @@ def initialize(ip_address)
end
def geolocate_ip
- #the request URI
uri = "http://freegeoip.net/json/#{ip_address}"
- #Get the response with Faraday and store to 'http_response' var
http_response = Faraday.get uri
- #If the HTTP request is successful...
- if http_response.success? == true && http_response.status == 200;
- #JSON PARSE THE BODY
+
+ if http_response.success? == true && http_response.status == 200
result = JSON.parse(http_response.body)
- #If the response is missing basic info, like 'city'...
+
if result["city"].empty?
raise "Incomplete record. Please try another address"
- #otherwise return the result
else
return result
end
- #If the HTTP request fails...
else
raise "IP address not found"
end

0 comments on commit 57c4a1d

Please sign in to comment.
Something went wrong with that request. Please try again.