Question

I am trying to obtain the city, country and region name with using the Geocoder gem. This is how I am doing it:

geo_data = Geocoder.search('50.131.44.XXX')
puts geo_data.inspect
puts geo_data[0].city
#puts geo_data[0].inspect
puts geo_data[0].country_code
puts geo_data[0].region_name

and the output:

[#<Geocoder::Result::Freegeoip:0x007fd6db981b38 @data={"ip"=>"50.131.44.XXX", "country_code"=>"US", "country_name"=>"United States", "region_code"=>"CA", "region_name"=>"California", "city"=>"Sunnyvale", "zipcode"=>"94087", "latitude"=>XXX, "longitude"=>-XXX, "metro_code"=>"807", "areacode"=>"408"}, @cache_hit=nil>]
Sunnyvale
US
NoMethodError (undefined method `region_name' for #<Geocoder::Result::Freegeoip:0x007fd6db981b38>):

Why I cannot get the region name?

Thanks

Was it helpful?

Solution

geo_data[0].data["region_name"].Accessing a value in a Hash#[] requires using its key

Geocoder.search() method return array with object data. In data you have simple hash with value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top