<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Samson&#039;s Weblog &#187; geo</title>
	<atom:link href="http://blog.samsonis.me/tag/geo/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.samsonis.me</link>
	<description>天与弗取，反受其咎；时至不行，反受其殃</description>
	<lastBuildDate>Sun, 29 Jan 2012 13:53:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Ruby gem geoip and Rack::GeoIPCountry</title>
		<link>http://blog.samsonis.me/2010/02/ruby-gem-geoip-and-rack-geoipcountry/</link>
		<comments>http://blog.samsonis.me/2010/02/ruby-gem-geoip-and-rack-geoipcountry/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 08:51:44 +0000</pubDate>
		<dc:creator>Samson Wu</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[geo]]></category>
		<category><![CDATA[Rack]]></category>

		<guid isPermaLink="false">http://blog.samsonis.me/?p=1733</guid>
		<description><![CDATA[wikipedia上对于geolocation的解释是这样的： Geolocation is the identification of the real-world geographic location of an Internet-connected computer, mobile device, website visitor or other. 检测的方法貌似也有很多： Geolocation can be performed by associating a geographic location with the Internet Protocol (IP) address, MAC address, RFID, hardware embedded article/production number, embedded software number (such as UUID, Exif/IPTC/XMP or modern steganography), invoice, Wi-Fi ...]]></description>
			<content:encoded><![CDATA[<p>wikipedia上对于<a href="http://en.wikipedia.org/wiki/Geolocation">geolocation</a>的解释是这样的：</p>
<blockquote><p>
Geolocation is the identification of the real-world geographic location of an Internet-connected computer, mobile device, website visitor or other.
</p></blockquote>
<p>检测的方法貌似也有很多：</p>
<blockquote><p>Geolocation can be performed by associating a geographic location with the Internet Protocol (IP) address, MAC address, RFID, hardware embedded article/production number, embedded software number (such as UUID, Exif/IPTC/XMP or modern steganography), invoice, Wi-Fi  connection location, or device GPS  coordinates, or other, perhaps self-disclosed information.</p></blockquote>
<p>（班门弄斧一下先，呵呵呵呵）</p>
<p>ruby里面取到geo信息的一个相对方便的办法是使用rubygem <a href="http://geoip.rubyforge.org/">geoip</a>，虽然本质上还是使用<a href="http://www.maxmind.com/">maxmind</a>的data，但是免去了编译和安装其c library的麻烦。<br />
<span id="more-1733"></span><br />
安装gem并下载其depend到的geoip database：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> geoip</div></div>
<div class="codecolorer-container bash railscasts" style="border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>geolite.maxmind.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>geoip<span style="color: #000000; font-weight: bold;">/</span>database<span style="color: #000000; font-weight: bold;">/</span>GeoLiteCountry<span style="color: #000000; font-weight: bold;">/</span>GeoIP.dat.gz <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">gunzip</span> GeoIP.dat.gz</div></div>
<p>如果想要城市信息，还可以下载稍微大一点的带city info的free database：</p>
<div class="codecolorer-container bash railscasts" style="border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>geolite.maxmind.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>geoip<span style="color: #000000; font-weight: bold;">/</span>database<span style="color: #000000; font-weight: bold;">/</span>GeoLiteCity.dat.gz <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">gunzip</span> GeoLiteCity.dat.gz</div></div>
<p>简单的测试一下：</p>
<div class="codecolorer-container ruby railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'geoip'</span><br />
<br />
<span style="color:#CC0066; font-weight:bold;">p</span> GeoIP.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'GeoIP.dat'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">country</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;google.com&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#CC0066; font-weight:bold;">p</span> GeoIP.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'GeoLiteCity.dat'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">city</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;baidu.com&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<p>alright, looks good:</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">&quot;google.com&quot;</span>, <span style="color: #ff0000;">&quot;66.249.91.104&quot;</span>, <span style="color: #000000;">225</span>, <span style="color: #ff0000;">&quot;US&quot;</span>, <span style="color: #ff0000;">&quot;USA&quot;</span>, <span style="color: #ff0000;">&quot;United States&quot;</span>, <span style="color: #ff0000;">&quot;NA&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">&quot;baidu.com&quot;</span>, <span style="color: #ff0000;">&quot;220.181.6.81&quot;</span>, <span style="color: #ff0000;">&quot;CN&quot;</span>, <span style="color: #ff0000;">&quot;CHN&quot;</span>, <span style="color: #ff0000;">&quot;China&quot;</span>, <span style="color: #ff0000;">&quot;AS&quot;</span>, <span style="color: #ff0000;">&quot;22&quot;</span>, <span style="color: #ff0000;">&quot;Beijing&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span>, <span style="color: #000000;">39.9289</span>, <span style="color: #000000;">116.3883</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></div></div>
<p>由于geo的普遍性，把其做成可以plug&#038;play的Rack middleware绝对是个好主意，这就是接下来要介绍的<a href="http://coderack.org/users/hosiawak/entries/36-geoip-country">Rack::GeoIPCountry</a>，<a href="http://coderack.org/">CodeRack Contest</a>的first place。</p>
<p>其原理就是调用geoip gem获得访问IP的geo信息并写入request header，这样后面的app或者middleware就能读到并处理了。</p>
<p>同样写个简单的middleware测试一下：</p>
<div class="codecolorer-container ruby railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'geo_ip_country'</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> GeoTest<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@app</span> = app<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Remote Address: #{env['REMOTE_ADDR']}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;GeoIP Country ID: #{env['X_GEOIP_COUNTRY_ID']}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;GeoIP Country Code: #{env['X_GEOIP_COUNTRY_CODE']}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;GeoIP Country Code3: #{env['X_GEOIP_COUNTRY_CODE3']}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;GeoIP Country: #{env['X_GEOIP_COUNTRY']}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;GeoIP Continent: #{env['X_GEOIP_CONTINENT']}&quot;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@app</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
use <span style="color:#6666ff; font-weight:bold;">Rack::ContentLength</span><br />
use <span style="color:#6666ff; font-weight:bold;">Rack::GeoIPCountry</span><br />
use GeoTest<br />
<br />
run <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>env<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">200</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'Content-Type'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'text/html'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'OK'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div></div>
<p>rackup起来后，访问http://localhost:9292，确认console下输出了我们想要的geo信息：</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Remote Address: 127.0.0.1<br />
GeoIP Country ID: <span style="color: #000000;">0</span><br />
GeoIP Country Code: <span style="color: #660033;">--</span><br />
GeoIP Country Code3: <span style="color: #660033;">--</span><br />
GeoIP Country: N<span style="color: #000000; font-weight: bold;">/</span>A<br />
GeoIP Continent: <span style="color: #660033;">--</span><br />
127.0.0.1 - - <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">25</span><span style="color: #000000; font-weight: bold;">/</span>Feb<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2010</span> <span style="color: #000000;">16</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">11</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #ff0000;">&quot;GET / HTTP/1.1&quot;</span> <span style="color: #000000;">200</span> <span style="color: #000000;">2</span> <span style="color: #000000;">0.0016</span></div></div>
<p>当然上面的127.0.0.1是不会有任何信息的，local network也是不会在database里面找到对应的，只有当真正的外网ip hit到我们的server时，我们才能看到有意义的geo信息。</p>

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://blog.samsonis.me/2010/02/rubys-python-simplehttpserver/" title="ruby&#8217;s &#8220;python -m SimpleHTTPServer&#8221; (February 21, 2010)">ruby&#8217;s &#8220;python -m SimpleHTTPServer&#8221;</a> (2)</li>
	<li><a href="http://blog.samsonis.me/2011/02/rubygems-1-5-2-manual-upgrade/" title="RubyGems 1.5.2 Manual Upgrade (February 16, 2011)">RubyGems 1.5.2 Manual Upgrade</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2010/02/list-performance-ruby-1-8-and-1-9-part-1/" title="List performance: ruby 1.8 and 1.9 (Part 1) (February 22, 2010)">List performance: ruby 1.8 and 1.9 (Part 1)</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/12/downgrade-rubygems/" title="Downgrade Rubygems (December 5, 2011)">Downgrade Rubygems</a> (0)</li>
	<li><a href="http://blog.samsonis.me/2011/03/archlinux-rvm-ruby-rails-nginx-passenger-and-you/" title="ArchLinux, RVM, Ruby, Rails, Nginx, Passenger and You (March 30, 2011)">ArchLinux, RVM, Ruby, Rails, Nginx, Passenger and You</a> (3)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://blog.samsonis.me/2010/02/ruby-gem-geoip-and-rack-geoipcountry/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

