Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Put things in their own files.

We should try to stick to one class per file. Same
with specs.
  • Loading branch information...
commit 09b7620524c96ab9617401bb7f2433361fdc6cd1 1 parent 57c4a1d
@steveklabnik authored
View
27 lib/geolocator.rb
@@ -1,6 +1,7 @@
require "geolocator/version"
require 'faraday'
require 'json'
+require "geolocator/free_geo_ip"
class Geolocator
attr_accessor :ip_address
@@ -19,29 +20,3 @@ def ip_lookup
end
end
end
-
-class Geolocator::FreeGeoIp
-
- attr_accessor :ip_address
-
- def initialize(ip_address)
- @ip_address = ip_address
- end
-
- def geolocate_ip
- uri = "http://freegeoip.net/json/#{ip_address}"
- http_response = Faraday.get uri
-
- if http_response.success? == true && http_response.status == 200
- result = JSON.parse(http_response.body)
-
- if result["city"].empty?
- raise "Incomplete record. Please try another address"
- else
- return result
- end
- else
- raise "IP address not found"
- end
- end
-end
View
27 lib/geolocator/free_geo_ip.rb
@@ -0,0 +1,27 @@
+class Geolocator
+ class FreeGeoIp
+
+ attr_accessor :ip_address
+
+ def initialize(ip_address)
+ @ip_address = ip_address
+ end
+
+ def geolocate_ip
+ uri = "http://freegeoip.net/json/#{ip_address}"
+ http_response = Faraday.get uri
+
+ if http_response.success? == true && http_response.status == 200
+ result = JSON.parse(http_response.body)
+
+ if result["city"].empty?
+ raise "Incomplete record. Please try another address"
+ else
+ return result
+ end
+ else
+ raise "IP address not found"
+ end
+ end
+end
+end
View
32 spec/free_geo_ip_spec.rb
@@ -0,0 +1,32 @@
+require "spec_helper"
+require "geolocator/free_geo_ip"
+
+describe Geolocator::FreeGeoIp do
+ let(:geo_ip) { Geolocator::FreeGeoIp.new(@ip) }
+ describe "#geolocate_ip" do
+ it "successfully makes a HTTP request" do
+ @ip = "231.4.8.6"
+ expect{geo_ip.geolocate_ip.success?}.to be_true
+ end
+
+ it "throws an error for HTTP statuses other than 200" do
+ @ip = "0.0.0.0"
+ expect {geo_ip.geolocate_ip}.to raise_error(RuntimeError,"IP address not found")
+ end
+
+ it "parses the HTTP body with JSON" do
+ @ip = "123.45.6.28"
+ geo_ip.geolocate_ip.should be_an_instance_of Hash
+ end
+
+ it "has a value for city" do
+ @ip = "123.45.6.28"
+ geo_ip.geolocate_ip["city"].should_not be_empty
+ end
+
+ it "raises a RuntimeError if there isn't a city" do
+ @ip = "240.0.0.0"
+ expect {geo_ip.geolocate_ip}.to raise_error(RuntimeError,"Incomplete record. Please try another address")
+ end
+ end
+end
View
31 spec/geolocator_spec.rb
@@ -25,34 +25,3 @@
end
end
-describe Geolocator::FreeGeoIp do
- let(:geo_ip) { Geolocator::FreeGeoIp.new(@ip) }
- describe "#geolocate_ip" do
- it "successfully makes a HTTP request" do
- @ip = "231.4.8.6"
- expect{geo_ip.geolocate_ip.success?}.to be_true
- end
-
- it "throws an error for HTTP statuses other than 200" do
- @ip = "0.0.0.0"
- expect {geo_ip.geolocate_ip}.to raise_error(RuntimeError,"IP address not found")
- end
-
- it "parses the HTTP body with JSON" do
- @ip = "123.45.6.28"
- geo_ip.geolocate_ip.should be_an_instance_of Hash
- end
-
- it "has a value for city" do
- @ip = "123.45.6.28"
- geo_ip.geolocate_ip["city"].should_not be_empty
- end
-
- it "raises a RuntimeError if there isn't a city" do
- @ip = "240.0.0.0"
- expect {geo_ip.geolocate_ip}.to raise_error(RuntimeError,"Incomplete record. Please try another address")
- end
- end
-end
-
-
Please sign in to comment.
Something went wrong with that request. Please try again.