1

I am using Rails 5 with ruby 2.4 I am using TCP server for the TCP api communication. I am able to find the Ip address from which the request is getting generated like:

    require 'socket'
    server = TCPServer.new 53492 # Server bound to port 53492
        loop do
          Thread.start(server.accept) do |client|
            p "Client peer address = #{client.peeraddr[3]}"
         end
       end

Can I get the country form which the request client is using the Api.

vidur punj
  • 5,019
  • 4
  • 46
  • 65
  • Possible duplicate: https://stackoverflow.com/questions/1988049/getting-a-user-country-name-from-originating-ip-address-with-ruby-on-rails – Casper Dec 06 '19 at 12:14

2 Answers2

2

This is called geolocating. Just google "ruby geolocation" and you will find gems and resources. Here are a couple that are able to convert an IP address into a geolocation:

https://github.com/alexreisner/geocoder
https://github.com/geokit/geokit

Casper
  • 33,403
  • 4
  • 84
  • 79
1

Reference: https://github.com/alexreisner/geocoder Included gem in gemfile:

 gem 'geocoder'

in the code:

   results = Geocoder.search(remote_ip)
   country = results.first.country
vidur punj
  • 5,019
  • 4
  • 46
  • 65