Getting a user country name from originating IP address with Ruby on Rails

前端 未结 11 1676
一整个雨季
一整个雨季 2020-12-04 08:26

I want to extract a user country name from visitors\' IP addresses.

I could get the IP address with remote_ip. But what could be the easiest way to get

11条回答
  •  感情败类
    2020-12-04 08:57

    Try the IP2Location Ruby

    https://github.com/ip2location/ip2location-ruby

    Pre-requisite

    Download the free LITE database from http://lite.ip2location.com/ and use below.

    Install

    gem install ip2location_ruby
    

    Usage

    require 'ip2location_ruby'
    
    i2l = Ip2location.new.open("./data/IP-COUNTRY-SAMPLE.BIN")
    record = i2l.get_all('8.8.8.8')
    
    print 'Country Code: ' + record.country_short + "\n"
    print 'Country Name: ' + record.country_long + "\n"
    print 'Region Name: ' + record.region + "\n"
    print 'City Name: ' + record.city + "\n"
    print 'Latitude: '
    print record.latitude
    print "\n"
    print 'Longitude: '
    print record.longitude
    print "\n"
    print 'ISP: ' + record.isp + "\n"
    print 'Domain: ' + record.domain + "\n"
    print 'Net Speed: ' + record.netspeed + "\n"
    print 'Area Code: ' + record.areacode + "\n"
    print 'IDD Code: ' + record.iddcode + "\n"
    print 'Time Zone: ' + record.timezone + "\n"
    print 'ZIP Code: ' + record.zipcode + "\n"
    print 'Weather Station Code: ' + record.weatherstationname + "\n"
    print 'Weather Station Name: ' + record.weatherstationcode + "\n"
    print 'MCC: ' + record.mcc + "\n"
    print 'MNC: ' + record.mnc + "\n"
    print 'Mobile Name: ' + record.mobilebrand + "\n"
    print 'Elevation: '
    print record.elevation
    print "\n"
    print 'Usage Type: ' + record.usagetype + "\n"
    

提交回复
热议问题