Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lStARl opened this issue Sep 6, 2018 · 0 comments

Comments

@lStARl
Copy link

lStARl commented Sep 6, 2018

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.

@lStARl lStARl closed this as completed Sep 6, 2018
@lStARl lStARl reopened this Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant