diff --git a/lib/geolocator.rb b/lib/geolocator.rb index 40f3e61..dc406ec 100644 --- a/lib/geolocator.rb +++ b/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 diff --git a/lib/geolocator/free_geo_ip.rb b/lib/geolocator/free_geo_ip.rb new file mode 100644 index 0000000..c7277ed --- /dev/null +++ b/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 diff --git a/spec/free_geo_ip_spec.rb b/spec/free_geo_ip_spec.rb new file mode 100644 index 0000000..a732cde --- /dev/null +++ b/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 diff --git a/spec/geolocator_spec.rb b/spec/geolocator_spec.rb index 7032509..787de85 100644 --- a/spec/geolocator_spec.rb +++ b/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 - -