<?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>Ruby, Rails, OSX and Linux fun &#187; ruby</title>
	<atom:link href="http://www.frederico-araujo.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frederico-araujo.com</link>
	<description>Ruby, Rails, OSX and linux sysadmin</description>
	<lastBuildDate>Thu, 02 Sep 2010 08:13:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Really checking the content-type/mime_type of a file in OSX and Linux</title>
		<link>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/</link>
		<comments>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 18:55:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[osx]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[content_type]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mime_type]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.frederico-araujo.com/?p=95</guid>
		<description><![CDATA[I have came across many projects where checking file uploads and content-type (mime-type) is poorly implemented or heavy in resource.
Methods I have seen so far:
1. Checking content-type from file name: this inefficient, a user can just rename a file and you are fooled, or the file can have a different file format and you will [...]]]></description>
			<content:encoded><![CDATA[<p>I have came across many projects where checking file uploads and content-type (mime-type) is poorly implemented or heavy in resource.</p>
<p>Methods I have seen so far:</p>
<p>1. Checking content-type from file name: this inefficient, a user can just rename a file and you are fooled, or the file can have a different file format and you will not get the expected result.</p>
<p>2. Using Rmagick to check if the file is an image. This is so slow and uses so much Ram. You can try to initialize an rmagick object from an image file, then rescue when the file is not an image.</p>
<p>3. Using mini_magick to check if a file. This method is faster than rmagick. Implemen ted same way as rmagick.</p>
<p>A Better method for OSX and Linux,  is to use the command line tool &#8220;file&#8221; included in most UNIX operating systems.</p>
<p>It is very fast and very accurate.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">file = <span style="color:#996600;">&quot;/path/to/file.ext&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> RUBY_PLATFORM.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>darwin<span style="color:#006600; font-weight:bold;">|</span>linux<span style="color:#006600; font-weight:bold;">|</span>unix<span style="color:#006600; font-weight:bold;">|</span>solaris<span style="color:#006600; font-weight:bold;">|</span>bsd<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
 content_type = <span style="color:#996600;">`file --raw --brief &quot;#{file}&quot;`</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>
 <span style="color:#9966CC; font-weight:bold;">case</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>image<span style="color:#006600; font-weight:bold;">|</span>png<span style="color:#006600; font-weight:bold;">|</span>jpg<span style="color:#006600; font-weight:bold;">|</span>jpeg<span style="color:#006600; font-weight:bold;">|</span>gif<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;image&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>pdf<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;pdf&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Microsoft Word|Microsoft Office Document&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;doc&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#008000; font-style:italic;"># This can go on and on</span>
   real_type = <span style="color:#996600;">&quot;Unknown&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Some examples of content types:</p>
<p>.doc =  Microsoft Word document data</p>
<p>.doc = Microsoft Office Document</p>
<p>.pdf = PDF document, version 1.4</p>
<p>.pdf = PDF document, version 1.3</p>
<p>.psd = Adobe Photoshop Image</p>
<p>.png  = PNG image data, 3508 x 4961, 8-bit/color RGBA, non-interlaced</p>
<p>.gif = GIF image data, version 89a, 195 x 109</p>
<p>.jpg = JPEG image data, EXIF standard</p>
<p>etc&#8230;</p>
<p>I hope this can be useful to someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A little Haml tutorial on how to render different formats</title>
		<link>http://www.frederico-araujo.com/2008/08/19/a-little-haml-tutorial-on-how-to-render-different-formats/</link>
		<comments>http://www.frederico-araujo.com/2008/08/19/a-little-haml-tutorial-on-how-to-render-different-formats/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 17:00:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[haml]]></category>

		<guid isPermaLink="false">http://wp.frederico-araujo.com/?p=8</guid>
		<description><![CDATA[Suppose you have a Model called Article that contains a text field and a format field.
You would like to use haml, textile or HTML to edit your Article from the admin interface.

  create_table &#34;articles&#34;, :force =&#62; true do &#124;t&#124;
    t.string   &#34;title&#34;
    t.text     [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you have a Model called Article that contains a text field and a format field.</p>
<p>You would like to use haml, textile or HTML to edit your Article from the admin interface.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  create_table <span style="color:#996600;">&quot;articles&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
    t.<span style="color:#CC0066; font-weight:bold;">string</span>   <span style="color:#996600;">&quot;title&quot;</span>
    t.<span style="color:#9900CC;">text</span>     <span style="color:#996600;">&quot;body&quot;</span>
    t.<span style="color:#CC0066; font-weight:bold;">string</span>   <span style="color:#996600;">&quot;formatting_type&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;HTML&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>app/models/article.rb</p>
<p>It’s quite simple. All you have to do is to add this helper in your application_helper.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> print_formated<span style="color:#006600; font-weight:bold;">&#40;</span>type,text<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> type
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;HTML&quot;</span>
      text
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;Plain Text&quot;</span>
      h text
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;HAML&quot;</span>
      <span style="color:#6666ff; font-weight:bold;">Haml::Engine</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">render</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;Syntaxy&quot;</span>
      Syntaxi.<span style="color:#9900CC;">line_number_method</span> = <span style="color:#996600;">'none'</span>
      Syntaxi.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">process</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;Textile&quot;</span>
      RedCloth.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_html</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In your views/articles/_form.haml add the select field.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  = label <span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:formatting_type</span>
  = <span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:formatting_type</span>, <span style="color:#6666ff; font-weight:bold;">Article::FORMATTING_TYPES</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:include_blank</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Then in the Show view (articles/show.haml)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
.<span style="color:#9900CC;">article</span>
  .<span style="color:#9900CC;">title</span>
    <span style="color:#006600; font-weight:bold;">%</span>h1
      = h <span style="color:#0066ff; font-weight:bold;">@article</span>.<span style="color:#9900CC;">title</span>
  .<span style="color:#9900CC;">body</span>
    = print_formated<span style="color:#006600; font-weight:bold;">&#40;</span>@article.<span style="color:#9900CC;">formatting_type</span>, <span style="color:#0066ff; font-weight:bold;">@article</span>.<span style="color:#9900CC;">body</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>that’s pretty much it.<br />
 <img src='http://www.frederico-araujo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederico-araujo.com/2008/08/19/a-little-haml-tutorial-on-how-to-render-different-formats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ruby-mysql now Ruby 1.9 compatible</title>
		<link>http://www.frederico-araujo.com/2008/03/07/ruby-mysql-now-ruby-19-compatible/</link>
		<comments>http://www.frederico-araujo.com/2008/03/07/ruby-mysql-now-ruby-19-compatible/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 05:16:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.frederico-araujo.com/?p=37</guid>
		<description><![CDATA[Tommy has just released an new mysql-ruby package.
Actually 2 of them:
mysql-ruby-2.7.5 and mysql-ruby-2.8pre2
They are Ruby 1.9 compatible
Requirements
* MySQL 5.0.51a
* Ruby 1.8.6, 1.9.0
here is the link http://tmtm.org/en/mysql/ruby/
Great Job
]]></description>
			<content:encoded><![CDATA[<p>Tommy has just released an new mysql-ruby package.</p>
<p>Actually 2 of them:</p>
<p>mysql-ruby-2.7.5 and mysql-ruby-2.8pre2</p>
<p>They are Ruby 1.9 compatible<br />
Requirements</p>
<p>* MySQL 5.0.51a<br />
* Ruby 1.8.6, 1.9.0</p>
<p>here is the link http://tmtm.org/en/mysql/ruby/</p>
<p>Great Job</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederico-araujo.com/2008/03/07/ruby-mysql-now-ruby-19-compatible/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
