ruby:给定一个ip地址,找出城市和州

ruby: given an ip address, find out the City and State

我已经搜索了大约十分钟,还没有找到简单的代码示例。

基本上,我正在寻找这样的东西:

puts Geocoder.lookup_location_by_ip("76.95.251.102")

result California, USA

所以 ruby 方法将 ip 地址解码到它的城市,州。

这些是步骤:

  1. 安装地理编码器使用:

    gem install geocoder
    
  2. 在 Ruby 脚本中使用:

    require "geocoder"
    puts Geocoder.address('76.95.251.102')   #Change the ip address accordingly.
    
  3. 运行 获取城市、州、邮政编码、国家/地区的脚本。

    # => Bellflower, CA 90706, United States