Y
Y
yezolux2014-11-27 14:08:25
Ruby on Rails
yezolux, 2014-11-27 14:08:25

How to properly parse Sypexgeo json in Rails?

Perhaps the question is very stupid. But I could not pull out only the city from json (name_ru).
How to do it right? Tried to do so

def location_city
    @remote_ip = request.remote_ip
    @result = Net::HTTP.get(URI.parse("http://api.sypexgeo.net/json/#{@remote_ip}"))
    @city = ?
  end

When requested from ip, json is returned. Type:
{
   "ip":"123.45.67.89",
   "city":{
      ...
      "name_ru":"Сеул",
      ...
   },
   "region":{
      ...
   },
   "country":{
      ...
   }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2014-11-27
@yezolux

@result = JSON.parse(@result)
@city = @result['city']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question