Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

GeoIP delegating to GeoKit. Implemented support to US zipcode.

  • Loading branch information...
commit 254bf398520292a6985c24ce8dcbb659625133b7 1 parent b10bac5
Esdras Mayrink authored
2  README
View
@@ -1,7 +1,5 @@
This gem was created with geolocation in mind, meaning that the main goal is to get a latitude and longitude, but it will return additional data if available. With this gem you can pass an IP number or an ZIP number or an ordinary address and it will try to get as many information as possible about the given argument.
-* IMPORTANT: At this moment only brazilian zipcode is supported. Feel free to add suport for zip codes from other countries.
-
@usage
gem install simple_geolocation
2  lib/simple_geolocation/geocoder.rb
View
@@ -8,7 +8,7 @@ def self.keys
Location::ATTRIBUTES + [:completeness]
end
- delegate :lat, :lng, :city, :state, :provider, :zip, :street, :district, :number, :completeness, :to => :location
+ delegate :lat, :lng, :city, :state, :country, :provider, :zip, :street, :district, :number, :completeness, :to => :location
def initialize(raw_location)
@raw_location = raw_location
14 lib/simple_geolocation/geoworkers/geoip/geoip.rb
View
@@ -16,14 +16,12 @@ def ip_geocoder
def get_location!
result = ip_geocoder.city(geocoder.raw_location)
- if @success = result && result.city_name.present? && result.region_name.present?
- @location = Location.new(
- :lat => result.latitude,
- :lng => result.longitude,
- :city => result.city_name,
- :state => result.region_name,
- :provider => "geoip"
- )
+ if @success = result && result.latitude.present? && result.longitude.present?
+ new_raw_location = "#{result.latitude}, #{result.longitude}"
+ geokit = Geokit.new(Geocoder.new(new_raw_location))
+ geokit.process!
+ @success = geokit.success?
+ @location = geokit.location
else
Geokit.new(geocoder).process!
end
1  lib/simple_geolocation/geoworkers/geokit/geokit.rb
View
@@ -22,6 +22,7 @@ def get_location!
:lng => result.lng,
:city => result.city,
:state => result.state,
+ :country => result.country,
:street => result.street_name,
:number => result.street_number,
:zip => result.zip,
2  lib/simple_geolocation/location.rb
View
@@ -1,6 +1,6 @@
module SimpleGeolocation
class Location
- ATTRIBUTES = [:lat, :lng, :city, :state, :provider, :zip, :street, :district, :number]
+ ATTRIBUTES = [:lat, :lng, :city, :state, :country, :provider, :zip, :street, :district, :number]
attr_accessor *ATTRIBUTES
def initialize(attributes = {})
2  lib/simple_geolocation/version.rb
View
@@ -1,3 +1,3 @@
module SimpleGeolocation
- VERSION = "0.0.5"
+ VERSION = "0.0.6"
end
17 spec/simple_geolocation_spec.rb
View
@@ -5,7 +5,7 @@
it 'should identify if the given location is an ip' do
- @geo = SimpleGeolocation::Geocoder.new("74.125.113.105")
+ @geo = SimpleGeolocation::Geocoder.new("189.58.112.247")
@geo.ip?.should be_true
@geo.zip?.should be_false
@geo.address?.should be_false
@@ -26,13 +26,16 @@
end
it "should return the location of an user's based on IP" do
- @geo = SimpleGeolocation::Geocoder.new("74.125.113.105")
+ @geo = SimpleGeolocation::Geocoder.new("189.58.112.247")
@geo.ip?.should be_true
@geo.geocode!
@geo.success?.should be_true
- @geo.lat.should == 37.41919999999999
- @geo.lng.should == -122.0574
- @geo.completeness.should == 55
+ @geo.lat.should == -26.3001576
+ @geo.lng.should == -48.8320838
+ @geo.city.should == "Joinville"
+ @geo.state.should == "SC"
+ @geo.country.should == "Brasil"
+ @geo.completeness.should == 90
end
it "should return the location of an ordinary address" do
@@ -42,7 +45,7 @@
@geo.success?.should be_true
@geo.lat.should == -26.30101
@geo.lng.should == -48.8452974
- @geo.completeness.should == 88
+ @geo.completeness.should == 90
end
it "should return the location of a brazilian zipcode (CEP)" do
@@ -57,7 +60,7 @@
@geo.zip.should == "22640-100"
@geo.lat.should == -23.0032808
@geo.lng.should == -43.3230295
- @geo.completeness.should == 88
+ @geo.completeness.should == 90
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.