Gem 写了一个通过 IP 获得城市名定位的 gem,求指正

pobing · 2013年07月03日 · 最后由 pobing 回复于 2013年07月03日 · 3581 次阅读

以前老用别人的 gem,从没自己写过,最近有个需求没有找到合适的 gem,所以尝试着写了第一个 gem。 地址: https://rubygems.org/gems/sina_geoip 用的是新浪的: [http://int.dpool.sina.com.cn/iplookup/iplookup.phphttp://int.dpool.sina.com.cn/iplookup/iplookup.php)]( 参考自:https://rubygems.org/gems/geo_ip 求大家轻拍,给予意见。

以下是 Readme 的内容:

Retreive the geolocation of an IP address based on the http://int.dpool.sina.com.cn/iplookup/iplookup.php webservice.

Installation

Add this line to your application's Gemfile:

gem 'sina_geoip'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sina_geoip

Usage

Retrieve geolocation

SinaGeoIp.geolocation(ip_address)

Example

# 12.130.132.30 = sina.com.cn (CN) GeoIp.geolocation('12.130.132.30')

returns:

{
  :ret => 1, 
  :start => "202.108.24.0", 
  :end => "202.108.58.255", 
  :country => "中国", 
  :province => "北京", 
  :city => "北京", 
  :district => "", 
  :isp => "联通", 
  :type => "", 
  :desc => ""
} 

precision

There is an option to only retreive the country information and thus excluding the city details. This results in a faster response from the service since less queries need to be done.

precision country

GeoIp.geolocation('202.108.24.0', :precision => :country)

returns:

{
  :country => "中国"
}

precision province

GeoIp.geolocation('202.108.24.0', :precision => :province)

returns:

{
  :country => "中国",
  :province => "北京"
} 

precision city

GeoIp.geolocation('202.108.24.0', :precision => :city)

returns:

{
  :country => "中国",
  :province => "北京",
  :city => "北京"

} 

Test

rspec spec/sina_geoip/sina_geoip_spec.rb

Contributors

用的是 的api 似乎漏掉字了

#1 楼 @loveky 写的是 sina,估计 markdown 排版写的有问题

需要 登录 后方可回复, 如果你还没有账号请 注册新账号