Rails 从 ZIP Geoip 获取状态

Rails Get State from ZIP Geoip

我有他们输入的用户的邮政编码,想用 city/state 预填充一些字段。有谁知道获取此信息的好方法?我想做这样的事情:

GeoIP.new('geoip/GeoLiteCity.dat').zip('53593')

我知道我可以做到 GeoIP.new('geoip/GeoLiteCity.dat').city(request.remote_ip) 但那是他们当时所在的地方,而不是他们实际居住的地方。

我不认为 geoip Ruby gem (Github repo) has the ability to search by zip / postal code. When scanning the geoip.rb source,没有 zippostal_code 的方法。

还有一个 gem、my_zipcode_gem, that you may be able to use. This is also hosted on Github,语法如下:

zipcode = Zipcode.find_by_code '66206'
zipcode.state.abbr    # => 'KS'
zipcode.city          # => 'Shawnee Mission'
zipcode.county.name   # => 'Johnson'
zipcode.lat.to_s      # => '38.959356', it is actually a BigDecimal object converted to_s for documentation.
zipcode.lon.to_s      # => '-94.716155', ditto
zipcode.is_geocoded?  # => true, most if not all should be pre-geocoded.