浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Rails 如何实现通过登录IP确定城市功能


Rails 如何实现通过登录IP确定城市功能



对于Rails而言,主流方式应该是使用google库的插件geoip
github地址如下

Ruby代码  收藏代码
  1. require 'geoip'  
  2. GeoIP.new('GeoLiteCity.dat').country('www.atlantis.sk')  
  3. => ["www.atlantis.sk""217.67.18.26""SK""SVK""Slovakia""EU""02""Bratislava""", 48.15, 17.1167, nilnil"Europe/Bratislava"]  
  4.   
  5. Returned values are the requested hostname, the IP address as a dotted quad,  
  6. Maxmind's country code, the ISO3166-1 country code, the ISO3166-2 country code,  
  7. the ISO3166 country name, and the continent code.  
  8.   
  9. GeoIP.new('GeoCity.dat').city('github.com')  
  10. => ["github.com""207.97.227.239""US""USA""United States""NA""CA""San Francisco""94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"]  
  11.   
  12. Returned values are the country values followed by region or state name,  
  13. city name, postal_code/zipcode, latitude, longitude, USA DMA code, USA area code,  
  14. timezone name. Sorry it's not a Hash... historical.  
  15.   
  16. GeoIP.new('GeoIPASNum.dat').asn("www.fsb.ru")  
  17. => ["AS8342""RTComm.RU Autonomous System"]  


另外一个 geo_ip

使用如下:
Ruby代码  收藏代码
  1. GeoIp.geolocation(ip_address)  


Ruby代码  收藏代码
  1.   # 209.85.227.104 = google.be (US)  
  2.   GeoIp.geolocation('209.85.227.104')  
  3. #returns:  
  4.   
  5.   {  
  6.     :status           =>"OK",  
  7.     :ip               =>"209.85.227.104"  
  8.     :country_code     =>"US",  
  9.     :country_name     =>"United States",  
  10.     :region_code      =>"06",  
  11.     :region_name      =>"California",  
  12.     :city             =>"Mountain View",  
  13.     :zip_postal_code  =>"94043",  
  14.     :latitude         =>"37.4192",  
  15.     :longitude        =>"-122.057"  
  16.   }  


geokit是一个关于地理的工具,比如根据经纬度确定城市和距离之类

Ruby代码  收藏代码
  1. #Find near latitude and longitude:  
  2. Store.find(:all:origin =>[37.792,-122.393], :within=>10)  
  3. #Find near an address:  
  4. Store.find(:all:origin=>'100 Spear st, San Francisco, CA':within=>10)  
  5. #Order by distance from the center of a zipcode:  
  6. Store.find(:all:origin=>'94117':within=>10,  
  7.            :order=>'distance asc')  
  8. #Combine distance conditions with regular conditions  
  9. Store.find(:all:origin=>'94117':within=>10,   
  10.            :conditions=>{:store_type=>'CAFE'})  



一个是通过网络的IP查询API,这个办法IP库更新比较快。通用的库有几个比如google。
xml处理页面完全可以通过nokogiri等专门处理工具代替

提供IP地址查询的API很多比如网易
http://www.youdao.com/smartresult-xml/search.s?type=ip&q=IP地址
Ruby代码  收藏代码
  1. require 'net/http'  
  2. require 'rexml/document'  
  3. include REXML  
  4.   
  5. class MapsController < ApplicationController  
  6.     def index  
  7.         @location = locateIp()  
  8.         end  
  9.   
  10.     def locateIp  
  11.         #ip = "123.123.123.123";  
  12.         ip = request.remote_ip  
  13.         ips = ip.to_s  
  14.         url = "http://ipinfodb.com/ip_query.php?ip="+ips+"&timezone=false"  
  15.   
  16.         xml_data = Net::HTTP.get_response(URI.parse(url)).body  
  17.   
  18.                 xmldoc = REXML::Document.new(xml_data)  
  19.   
  20.         # Now get the root element  
  21.         root = xmldoc.root  
  22.         city = ""  
  23.         regionName = ""  
  24.         countryName = ""  
  25.   
  26.         # This will take country name...  
  27.         xmldoc.elements.each("Response/CountryName") {  
  28.         |e| countryName << e.text  
  29.              }  
  30.   
  31.         # Now get city name...  
  32.         xmldoc.elements.each("Response/City") {  
  33.         |e| city << e.text  
  34.             }  
  35.   
  36.         # This will take regionName...  
  37.         xmldoc.elements.each("Response/RegionName") {  
  38.         |e| regionName << e.text  
  39.             }  
  40.   
  41.                ipLocation = city +", "+regionName+", "+countryName  
  42.   
  43.                return ipLocation  
  44.   
  45.         end  #end of method locateIp  
  46.   
  47. end  
分享到:
评论
2 楼 fireflyman 2010-11-04  
1 楼 qichunren 2010-08-25  
支持! 



posted on 2011-08-19 22:55  lexus  阅读(392)  评论(0编辑  收藏  举报