1

I have already search out on the stack overflow but didn't find any answered related to it.

With the following below line we can get the ip address of request.

request.remote_ip 

it's working fine for the ip address and gives me correct ip address of the remote machine.

But for the location it doesn't gives me the correct location.

request.location

With the above line the data which i get is like below

#<Geocoder::Result::Freegeoip:0xb47eae8c @data={"ip"=>"192.168.1.183", "country_code"=>"RD", "country_name"=>"Reserved", "region_code"=>"", "region_name"=>"", "city"=>"", "zipcode"=>"", "latitude"=>0, "longitude"=>0, "metro_code"=>"", "areacode"=>""}, @cache_hit=nil>

But the ip address is currently in india and it gives me the country name "Reserved"

Any one help will defiantly appreciate

Bharat soni
  • 2,686
  • 18
  • 27

3 Answers3

1

I'm not a Ruby programmer, PHP instead. But I doubt Ruby has an innate way to know what country an IP address is located. Try the API from a site like this: http://www.geobytes.com/IpLocator.htm

Thread7
  • 1,081
  • 2
  • 14
  • 28
1

use geoip gem,

visit this SO for more and detailed answers.

If you do not want to use gem then use following simple code:--

require 'net/http'

 locale = Timeout::timeout(5) { Net::HTTP.get_response(URI.parse('api.hostip.info/country.php?ip='; + request.remote_ip )).body } rescue "US" 
Community
  • 1
  • 1
Alok Anand
  • 3,346
  • 1
  • 20
  • 17
1

Okay, here is the sample code that you may refer to.

Retreive the geolocation of an IP address using Ruby

HexaHow
  • 121
  • 1
  • 3