geokit / geokit-rails

Official Geokit plugin for Rails/ActiveRecord. Provides location-based goodness for your Rails app. Requires the Geokit gem.
MIT License
1.57k stars 245 forks source link

Bug in the GeoLoc.new method and related to the retrieve_location_from_cookie_or_service method. #137

Open lStARl opened 6 years ago

lStARl commented 6 years ago

Hi all. When I visit a website for the first time geolocation information is stored in session and cookies. When I delete session cookie in the browser and update a page the bug occurs.

def retrieve_location_from_cookie_or_service
  return GeoLoc.new(YAML.load(cookies[:geo_location])) if cookies[:geo_location]
  location = Geocoders::MultiGeocoder.geocode(get_ip_address)
  return location.success ? location : nil
end

YAML.load(cookies[:geo_location] returns a hash with valid with valid information {"success"=>true, "lat"=>******, "lng"=>******, "country_code"=>"UA", "city"=>"******", "state"=>"******", "zip"=>nil, "street_address"=>nil, "district"=>nil, "provider"=>"geo_plugin", "full_address"=>"*******", "is_us?"=>false, "ll"=>"******", "precision"=>"unknown", "district_fips"=>nil, "state_fips"=>nil, "block_fips"=>nil, "sub_premise"=>nil}. But GeoLoc.new(YAML.load(cookies[:geo_location])) returns next result #<Geokit::GeoLoc:0x00007f52e8618b58 @all=[#<Geokit::GeoLoc:0x00007f52e8618b58 ...>], @street_address=nil, @sub_premise=nil, @street_number=nil, @street_name=nil, @city=nil, @state=nil, @state_code=nil, @state_name=nil, @zip=nil, @country_code=nil, @success=false, @precision="unknown", @full_address=nil, @lat=nil, @lng=nil> The problem is in the GeoLoc.new method.

def initialize(h = {})
      @all = [self]

      # sanatises the GeoLoc object so that it conforms to []
      h = h.to_hash

      @street_address = h[:street_address]
      @sub_premise = nil
      @street_number = nil
      @street_name = nil
      @city = h[:city]
      @state = h[:state]
      @state_code = h[:state_code]
      @state_name = h[:state_name]
      @zip = h[:zip]
      @country_code = h[:country_code]
      @success = false
      @precision = 'unknown'
      @full_address = nil
      super(h[:lat], h[:lng])
    end

The initialize method expects the h variable is a hash with keys as symbols, but as you can see above YAML.load(cookies[:geo_location] returns a hash with keys as strings. So instead of getting an object with valid information, I get an object with nil attributes.