ruby-on-railsrubyrubygemsgeocoder

Ruby-on-Rails, Geocoding and IPv6


For years I have been determined a users location with the help of Geocoder, looking up the IPv4 address. By firing

request.location, I determined the location, and if the country was multilingual, I looked up the browser language setting to set the appropriate language. When I feed an IPv6 address to my Rails app, the IP address returns :::1, which isn't very helpful.

Unfortunately, IPv6 poses a challenge when it comes to determining a visitors location. Is there any proven way of working with IPv6 on Ruby-on-Rails?

I have googled my way through the internet but didn't come across a specific solution.


Solution

  • You can do this with the ipaddr library. It supports ipv4 and ipv6:

    $ require 'ipaddr'
    $ ipaddr = IPAddr.new "3ffe:505:2::1"
    $ p ipaddr                   
    #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
    $ p ipaddr.to_s              
    #=> "3ffe:505:2::1"
    

    If you want more advanced functionality, try the ipaddress gem which also supports ipv6.

    You can use Geocoder.search() to search by an ipv6 address:

    Geocoder.search(ipaddr)
    Geocoder.search('2607:fea8:1360:f7d:dce7:b7f0:e0b6:1014')