<?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>Mundo Ruby &#187; GeoLocation</title>
	<atom:link href="http://www.mundoruby.com.ar/tag/geolocation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mundoruby.com.ar</link>
	<description>Ruby Artists, Hackers y otras yerbas ...</description>
	<lastBuildDate>Wed, 12 Aug 2009 23:02:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to know a user location with Rails</title>
		<link>http://www.mundoruby.com.ar/2009/03/26/how-to-know-a-user-location-with-rails/</link>
		<comments>http://www.mundoruby.com.ar/2009/03/26/how-to-know-a-user-location-with-rails/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 20:02:42 +0000</pubDate>
		<dc:creator>FreedomCoder</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[GeoLocation]]></category>

		<guid isPermaLink="false">http://www.mundoruby.com.ar/?p=60</guid>
		<description><![CDATA[Knowing the user location is useful not only for statics, but also, for example, to avoid that the same ip address votes twice or to allow that your site shows products dynamically in relation with the place where the user is and a lot of other applications.
The first task is to know the ip address [...]]]></description>
			<content:encoded><![CDATA[<p>Knowing the user location is useful not only for statics, but also, for example, to avoid that the same ip address votes twice or to allow that your site shows products dynamically in relation with the place where the user is and a lot of other applications.</p>
<p>The first task is to know the ip address of the user, to do this we can use a very helpful method of rails called remote_ip, this function is very smart, looks for in the headers HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR and REMOTE_ADDR and parse the value to figure out the ip address. So if we want to know the ip address we can use this:</p>
<pre class="source-code"><code>@client_ip = request.remote_ip</code></pre>
<p>If you are working in your local machine the remote_ip will be 127.0.0.1 and this address cannot be processed, then you have to write the following, instead the above code:</p>
<pre class="source-code"><code>@client_ip = (RAILS_ENV == 'development') ? '201.231.22.125' : request.remote_ip</code></pre>
<p>Once we have the ip address we have to figure out the place where the ip address is from, to do this we are going to use a free database of <a href="http://www.maxmind.com/">MaxMind</a>, they offer geolocation databases, the free edition (GeoIpLite) is less accurate than the commercial version but it was enough for me, <a href="http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz">here</a> is the link to the free binary database, I decompress the database in /opt/GeoIP/share/GeoIP/.</p>
<p>To work with the above database you have to install the C library <a href="http://www.maxmind.com/app/c">geoip</a>, I did this from the source but you can get it from synaptic, these are the commands that I used:</p>
<pre class="source-code"><code>wget http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gztar -zxvf GeoIP.tar.gzcd GeoIP./configure --prefix=/opt/GeoIPmake &amp;&amp; sudo make install</code></pre>
<p>And to work from Ruby with this library I use <a href="http://rubyforge.org/projects/geoip-city/">geoip_city</a> gem, to install it I used:</p>
<pre class="source-code"><code>sudo gem install geoip_city -- --with-geoip-dir=/opt/GeoIP</code></pre>
<p>Well we have everything installed, now we can test it from the Rails side, in the root project open a console and run the following:</p>
<p><span style="font-size:85%;"><span style="font-family:courier new;">script/console</span><br />
<span style="font-family:courier new;"> &gt;&gt; require &#8216;geoip_city&#8217;</span><br />
<span style="font-family:courier new;"> &gt;&gt; g = GeoIPCity::Database.new(&#8217;/opt/GeoIP/share/GeoIP/GeoLiteCity.dat&#8217;)</span><br />
<span style="font-family:courier new;"> &gt;&gt; res = g.look_up(&#8217;201.231.22.125&#8242;)</span><br />
<span style="font-family:courier new;"> =&gt; {:latitude=&gt;-33.13330078125, :country_code3=&gt;&#8221;ARG&#8221;, :longitude=&gt;-64.3499984741211, :city=&gt;&#8221;Río Cuarto&#8221;, :country_name=&gt;&#8221;Argentina&#8221;, :country_code=&gt;&#8221;AR&#8221;, :region=&gt;&#8221;05&#8243;}</span></span></p>
<p>If you get something like this you have everything working, but when I did my first test I got the following error message:<br />
<span style="font-size:85%;"><span style="font-family:courier new;"> &#8220;libgeoip.so.1: cannot open shared object file: No such file or directory&#8221;</span></span><br />
I solved it running:  <span style="font-family:courier new;">sudo <span style="font-size:85%;">ldconfig /etc/ld.so.conf</span></span></p>
<p>Once that we have everything working we can build our country method oracle, I did it in the application.rb file:</p>
<p><code>def get_country<br />
@client_ip = request.remote_ip @client_ip = '201.231.22.125'</p>
<p>if session[:country].blank?<br />
g = GeoIPCity::Database.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat')<br />
@user_location = g.look_up(@client_ip)</p>
<p> # if there isn't a country_code then the default country is USA<br />
session[:country] = @user_location[:country_code] || 'US'<br />
end<br />
end</code></p>
<p>and &#8220;voila&#8221; you have in all the views and controllers the variable session[:country] that contains the current country location of the user, if you want you can be more accurate and ask for cities, lats, longs but I  just neded the user country.</p>
<p>Well this was my first post, so I hope that this will be useful for somebody.</p>
<div class="blogger-post-footer"><img src="http://res1.blogblog.com/tracker/2450465518865184850-3236818411256954707?l=mea-docta-ignorantia.blogspot.com" alt="" width="1" height="1" /></div>
<p>(Via <a href="http://mea-docta-ignorantia.blogspot.com/">Docta Ignorantia</a>.) Original Link: <a href="http://mea-docta-ignorantia.blogspot.com/2009/03/how-to-know-user-location-with-rails.html">How to know a user location with Rails</a></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7949681675937032";
google_ad_slot = "0874687580";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mundoruby.com.ar/2009/03/26/how-to-know-a-user-location-with-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

