Skip to content

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

@lStARl

Description

@lStARl

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions