Rails 如何实现通过登录IP确定城市功能
2011-08-19 22:55
751 查看
Rails 如何实现通过登录IP确定城市功能
Rails 如何实现通过登录IP确定城市功能
博客分类:RubyOnRailsRailsRubyGitGoogleXML
对于Rails而言,主流方式应该是使用google库的插件geoip
github地址如下
Ruby代码
require 'geoip'
GeoIP.new('GeoLiteCity.dat').country('www.atlantis.sk')
=> ["www.atlantis.sk", "217.67.18.26", "SK", "SVK", "Slovakia", "EU", "02", "Bratislava", "", 48.15, 17.1167, nil, nil, "Europe/Bratislava"]
Returned values are the requested hostname, the IP address as a dotted quad,
Maxmind's country code, the ISO3166-1 country code, the ISO3166-2 country code,
the ISO3166 country name, and the continent code.
GeoIP.new('GeoCity.dat').city('github.com')
=> ["github.com", "207.97.227.239", "US", "USA", "United States", "NA", "CA", "San Francisco", "94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"]
Returned values are the country values followed by region or state name,
city name, postal_code/zipcode, latitude, longitude, USA DMA code, USA area code,
timezone name. Sorry it's not a Hash... historical.
GeoIP.new('GeoIPASNum.dat').asn("www.fsb.ru")
=> ["AS8342", "RTComm.RU Autonomous System"]
另外一个 geo_ip
使用如下:
Ruby代码
GeoIp.geolocation(ip_address)
Ruby代码
# 209.85.227.104 = google.be (US)
GeoIp.geolocation('209.85.227.104')
#returns:
{
:status =>"OK",
:ip =>"209.85.227.104"
:country_code =>"US",
:country_name =>"United States",
:region_code =>"06",
:region_name =>"California",
:city =>"Mountain View",
:zip_postal_code =>"94043",
:latitude =>"37.4192",
:longitude =>"-122.057"
}
geokit是一个关于地理的工具,比如根据经纬度确定城市和距离之类
Ruby代码
#Find near latitude and longitude:
Store.find(:all, :origin =>[37.792,-122.393], :within=>10)
#Find near an address:
Store.find(:all, :origin=>'100 Spear st, San Francisco, CA', :within=>10)
#Order by distance from the center of a zipcode:
Store.find(:all, :origin=>'94117', :within=>10,
:order=>'distance asc')
#Combine distance conditions with regular conditions
Store.find(:all, :origin=>'94117', :within=>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代码
require 'net/http'
require 'rexml/document'
include REXML
class MapsController < ApplicationController
def index
@location = locateIp()
end
def locateIp
#ip = "123.123.123.123";
ip = request.remote_ip
ips = ip.to_s
url = "http://ipinfodb.com/ip_query.php?ip="+ips+"&timezone=false"
xml_data = Net::HTTP.get_response(URI.parse(url)).body
xmldoc = REXML::Document.new(xml_data)
# Now get the root element
root = xmldoc.root
city = ""
regionName = ""
countryName = ""
# This will take country name...
xmldoc.elements.each("Response/CountryName") {
|e| countryName << e.text
}
# Now get city name...
xmldoc.elements.each("Response/City") {
|e| city << e.text
}
# This will take regionName...
xmldoc.elements.each("Response/RegionName") {
|e| regionName << e.text
}
ipLocation = city +", "+regionName+", "+countryName
return ipLocation
end #end of method locateIp
end
分享到:
mac安装rmagic | Nokogiri 中文乱码的几种情况 01:17
评论 / 浏览 (2 / 466)
分类:编程语言
相关推荐
评论 2 楼 fireflyman 2010-11-04
1 楼 qichunren 2010-08-25 支持!
相关文章推荐
- PHP如何通过AJAX方式实现登录功能
- 如何通过配置来实现IP+MAC+端口绑定功能
- 如何通过DHCP Snooping配置来实现IP+MAC+端口绑定功能
- PHP如何通过AJAX方式实现登录功能
- 如何实现Linux操作系统的自动登录功能
- C#如何实现记住密码,自动登录功能?【转载】
- Android通过SharedPreferences实现自动登录记住用户名和密码功能
- 单IP电信服务器如何实现多view功能 推荐
- 如何通过js实现图片预览功能
- Rails实现一个blog项目02-登录功能和session的实现
- Java+Selenium3框架设计篇4-如何实现登录功能放在setUp()方法中
- 通过Ip得到城市实现步骤
- 通过jquery.cookie.js实现记住用户名、密码登录功能
- UNIX下如何通过IP限制用户远程登录
- 设备中如何实现类似gopro给iphone分配ip但是不修改dns的功能
- 如何通过QQ机器人技术实现禅道bug的自动提交功能
- 通过如何通过js实现复制粘贴功能
- Python爬虫通过替换http request header来欺骗浏览器实现登录功能
- 通过IP获取对应城市信息(该功能基于淘宝第三方IP库接口)
- 如何最大限度地实现安全登录功能?