Commit de403f3a

mo khan <mo@mokhan.ca>
2014-09-27 14:10:05
extract a location class.
1 parent dfcb3b5
Changed files (2)
app/models/location.rb
@@ -0,0 +1,16 @@
+class Location
+  GEO_IP=GeoIP.new('config/GeoLiteCity.dat')
+
+  class << self
+    def build_from_ip(ip)
+      city = GEO_IP.city(ip)
+      return nil unless city
+      Location.new(
+        latitude: city.latitude,
+        longitude: city.longitude,
+        city: city.city_name,
+        country: city.country_name
+      )
+    end
+  end
+end
app/models/user_session.rb
@@ -39,11 +39,11 @@ class UserSession < ActiveRecord::Base
   end
 
   def apply_geo_location_information_for(request)
-    city = GeoIP.new('config/GeoLiteCity.dat').city(request.ip)
-    return if city.nil?
-    self.latitude = city.latitude
-    self.longitude = city.longitude
-    self.city = city.city_name
-    self.country = city.country_name
+    location = Location.build_from_ip(request.ip)
+    return if location.nil?
+    self.latitude = location.latitude
+    self.longitude = location.longitude
+    self.city = location.city
+    self.country = location.country
   end
 end