<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2450465518865184850</id><updated>2011-07-29T00:07:34.003-03:00</updated><title type='text'>Mea Docta Ignorantia</title><subtitle type='html'>Yo no quería tener un blog :P</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mea-docta-ignorantia.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2450465518865184850/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://mea-docta-ignorantia.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Franco</name><uri>http://www.blogger.com/profile/08119717172848741330</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2450465518865184850.post-3236818411256954707</id><published>2009-03-25T10:16:00.029-03:00</published><updated>2009-03-26T12:16:01.644-03:00</updated><title type='text'>How to know a user location with Rails</title><content type='html'>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.&lt;br /&gt;&lt;br /&gt;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:&lt;br /&gt;&lt;pre class="source-code"&gt;&lt;code&gt;@client_ip = request.remote_ip&lt;/code&gt;&lt;/pre&gt;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:&lt;br /&gt;&lt;pre class="source-code"&gt;&lt;code&gt;@client_ip = (RAILS_ENV == 'development') ? '201.231.22.125' : request.remote_ip&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;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 &lt;a href="http://www.maxmind.com/"&gt;MaxMind&lt;/a&gt;, they offer geolocation databases, the free edition (GeoIpLite) is less accurate than the commercial version but it was enough for me, &lt;a href="http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"&gt;here&lt;/a&gt; is the link to the free binary database, I decompress the database in /opt/GeoIP/share/GeoIP/.&lt;br /&gt;&lt;br /&gt;To work with the above database you have to install the C library &lt;a href="http://www.maxmind.com/app/c"&gt;geoip&lt;/a&gt;, I did this from the source but you can get it from synaptic, these are the commands that I used:&lt;br /&gt;&lt;pre class="source-code"&gt;&lt;code&gt;&lt;br /&gt;wget http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz&lt;br /&gt;tar -zxvf GeoIP.tar.gz&lt;br /&gt;cd GeoIP&lt;br /&gt;./configure --prefix=/opt/GeoIP&lt;br /&gt;make &amp;amp;&amp;amp; sudo make install&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;And to work from Ruby with this library I use &lt;a href="http://rubyforge.org/projects/geoip-city/"&gt;geoip_city&lt;/a&gt; gem, to install it I used:&lt;br /&gt;&lt;pre class="source-code"&gt;&lt;code&gt;&lt;br /&gt;sudo gem install geoip_city -- --with-geoip-dir=/opt/GeoIP&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;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:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;script/console&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  &gt;&gt; require 'geoip_city'&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  &gt;&gt; g = GeoIPCity::Database.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat')&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  &gt;&gt; res = g.look_up('201.231.22.125')&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  =&gt; {:latitude=&gt;-33.13330078125, :country_code3=&gt;"ARG", :longitude=&gt;-64.3499984741211, :city=&gt;"Río Cuarto", :country_name=&gt;"Argentina", :country_code=&gt;"AR", :region=&gt;"05"}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you get something like this you have everything working, but when I did my first test I got the following error message:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;  "libgeoip.so.1: cannot open shared object file: No such file or directory"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;I solved it running:  &lt;span style="font-family:courier new;"&gt;sudo &lt;span style="font-size:85%;"&gt;ldconfig /etc/ld.so.conf&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Once that we have everything working we can build our country method oracle, I did it in the application.rb file:&lt;br /&gt;&lt;pre class="source-code"&gt;&lt;code&gt;&lt;br /&gt;def get_country&lt;br /&gt;@client_ip = request.remote_ip @client_ip = '201.231.22.125'&lt;br /&gt;&lt;br /&gt;if session[:country].blank?&lt;br /&gt; g = GeoIPCity::Database.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat')&lt;br /&gt; @user_location = g.look_up(@client_ip)&lt;br /&gt;&lt;br /&gt; # if there isn't a country_code then the default country is USA&lt;br /&gt; session[:country] = @user_location[:country_code] || 'US'&lt;br /&gt;end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;and "voila" 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.&lt;br /&gt;&lt;br /&gt;Well this was my first post, so I hope that this will be useful for somebody.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2450465518865184850-3236818411256954707?l=mea-docta-ignorantia.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mea-docta-ignorantia.blogspot.com/feeds/3236818411256954707/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://mea-docta-ignorantia.blogspot.com/2009/03/how-to-know-user-location-with-rails.html#comment-form' title='1 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2450465518865184850/posts/default/3236818411256954707'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2450465518865184850/posts/default/3236818411256954707'/><link rel='alternate' type='text/html' href='http://mea-docta-ignorantia.blogspot.com/2009/03/how-to-know-user-location-with-rails.html' title='How to know a user location with Rails'/><author><name>Franco</name><uri>http://www.blogger.com/profile/08119717172848741330</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2450465518865184850.post-7540448168947488777</id><published>2009-03-16T16:20:00.000-03:00</published><updated>2009-03-17T15:45:32.612-03:00</updated><title type='text'>Nombre del blog y presentación :)</title><content type='html'>Para lo que estan un poco vagos para buscar por internet y el nivel de latín es como el mio les comento que el significado de "&lt;span style="font-style: italic;"&gt;Mea Docta Ignorantia&lt;/span&gt;" sería algo como "&lt;span style="font-style: italic;"&gt;Mi Sabia Ignorancia&lt;/span&gt;".&lt;br /&gt;&lt;br /&gt;El objetivo de este blog es escribir textos técnicos sobre programación que espero le sean utiles a algún trasnochado en problemas, aunque creo que la principal utilidad del blog será un estilo de colección de mis propios "how to".pluralize().&lt;br /&gt;&lt;br /&gt;Últimamente he trabajado bastante con &lt;a href="http://rubyonrails.org/"&gt;Ruby On Rails&lt;/a&gt; así que supongo que será uno de los principales temas del blog.&lt;br /&gt;&lt;br /&gt;Bueno, espero poder comenzar pronto y que le sea útil a alguien, saludos.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2450465518865184850-7540448168947488777?l=mea-docta-ignorantia.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mea-docta-ignorantia.blogspot.com/feeds/7540448168947488777/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://mea-docta-ignorantia.blogspot.com/2009/03/nombre-del-blog.html#comment-form' title='2 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2450465518865184850/posts/default/7540448168947488777'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2450465518865184850/posts/default/7540448168947488777'/><link rel='alternate' type='text/html' href='http://mea-docta-ignorantia.blogspot.com/2009/03/nombre-del-blog.html' title='Nombre del blog y presentación :)'/><author><name>Franco</name><uri>http://www.blogger.com/profile/08119717172848741330</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>
