<?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>Manas</title>
	<atom:link href="http://www.manas.com.ar/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.manas.com.ar</link>
	<description>Thinking code</description>
	<lastBuildDate>Mon, 06 May 2013 20:30:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tesis en Ciencias de la Computación: Reconocedor de Dígitos (spike)</title>
		<link>http://www.manas.com.ar/pbrusco/2013/05/06/htk-digit-recognizer/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=htk-digit-recognizer</link>
		<comments>http://www.manas.com.ar/pbrusco/2013/05/06/htk-digit-recognizer/#comments</comments>
		<pubDate>Mon, 06 May 2013 18:39:27 +0000</pubDate>
		<dc:creator>Pablo Brusco</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[castellano]]></category>
		<category><![CDATA[español]]></category>
		<category><![CDATA[htk]]></category>
		<category><![CDATA[machine learning]]></category>
		<category><![CDATA[reconocimiento de dígitos]]></category>
		<category><![CDATA[Speech Recognition]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/?p=719</guid>
		<description><![CDATA[This post is in Spanish as it refers to a tutorial on speech recognition written in Spanish in the context of a master thesis in Computer Science at FCEN-UBA. Nevertheless, the code and its documentation is in English, so feel free to check it out in this repository Ahora si, en español. Como ya les comenté en &#8230; <a href="http://www.manas.com.ar/pbrusco/2013/05/06/htk-digit-recognizer/">more</a>]]></description>
			<content:encoded><![CDATA[<p>This post is in Spanish as it refers to a tutorial on speech recognition written in Spanish in the context of a master thesis in <a href="http://dc.uba.ar/">Computer Science at FCEN-UBA</a>. Nevertheless, the code and its documentation is in English, so feel free to check it out in <a title="bitbucket repo" href="https://bitbucket.org/pbrusco/tesis-proyectos/src/master/Spikes/digitos?at=master" target="_blank">this repository</a></p>
<p>Ahora si, en español.</p>
<p>Como ya les comenté en un <a title="post-tesis" href="http://www.manas.com.ar/pbrusco/2013/04/03/master-thesis-in-computer-science-speech-recognition-for-farm-radios/" target="_blank">post anterior</a>, estoy trabajando en mi tesis, un reconocedor de keywords para programas radiales. Por ser una tarea difícil que, entre otras necesidades, incluye el aprendizaje de la herramienta <a title="HTK webpage" href="http://htk.eng.cam.ac.uk" target="_blank">HTK</a>, me dispuse a realizar unas pruebas generando un reconocedor de dígitos en español para lograr entender la herramienta.</p>
<p>Al hacerlo, encontré infinidad de dificultades a la hora de modelar e implementar este sistema, y es por eso que escribí el siguiente tutorial: <a title="man.as/htk-tutorial" href="http://man.as/htk-tutorial" target="_blank">Reconocimiento de Dígitos en Castellano utilizando HTK</a>.</p>
<p>Los resultados no son perfectos pero son muy buenos para la poca cantidad de datos de entrenamiento que utilice. Así que los desafío a mejorar la implementación y, por qué no, a implementar nuevos sistemas con la ayuda de este tutorial.</p>
<p>Espero que les guste!</p>
<p>&nbsp;<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/pbrusco/2013/05/06/htk-digit-recognizer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Master Thesis in Computer Science: Speech recognition for Farm Radios</title>
		<link>http://www.manas.com.ar/pbrusco/2013/04/03/master-thesis-in-computer-science-speech-recognition-for-farm-radios/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=master-thesis-in-computer-science-speech-recognition-for-farm-radios</link>
		<comments>http://www.manas.com.ar/pbrusco/2013/04/03/master-thesis-in-computer-science-speech-recognition-for-farm-radios/#comments</comments>
		<pubDate>Wed, 03 Apr 2013 18:23:40 +0000</pubDate>
		<dc:creator>Pablo Brusco</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[Farm Radios]]></category>
		<category><![CDATA[Investigación]]></category>
		<category><![CDATA[Keyword Spotting]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Speech Recognition]]></category>
		<category><![CDATA[Tesis]]></category>
		<category><![CDATA[Thesis]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/?p=716</guid>
		<description><![CDATA[&#160; Last month I started working on my master thesis in the context of Manas Research. The idea for this research is to contribute with Farm Radio International (an organization that works with many partners in Africa to deliver effective programs to serve smallholder farmers through radio) by helping them with the problem of indexing &#8230; <a href="http://www.manas.com.ar/pbrusco/2013/04/03/master-thesis-in-computer-science-speech-recognition-for-farm-radios/">more</a>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>Last month I started working on my master thesis in the context of Manas Research. The idea for this research is to contribute with <a title="Farm Radio International" href="http://www.farmradio.org" target="_blank">Farm Radio International</a> (an organization that works with many partners in Africa to deliver effective programs to serve smallholder farmers through radio) by helping them with the problem of indexing content using speech recognition.</p>
<p dir="ltr">We plan on solving this problem by using a technique called <a title="Keyword Spotting" href="http://en.wikipedia.org/wiki/Keyword_spotting" target="_blank">Keyword Spotting</a> which is used when the program has to recognize utterances of words in large amounts of recorded data.</p>
<p dir="ltr">One could say that for this task, we could use a general-purpose speech recognition system (based on <a title="Hidden Markov Models" href="http://en.wikipedia.org/wiki/Hidden_Markov_model" target="_blank">Hidden Markov Models</a> or similar techniques),  but the first constraint we are dealing with consists of thinking that the target language does not have resources for speech processing (as do Spanish or English where a lot of research and software have been developed) since the radio programs will be spoken in less studied languages such as Swahilli.</p>
<p dir="ltr">Therefore, our research will be based on the search of other kind of methods to face the problem.</p>
<p dir="ltr">Hopefully, this research will show us that good results can be achieved in spite of the constraints, and then, we will be able to implement full systems using our studies and algorithms.</p>
<p dir="ltr">Keep you informed <img src='http://www.manas.com.ar/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/pbrusco/2013/04/03/master-thesis-in-computer-science-speech-recognition-for-farm-radios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extracting Subject Alternative Name from Microsoft authentication client certificates</title>
		<link>http://www.manas.com.ar/spalladino/2013/01/29/extracting-subject-alternative-name-from-microsoft-authentication-client-certificates/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=extracting-subject-alternative-name-from-microsoft-authentication-client-certificates</link>
		<comments>http://www.manas.com.ar/spalladino/2013/01/29/extracting-subject-alternative-name-from-microsoft-authentication-client-certificates/#comments</comments>
		<pubDate>Tue, 29 Jan 2013 16:02:21 +0000</pubDate>
		<dc:creator>Santiago Palladino</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[Certificates]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/?p=707</guid>
		<description><![CDATA[Among the client certificates present in an HSPD-12 smart card, you may find a Windows Smart Card Login certificate used for authentication, which has several particularities, one of them being storing the Subject Alternative Name (this is, the Windows logon and domain of the card’s owner) in an ASN1-encoded UTF8 string in a Microsoft extension (OID 1.3.6.1.4.1.311.20.2.3). What does this &#8230; <a href="http://www.manas.com.ar/spalladino/2013/01/29/extracting-subject-alternative-name-from-microsoft-authentication-client-certificates/">more</a>]]></description>
			<content:encoded><![CDATA[<p>Among the client certificates present in an <a href="http://hspd12.usda.gov/">HSPD-12 smart card</a>, you may find a Windows Smart Card Login certificate used for authentication, which has <a href="http://unmitigatedrisk.com/?p=247">several particularities</a>, one of them being storing the Subject Alternative Name (this is, the Windows logon and domain of the card’s owner) in an ASN1-encoded UTF8 string in a <a href="http://support.microsoft.com/kb/281245">Microsoft extension</a> (OID 1.3.6.1.4.1.311.20.2.3).</p>
<p>What does this mean? This means that if you attempt to extract the subject alternative name from the certificate using Ruby&#8217;s <a href="http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL.html">OpenSSL::X509</a> library in the usual way, you&#8217;ll get a string reading simply &#8220;unsupported&#8221;.</p>
<div id="gist4665290" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-san-std-rb-L1" rel="file-san-std-rb-L1">1</span>
          <span class="line-number" id="file-san-std-rb-L2" rel="file-san-std-rb-L2">2</span>
          <span class="line-number" id="file-san-std-rb-L3" rel="file-san-std-rb-L3">3</span>
          <span class="line-number" id="file-san-std-rb-L4" rel="file-san-std-rb-L4">4</span>
          <span class="line-number" id="file-san-std-rb-L5" rel="file-san-std-rb-L5">5</span>
          <span class="line-number" id="file-san-std-rb-L6" rel="file-san-std-rb-L6">6</span>
          <span class="line-number" id="file-san-std-rb-L7" rel="file-san-std-rb-L7">7</span>
          <span class="line-number" id="file-san-std-rb-L8" rel="file-san-std-rb-L8">8</span>
          <span class="line-number" id="file-san-std-rb-L9" rel="file-san-std-rb-L9">9</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-san-std-rb-LC1"><span class="n">cert</span> <span class="o">=</span> <span class="ss">OpenSSL</span><span class="p">:</span><span class="ss">:X509</span><span class="o">::</span><span class="no">Certificate</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">certificate_string</span><span class="p">)</span></div><div class="line" id="file-san-std-rb-LC2"><span class="n">subject_alt_name</span> <span class="o">=</span> <span class="n">cert</span><span class="o">.</span><span class="n">extensions</span><span class="o">.</span><span class="n">find</span> <span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span><span class="o">.</span><span class="n">oid</span> <span class="o">==</span> <span class="s2">&quot;subjectAltName&quot;</span><span class="p">}</span></div><div class="line" id="file-san-std-rb-LC3"><span class="k">return</span> <span class="n">subject_alt_name</span><span class="o">.</span><span class="n">value</span></div><div class="line" id="file-san-std-rb-LC4">&nbsp;</div><div class="line" id="file-san-std-rb-LC5"><span class="c1"># On a standard certificate...</span></div><div class="line" id="file-san-std-rb-LC6"><span class="c1"># &#39;email:user@example.com&#39;</span></div><div class="line" id="file-san-std-rb-LC7">&nbsp;</div><div class="line" id="file-san-std-rb-LC8"><span class="c1"># On a MS certificate...</span></div><div class="line" id="file-san-std-rb-LC9"><span class="c1"># &#39;othername:&lt;unsupported&gt;, othername:&lt;unsupported&gt;&#39;</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/4665290/raw/12eb3d8ab15d9f5f06b53183d4b54e24920fa2ac/san.std.rb" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/4665290#file-san-std-rb" style="float:right; margin-right:10px; color:#666;">san.std.rb</a>
          <a href="https://gist.github.com/spalladino/4665290">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>

<p>Note that the value returned is a string indeed, not an exception; this is because the underlying unix openssl library, which is wrapped by the Ruby one, returns exactly that value.</p>
<p>In order to properly extract the owner&#8217;s identification, you&#8217;ll need to parse the certificate using <a href="http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/ASN1.html">OpenSSL::ASN1</a> and going through the hierarchy. The element you are looking for has key &#8220;msUPN&#8221;, this is, <a href="http://en.wikipedia.org/wiki/User_Principal_Name">Microsoft User Principal Name</a>.</p>
<div id="gist4665290" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-san-msupn-rb-L1" rel="file-san-msupn-rb-L1">1</span>
          <span class="line-number" id="file-san-msupn-rb-L2" rel="file-san-msupn-rb-L2">2</span>
          <span class="line-number" id="file-san-msupn-rb-L3" rel="file-san-msupn-rb-L3">3</span>
          <span class="line-number" id="file-san-msupn-rb-L4" rel="file-san-msupn-rb-L4">4</span>
          <span class="line-number" id="file-san-msupn-rb-L5" rel="file-san-msupn-rb-L5">5</span>
          <span class="line-number" id="file-san-msupn-rb-L6" rel="file-san-msupn-rb-L6">6</span>
          <span class="line-number" id="file-san-msupn-rb-L7" rel="file-san-msupn-rb-L7">7</span>
          <span class="line-number" id="file-san-msupn-rb-L8" rel="file-san-msupn-rb-L8">8</span>
          <span class="line-number" id="file-san-msupn-rb-L9" rel="file-san-msupn-rb-L9">9</span>
          <span class="line-number" id="file-san-msupn-rb-L10" rel="file-san-msupn-rb-L10">10</span>
          <span class="line-number" id="file-san-msupn-rb-L11" rel="file-san-msupn-rb-L11">11</span>
          <span class="line-number" id="file-san-msupn-rb-L12" rel="file-san-msupn-rb-L12">12</span>
          <span class="line-number" id="file-san-msupn-rb-L13" rel="file-san-msupn-rb-L13">13</span>
          <span class="line-number" id="file-san-msupn-rb-L14" rel="file-san-msupn-rb-L14">14</span>
          <span class="line-number" id="file-san-msupn-rb-L15" rel="file-san-msupn-rb-L15">15</span>
          <span class="line-number" id="file-san-msupn-rb-L16" rel="file-san-msupn-rb-L16">16</span>
          <span class="line-number" id="file-san-msupn-rb-L17" rel="file-san-msupn-rb-L17">17</span>
          <span class="line-number" id="file-san-msupn-rb-L18" rel="file-san-msupn-rb-L18">18</span>
          <span class="line-number" id="file-san-msupn-rb-L19" rel="file-san-msupn-rb-L19">19</span>
          <span class="line-number" id="file-san-msupn-rb-L20" rel="file-san-msupn-rb-L20">20</span>
          <span class="line-number" id="file-san-msupn-rb-L21" rel="file-san-msupn-rb-L21">21</span>
          <span class="line-number" id="file-san-msupn-rb-L22" rel="file-san-msupn-rb-L22">22</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-san-msupn-rb-LC1"><span class="n">cert</span> <span class="o">=</span> <span class="ss">OpenSSL</span><span class="p">:</span><span class="ss">:X509</span><span class="o">::</span><span class="no">Certificate</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">certificate_string</span><span class="p">)</span></div><div class="line" id="file-san-msupn-rb-LC2"><span class="n">subject_alt_name</span> <span class="o">=</span> <span class="n">cert</span><span class="o">.</span><span class="n">extensions</span><span class="o">.</span><span class="n">find</span> <span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span><span class="o">.</span><span class="n">oid</span> <span class="o">==</span> <span class="s2">&quot;subjectAltName&quot;</span><span class="p">}</span></div><div class="line" id="file-san-msupn-rb-LC3">&nbsp;</div><div class="line" id="file-san-msupn-rb-LC4"><span class="c1"># Parse the subject alternate name certificate extension as ASN1, first value should be the key</span></div><div class="line" id="file-san-msupn-rb-LC5"><span class="n">asn_san</span> <span class="o">=</span> <span class="ss">OpenSSL</span><span class="p">:</span><span class="ss">:ASN1</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">subject_alt_name</span><span class="p">)</span></div><div class="line" id="file-san-msupn-rb-LC6"><span class="k">raise</span> <span class="s2">&quot;Expected ASN1 Subject Alternate Name extension key to be subjectAltName but was </span><span class="si">#{</span><span class="n">asn_san</span><span class="o">.</span><span class="n">value</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">value</span><span class="si">}</span><span class="s2">&quot;</span> <span class="k">if</span> <span class="n">asn_san</span><span class="o">.</span><span class="n">value</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">value</span> <span class="o">!=</span> <span class="s1">&#39;subjectAltName&#39;</span></div><div class="line" id="file-san-msupn-rb-LC7">&nbsp;</div><div class="line" id="file-san-msupn-rb-LC8"><span class="c1"># And the second value should be a nested ASN1 sequence</span></div><div class="line" id="file-san-msupn-rb-LC9"><span class="n">asn_san_sequence</span> <span class="o">=</span> <span class="ss">OpenSSL</span><span class="p">:</span><span class="ss">:ASN1</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">asn_san</span><span class="o">.</span><span class="n">value</span><span class="o">[</span><span class="mi">1</span><span class="o">].</span><span class="n">value</span><span class="p">)</span></div><div class="line" id="file-san-msupn-rb-LC10">&nbsp;</div><div class="line" id="file-san-msupn-rb-LC11"><span class="c1"># Iterate through the ASN1 sequence looking for the msUPN key</span></div><div class="line" id="file-san-msupn-rb-LC12"><span class="n">asn_san_sequence</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">asn_data</span><span class="o">|</span></div><div class="line" id="file-san-msupn-rb-LC13">  <span class="c1"># As before, first value is the key</span></div><div class="line" id="file-san-msupn-rb-LC14">  <span class="n">key</span> <span class="o">=</span> <span class="n">asn_data</span><span class="o">.</span><span class="n">value</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">value</span></div><div class="line" id="file-san-msupn-rb-LC15">  <span class="k">next</span> <span class="k">if</span> <span class="n">key</span> <span class="o">!=</span> <span class="s1">&#39;msUPN&#39;</span></div><div class="line" id="file-san-msupn-rb-LC16">  <span class="c1"># And second value contains the actual data, return it if the key was msUPN</span></div><div class="line" id="file-san-msupn-rb-LC17">  <span class="n">email</span> <span class="o">=</span> <span class="n">asn_data</span><span class="o">.</span><span class="n">value</span><span class="o">[</span><span class="mi">1</span><span class="o">].</span><span class="n">value</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="n">value</span></div><div class="line" id="file-san-msupn-rb-LC18">  <span class="k">return</span> <span class="n">email</span></div><div class="line" id="file-san-msupn-rb-LC19"><span class="k">end</span></div><div class="line" id="file-san-msupn-rb-LC20">&nbsp;</div><div class="line" id="file-san-msupn-rb-LC21"><span class="c1"># Raise if we iterated through the sequence and did not find the key</span></div><div class="line" id="file-san-msupn-rb-LC22"><span class="k">raise</span> <span class="s2">&quot;Extension msUPN not found&quot;</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/4665290/raw/98e4052f278a9e37a639c69d2a5a14e5d077b4c5/san.msupn.rb" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/4665290#file-san-msupn-rb" style="float:right; margin-right:10px; color:#666;">san.msupn.rb</a>
          <a href="https://gist.github.com/spalladino/4665290">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>

<p>This way, you can set up a solution that works with both Microsoft based certificates and standard X509 authentication certificates.</p>
<p>Please comment if you have found other ways for working with these certificates, I&#8217;d love to see other implementations using Ruby&#8217;s OpenSSL or any other OpenSSL-based library!</p>
<p>[Many thanks to <a href="mailto:emagidson@deloitte.com">Ed Magidson</a> for his help in understanding and working with these certificates!]<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/spalladino/2013/01/29/extracting-subject-alternative-name-from-microsoft-authentication-client-certificates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoiding ActiveRecord instance creation to boost performance</title>
		<link>http://www.manas.com.ar/ary/2012/10/05/avoiding-activerecord-instance-creation-to-boost-performance/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=avoiding-activerecord-instance-creation-to-boost-performance</link>
		<comments>http://www.manas.com.ar/ary/2012/10/05/avoiding-activerecord-instance-creation-to-boost-performance/#comments</comments>
		<pubDate>Fri, 05 Oct 2012 19:52:02 +0000</pubDate>
		<dc:creator>Ary Borenszweig</dc:creator>
				<category><![CDATA[Blogs]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/?p=705</guid>
		<description><![CDATA[It is known that instantiating ActiveRecord objects is slow. When querying, most of the time it&#8217;s necessary to instantiate them so we can invoke their methods or navigate their relationships. When you need to do a custom select with a group by you generally do: Model.select(...).group(...).all This will create an ActiveRecord instance for each of &#8230; <a href="http://www.manas.com.ar/ary/2012/10/05/avoiding-activerecord-instance-creation-to-boost-performance/">more</a>]]></description>
			<content:encoded><![CDATA[<p>It is known that <a href="http://merbist.com/2012/02/23/quick-dive-into-ruby-orm-object-initialization/">instantiating ActiveRecord objects is slow</a>. When querying, most of the time it&#8217;s necessary to instantiate them so we can invoke their methods or navigate their relationships.</p>
<p>When you need to do a custom select with a group by you generally do:</p>
<pre>Model.select(...).group(...).all</pre>
<p>This will create an ActiveRecord instance for each of the results, with accessors for each of the elements in the select clause. This is very expensive compared to just creating arrays of the values as the result of the query.</p>
<p>So let&#8217;s do it:</p>
<pre>class ActiveRecord::Relation
  def raw
    connection.execute(to_sql)
  end
end</pre>
<p>You can use it like this:</p>
<pre>
Model.select('id, sum(hours)').group('id').raw.each do |id, hours|
  # ...
end
</pre>
<p>Here&#8217;s a sample IRB session showing the performance gain:</p>
<pre>
irb(main):024:0&gt; WorkedHour.where(work_group_id: 1).select('worker_id, worked_at, sum(hours)').group('worker_id, worked_at').all.length
=&gt; 3369
irb(main):025:0&gt; time = Time.now; WorkedHour.where(work_group_id: 1).select('worker_id, worked_at, sum(hours)').group('worker_id, worked_at').all; Time.now - time
=&gt; 0.314908
irb(main):026:0&gt; time = Time.now; WorkedHour.where(work_group_id: 1).select('worker_id, worked_at, sum(hours)').group('worker_id, worked_at').raw.to_a; Time.now - time
=&gt; 0.044524
irb(main):027:0&gt; 0.314908 / 0.044524
=&gt; 7.072769742161531
</pre>
<p>So, creating arrays as results for 3369 objects instead of ActiveRecord objects results in a 7x performance boost.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/ary/2012/10/05/avoiding-activerecord-instance-creation-to-boost-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Organizing coffeescript code in a Rails 3 app</title>
		<link>http://www.manas.com.ar/spalladino/2012/05/20/organizing-coffeescript-code-in-a-rails-3-app/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=organizing-coffeescript-code-in-a-rails-3-app</link>
		<comments>http://www.manas.com.ar/spalladino/2012/05/20/organizing-coffeescript-code-in-a-rails-3-app/#comments</comments>
		<pubDate>Sun, 20 May 2012 21:38:23 +0000</pubDate>
		<dc:creator>Santiago Palladino</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[organization]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/?p=698</guid>
		<description><![CDATA[Ever since Rails 3.1 adopted coffeescript within the asset pipeline as a default, coffescript has become an increasingly popular language for easily writing your javascript code. Despite a lot of tutorials out there on how to write coffee and take advantage of the Rails asset pipeline, there are rather few guidelines on how to properly &#8230; <a href="http://www.manas.com.ar/spalladino/2012/05/20/organizing-coffeescript-code-in-a-rails-3-app/">more</a>]]></description>
			<content:encoded><![CDATA[<p>Ever since <a href="http://guides.rubyonrails.org/3_1_release_notes.html">Rails 3.1</a> adopted <a href="http://coffeescript.org/">coffeescript</a> within the asset pipeline as a default, coffescript has become an increasingly popular language for easily writing your javascript code.</p>
<p>Despite a lot of tutorials out there on how to write coffee and take advantage of the Rails <a href="http://guides.rubyonrails.org/asset_pipeline.html">asset pipeline</a>, there are rather few guidelines on how to properly organize your code using these technologies; so let me outline a few techniques we have been using on some of our projects, such as <a href="https://bitbucket.org/instedd/verboice/wiki/Home">Verboice</a> and <a href="https://bitbucket.org/instedd/verboice/wiki/Home">ResourceMap</a> (both of them open source, so feel free to check them out).</p>
<h2>Knowing when to execute</h2>
<p>One of the key features of the asset pipeline is that all your js code is concatenated, minified and compressed into a single file, with aggresive client-side caching, so that the user only needs to download a single file the first time she visits your site.</p>
<p>However, this will imply that all of your javascript code will execute on every page in your site, unless you do something to prevent it. An easy way to solve this is to simply execute if there is a particular object in your DOM:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog01-js-coffee-L1" rel="file-blog01-js-coffee-L1">1</span>
          <span class="line-number" id="file-blog01-js-coffee-L2" rel="file-blog01-js-coffee-L2">2</span>
          <span class="line-number" id="file-blog01-js-coffee-L3" rel="file-blog01-js-coffee-L3">3</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog01-js-coffee-LC1"><span class="k">if</span> <span class="nx">$</span><span class="p">(</span><span class="s">&#39;#my_div_in_complex_page&#39;</span><span class="p">).</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span></div><div class="line" id="file-blog01-js-coffee-LC2">  <span class="k">class</span> <span class="nx">Foo</span></div><div class="line" id="file-blog01-js-coffee-LC3">    <span class="p">...</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/86c0d325ccd02eef05ac46df9e823e2e22f9e677/blog01.js.coffee" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog01-js-coffee" style="float:right; margin-right:10px; color:#666;">blog01.js.coffee</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>Eventually, if you find yourself relying too much on a particular object which indicates a complex functionality is required, you can abstract that to a global function, and use it whenever you need it:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog02-js-coffee-L1" rel="file-blog02-js-coffee-L1">1</span>
          <span class="line-number" id="file-blog02-js-coffee-L2" rel="file-blog02-js-coffee-L2">2</span>
          <span class="line-number" id="file-blog02-js-coffee-L3" rel="file-blog02-js-coffee-L3">3</span>
          <span class="line-number" id="file-blog02-js-coffee-L4" rel="file-blog02-js-coffee-L4">4</span>
          <span class="line-number" id="file-blog02-js-coffee-L5" rel="file-blog02-js-coffee-L5">5</span>
          <span class="line-number" id="file-blog02-js-coffee-L6" rel="file-blog02-js-coffee-L6">6</span>
          <span class="line-number" id="file-blog02-js-coffee-L7" rel="file-blog02-js-coffee-L7">7</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog02-js-coffee-LC1"><span class="c1"># on a global functions file</span></div><div class="line" id="file-blog02-js-coffee-LC2"><span class="vi">@onComplexFunctionality = </span><span class="nf">(callback) -&gt;</span> <span class="nx">$</span><span class="p">(</span><span class="nf">-&gt;</span> <span class="nx">callback</span><span class="p">()</span> <span class="k">if</span> <span class="nx">$</span><span class="p">(</span><span class="s">&#39;#my_div_in_complex_page&#39;</span><span class="p">).</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span></div><div class="line" id="file-blog02-js-coffee-LC3">&nbsp;</div><div class="line" id="file-blog02-js-coffee-LC4"><span class="c1"># when you need to use it</span></div><div class="line" id="file-blog02-js-coffee-LC5"><span class="nx">onComplexFunctionality</span> <span class="o">&gt;</span></div><div class="line" id="file-blog02-js-coffee-LC6">  <span class="k">class</span> <span class="nx">Foo</span></div><div class="line" id="file-blog02-js-coffee-LC7">    <span class="p">...</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/6457a4abab542bb733d680899f865da37add9c65/blog02.js.coffee" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog02-js-coffee" style="float:right; margin-right:10px; color:#666;">blog02.js.coffee</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>A more comprehensive approach is the one explained <a href="http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution">here</a>, which allows you to specify particular code to execute for each controller and each action without needing to rely on a particular DOM object or defining an &#8220;onFunctionality&#8221; function.</p>
<p>Basically, it works by defining functions in a global object with names that match controllers and their actions, such as:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog03-js-coffee-L1" rel="file-blog03-js-coffee-L1">1</span>
          <span class="line-number" id="file-blog03-js-coffee-L2" rel="file-blog03-js-coffee-L2">2</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog03-js-coffee-LC1"><span class="nv">Site.users.index = </span><span class="p">()</span> <span class="nf">-&gt;</span></div><div class="line" id="file-blog03-js-coffee-LC2">  <span class="c1"># your code for users/index here...</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/f9b1b1cfd7cde90c55aff5bb15c4f64e654d19e1/blog03.js.coffee" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog03-js-coffee" style="float:right; margin-right:10px; color:#666;">blog03.js.coffee</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>Modify your application&#8217;s template so it renders as data attributes of the HTML body which are the controller and action executed:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog04-html-erb-L1" rel="file-blog04-html-erb-L1">1</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog04-html-erb-LC1"><span class="nt">&lt;body</span> <span class="na">data-controller=</span><span class="s">&quot;</span><span class="cp">&lt;%=</span> <span class="n">controller_name</span> <span class="cp">%&gt;</span><span class="s">&quot;</span> <span class="na">data-action=</span><span class="s">&quot;</span><span class="cp">&lt;%=</span> <span class="n">action_name</span> <span class="cp">%&gt;</span><span class="s">&quot;</span><span class="nt">&gt;</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/40a649789ad95e97bb9d4816cf3267d0b1df91af/blog04.html.erb" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog04-html-erb" style="float:right; margin-right:10px; color:#666;">blog04.html.erb</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>And load that information from an executor function that runs on page load, executing the function that matches the current controller and action.</p>
<p>This approach, although it requires a larger setup, works very well for large applications, in which you have clearly separated behaviours in different pages of your app. Otherwise, relying on a simple check for a certain DOM element is a clean solution.</p>
<h2>One class per file</h2>
<p>One of the goodnesses of coffeescript is that it isolates each unit of code in a separate context, and makes each function local to that particular context, to avoid those pesky global functions so ubiquitous in traditional javascript. If you want to split the behaviour for a particular page in multiple files, if you are using any of the techniques described above, you will run into the problem that whatever classes you defined in file <em>A.js.coffee</em> within a callback at <em>onCondition</em>, will not be visible within the code you write in <em>B.js.coffee</em> in a similar callback.</p>
<p>If you want a class to be usable outside the context in which it is defined, the key is to define it as an attribute of the global window object, as described <a href="http://www.stakelon.com/2011/12/coffeescript-organizing-classes-in-separate-files/">here</a>:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog05-js-coffee-L1" rel="file-blog05-js-coffee-L1">1</span>
          <span class="line-number" id="file-blog05-js-coffee-L2" rel="file-blog05-js-coffee-L2">2</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog05-js-coffee-LC1"><span class="k">class</span> <span class="nb">window</span><span class="p">.</span><span class="nx">Foo</span></div><div class="line" id="file-blog05-js-coffee-LC2">  <span class="c1"># your code for the class...</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/ddc4d6fbeab76dcc83b6e82ad14a6afc854ec8e6/blog05.js.coffee" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog05-js-coffee" style="float:right; margin-right:10px; color:#666;">blog05.js.coffee</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>Even more succint, you can use the &#8220;@&#8221; syntax, as it will always refer to &#8216;this&#8217;, which, when defining the class, will be the global object:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog06-js-coffee-L1" rel="file-blog06-js-coffee-L1">1</span>
          <span class="line-number" id="file-blog06-js-coffee-L2" rel="file-blog06-js-coffee-L2">2</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog06-js-coffee-LC1"><span class="k">class</span> <span class="nx">@Foo</span></div><div class="line" id="file-blog06-js-coffee-LC2">  <span class="c1"># your code for the class...</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/3d0c480412d18f40fa5da9bb801612e6f445d679/blog06.js.coffee" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog06-js-coffee" style="float:right; margin-right:10px; color:#666;">blog06.js.coffee</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>This will allow you to refer to class Foo from any other file in your application easily. But what happens when you need to refer to class Foo before it has been defined, such as when you need to inherit from it? Consider the following case:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog07-js-coffee-L1" rel="file-blog07-js-coffee-L1">1</span>
          <span class="line-number" id="file-blog07-js-coffee-L2" rel="file-blog07-js-coffee-L2">2</span>
          <span class="line-number" id="file-blog07-js-coffee-L3" rel="file-blog07-js-coffee-L3">3</span>
          <span class="line-number" id="file-blog07-js-coffee-L4" rel="file-blog07-js-coffee-L4">4</span>
          <span class="line-number" id="file-blog07-js-coffee-L5" rel="file-blog07-js-coffee-L5">5</span>
          <span class="line-number" id="file-blog07-js-coffee-L6" rel="file-blog07-js-coffee-L6">6</span>
          <span class="line-number" id="file-blog07-js-coffee-L7" rel="file-blog07-js-coffee-L7">7</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog07-js-coffee-LC1"><span class="c1"># in file A.js.coffee</span></div><div class="line" id="file-blog07-js-coffee-LC2"><span class="k">class</span> <span class="nx">@A</span> <span class="k">extends</span> <span class="nx">B</span></div><div class="line" id="file-blog07-js-coffee-LC3">  <span class="c1"># fails because B has not been defined yet</span></div><div class="line" id="file-blog07-js-coffee-LC4">&nbsp;</div><div class="line" id="file-blog07-js-coffee-LC5"><span class="c1"># in file B.js.coffee</span></div><div class="line" id="file-blog07-js-coffee-LC6"><span class="k">class</span> <span class="nx">@B</span></div><div class="line" id="file-blog07-js-coffee-LC7">  <span class="c1"># is defined after A as this file is processed after A.js.coffee</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/fe9807aa21aa63c3e0b17f259985467f5faa8332/blog07.js.coffee" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog07-js-coffee" style="float:right; margin-right:10px; color:#666;">blog07.js.coffee</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>What you need to do is to instruct sprockets that file <em>B.js.coffee</em> needs to be included before file <em>A</em>. Considering your sprockets <em>application.js</em> file is probably simply doing a <em>require_tree</em> of the entire <em>assets/javascripts</em> folder, you want to keep this file as simple as possible, keeping the directive of including <em>B</em> before <em>A</em> within <em>A</em> or <em>B</em>.</p>
<p>As the <em>require</em> <a href="https://github.com/sstephenson/sprockets">directive in sprockets</a> will include the file only if it has not been included yet, you can simply add a require directive for file <em>B</em> at the beginning of <em>A</em>:<br />
<div id="gist2774757" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-blog08-js-coffee-L1" rel="file-blog08-js-coffee-L1">1</span>
          <span class="line-number" id="file-blog08-js-coffee-L2" rel="file-blog08-js-coffee-L2">2</span>
          <span class="line-number" id="file-blog08-js-coffee-L3" rel="file-blog08-js-coffee-L3">3</span>
          <span class="line-number" id="file-blog08-js-coffee-L4" rel="file-blog08-js-coffee-L4">4</span>
          <span class="line-number" id="file-blog08-js-coffee-L5" rel="file-blog08-js-coffee-L5">5</span>
          <span class="line-number" id="file-blog08-js-coffee-L6" rel="file-blog08-js-coffee-L6">6</span>
          <span class="line-number" id="file-blog08-js-coffee-L7" rel="file-blog08-js-coffee-L7">7</span>
          <span class="line-number" id="file-blog08-js-coffee-L8" rel="file-blog08-js-coffee-L8">8</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-blog08-js-coffee-LC1"><span class="c1"># in file A.js.coffee</span></div><div class="line" id="file-blog08-js-coffee-LC2"><span class="c1">#= require B</span></div><div class="line" id="file-blog08-js-coffee-LC3"><span class="k">class</span> <span class="nx">@A</span> <span class="k">extends</span> <span class="nx">B</span></div><div class="line" id="file-blog08-js-coffee-LC4">  <span class="p">...</span></div><div class="line" id="file-blog08-js-coffee-LC5">&nbsp;</div><div class="line" id="file-blog08-js-coffee-LC6"><span class="c1"># in file B.js.coffee</span></div><div class="line" id="file-blog08-js-coffee-LC7"><span class="k">class</span> <span class="nx">@B</span></div><div class="line" id="file-blog08-js-coffee-LC8">  <span class="p">...</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/spalladino/2774757/raw/737fd06041cd88d1972bb7d11e96f1d92e07be2e/blog08.js.coffee" style="float:right">view raw</a>
          <a href="https://gist.github.com/spalladino/2774757#file-blog08-js-coffee" style="float:right; margin-right:10px; color:#666;">blog08.js.coffee</a>
          <a href="https://gist.github.com/spalladino/2774757">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>
</p>
<p>This way, sprockets will execute the following flow:</p>
<ol>
<li>Starts from application.js, reads the require_tree directive and starts processing all files in the tree</li>
<li>First file in the tree is A.js.coffee, but begins with a directive to include B first</li>
<li>Includes file B.js.coffee, as specified by the directive in A</li>
<li>Proceeds to include the contents A.js.coffee</li>
<li>Continues processing the remaining files in the tree, skipping B.js.coffee as it has already included it</li>
</ol>
<p>Also, in the case you want to have a file containing certain global functions or classes, which will be used by other files in your app, you can take advantage of sprockets loading order.</p>
<p>Although you can use a <em>require global</em> directive at the beginning of each of your files, if you keep yout &#8216;global&#8217; code in the root of your javascripts directory and all page-specific files organised in folders, sprockets&#8217; <em>require_tree</em> directive will first load the files in the root of the directory, and then proceed with the sub-folders.</p>
<p>Another simple alternative is to add a <em>require path/to/globals</em> instruction before the <em>require_tree</em> in application.js, but you will have to follow this procedure for each global or shared file you add.</p>
<h2>Wrapping up</h2>
<p>There are lots of different techniques you can use to split your coffeescript code in different files and keep it organised. The key concepts to keep in mind when doing so are:</p>
<ul>
<li>You have to prepend @ to a class name if you want to use it from a different context</li>
<li>Be careful when writing code that depends on something defined in a different file, make sure to require it or ensure that sprockets will load it before</li>
<li>All your javascript code will always execute unless you execute it conditionally based on the HTML being rendered</li>
</ul>
<p>Hope it helps!<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/spalladino/2012/05/20/organizing-coffeescript-code-in-a-rails-3-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check if object can be destroyed if it has dependent: restrict associations</title>
		<link>http://www.manas.com.ar/spalladino/2012/01/31/how-to-check-if-object-can-be-destroyed-if-it-has-dependent-destroy-associations/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-check-if-object-can-be-destroyed-if-it-has-dependent-destroy-associations</link>
		<comments>http://www.manas.com.ar/spalladino/2012/01/31/how-to-check-if-object-can-be-destroyed-if-it-has-dependent-destroy-associations/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 14:35:14 +0000</pubDate>
		<dc:creator>Santiago Palladino</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/spalladino/?p=350</guid>
		<description><![CDATA[Rails provides several handy options for specifying how to deal with associated models upon deletion, for example: class Blog has_many :posts, :dependent =&#62; :destroy end The destroy value for the dependent option will call the destroy method for every post in the blog when the blog itself is destroyed. Other options are delete or nullify, &#8230; <a href="http://www.manas.com.ar/spalladino/2012/01/31/how-to-check-if-object-can-be-destroyed-if-it-has-dependent-destroy-associations/">more</a>]]></description>
			<content:encoded><![CDATA[<p>Rails provides <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many-label-Options">several handy options</a> for specifying how to deal with associated models upon deletion, for example:</p>
<pre>
class Blog
  has_many :posts, :dependent =&gt; :destroy
end
</pre>
<p>The <em>destroy</em> value for the <em>dependent</em> option will call the destroy method for every post in the blog when the blog itself is destroyed. Other options are delete or nullify, but the one we are interested in is <strong>restrict</strong>.</p>
<p>By specifying a relation as dependent restrict, Rails will prevent us from destroying a particular object if it has any associated objects. In the example, we would not be able to destroy a Blog if it has any Posts. It is implemented simply, by raising an <a href="http://apidock.com/rails/ActiveRecord/DeleteRestrictionError">ActiveRecord::DeleteRestrictionError</a> if there is any associated object.</p>
<p>Now, this works perfectly for preventing us from accidentally destroying an object, but we will usually want to check if we can destroy it beforehand, this is, when rendering a page to the client with a big bad <em>delete</em> button. Showing a delete button only to show an alert box with a &#8220;Could not delete&#8221; annoying message is certainly not a good practice, we should simply not draw the delete button in the first place.</p>
<p>How do we check this? We could manually check if each and every one of the associations we have marked with dependent restrict in the object are empty, but taking Rails DRY principle into account, we would like to automatically get that information from the object.</p>
<p>Luckily, ActiveRecord provides <a href="http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html">reflection methods</a> for obtaining info on the associations. Therefore, given an object, we can iterate its associations, and check if the restricted ones are empty or not.</p>
<p>This all boils down to <a href="https://gist.github.com/1710839">this small method</a>, which can be placed as an initializer in the Rails app:</p>
<pre>
class ActiveRecord::Base
  def can_destroy?
    self.class.reflect_on_all_associations.all? do |assoc|
      assoc.options[:dependent] != :restrict ||
        (assoc.macro == :has_one &amp;&amp; self.send(assoc.name).nil?) ||
        (assoc.macro == :has_many &amp;&amp; self.send(assoc.name).empty?)
    end
  end
end
</pre>
<p>That&#8217;s it! Now you can simply make a small helper method that renders a destroy link if can_destroy?, or a plain span notifying the user why she cannot destroy the object.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/spalladino/2012/01/31/how-to-check-if-object-can-be-destroyed-if-it-has-dependent-destroy-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Default request parameters in Rails functional tests</title>
		<link>http://www.manas.com.ar/spalladino/2011/12/02/default-request-parameters-in-rails-functional-tests/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=default-request-parameters-in-rails-functional-tests</link>
		<comments>http://www.manas.com.ar/spalladino/2011/12/02/default-request-parameters-in-rails-functional-tests/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 16:01:46 +0000</pubDate>
		<dc:creator>Santiago Palladino</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/spalladino/?p=343</guid>
		<description><![CDATA[I was looking for an easy way to force every request in a functional test in Rails to use a set of parameters by default, regardless of being specified explicitly. This is, every time I write: it "should get index" do get :index end Rails should actually do: it "should get index" do get :index, &#8230; <a href="http://www.manas.com.ar/spalladino/2011/12/02/default-request-parameters-in-rails-functional-tests/">more</a>]]></description>
			<content:encoded><![CDATA[<p>I was looking for an easy way to force every request in a functional test in Rails to use a set of parameters by default, regardless of being specified explicitly. This is, every time I write:</p>
<pre>
it "should get index" do
  get :index
end
</pre>
<p>Rails should actually do:</p>
<pre>
it "should get index" do
  get :index, :foo =&gt; 'value'
end
</pre>
<p>Lacking any option in the testing framework, I opted for simply monkeypatching the <b>process</b> method in <a href="http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html">ActionController::TestCase::Behaviour</a>. This method is invoked whenever methods <b>get</b>, <b>post</b>, <b>put</b>, <b>delete</b> or <b>head</b> are called, so it is the easiest single point to modify.</p>
<p><a href="https://gist.github.com/1423757">This gist</a> has the necessary code to perform the patch. If you are using <a href="https://www.relishapp.com/rspec">rspec</a>, simply copy the file in the spec/support folder.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/spalladino/2011/12/02/default-request-parameters-in-rails-functional-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>InSTEDD at Pacific Endeavor 2011</title>
		<link>http://www.manas.com.ar/spalladino/2011/09/21/instedd-at-pacific-endeavor-2011/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=instedd-at-pacific-endeavor-2011</link>
		<comments>http://www.manas.com.ar/spalladino/2011/09/21/instedd-at-pacific-endeavor-2011/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 03:20:32 +0000</pubDate>
		<dc:creator>Santiago Palladino</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[Instedd]]></category>
		<category><![CDATA[Pacific Endeavor]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/spalladino/?p=311</guid>
		<description><![CDATA[Pacific Endeavor is a humanitarian communication workshop, organized by the US Pacific Command, which brings together military representatives from all SE Asia, NGOs and industry leaders. Its main goal is to improve the multi-national communications in HA/DR situations, based on the premise that communication is the foundation to a successful response. This workshop if part &#8230; <a href="http://www.manas.com.ar/spalladino/2011/09/21/instedd-at-pacific-endeavor-2011/">more</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.pacom.mil/web/Site_Pages/Media/News_2011/08/17-Pacific-enveavor-2011-kicks-off.shtml">Pacific Endeavor</a> is a humanitarian communication workshop, organized by the US Pacific Command, which brings together military representatives from all SE Asia, NGOs and industry leaders. Its main goal is to improve the multi-national communications in HA/DR situations, based on the premise that communication is the foundation to a successful response. This workshop if part of the <a href="http://www.facebook.com/pages/MCIP-Multinational-Communications-Interoperability-Program/101122066602006?sk=info">MCIP</a>, Multinational Communications Interoperability Program:</p>
<blockquote><p>MCIP establishes a process that identifies and documents interoperability between Communication Systems (CS) of the MPAT nations in the Asia Pacfic Area. The process provides a mechanism for CS planners to effectively and rapidly establish interoperable CS arrangements to support MPAT during Multinational Humanitarian Assistance and Disaster Relief Operations (MNHADRO).</p></blockquote>
<p>Pacific Endeavor has had several previous editions. Last year Eric Rasmussen assisted representing InSTEDD; and last August I had the opportunity to assist to this year&#8217;s event, in order to communicate our experiences with the usage of text messaging during disaster response.</p>
<p><a href="http://www.manas.com.ar/wp-content/uploads/2011/09/IMG_12191.jpg"><img class="size-medium wp-image-316" src="http://www.manas.com.ar/wp-content/uploads/2011/09/IMG_12191-300x225.jpg" alt="Pacific Endeavor 2011" width="300" height="225" /></a></p>
<p>Pacific Endeavor is a large event, in which military representatives from multiple nations gather to assess the interoperability of their communication systems. The results of these experiments are fed into the MCIG, Multinational Communications Interoperability Guide, a database maintained by the MCIP that can be queried anytime to know whether communications between field teams of different countries on a disaster scenario will be possible, so that each team knows which equipment will be needed to communicate with others.</p>
<p><a href="http://www.manas.com.ar/wp-content/uploads/2011/09/IMG_0846.jpg"><img class="aligncenter size-medium wp-image-319" src="http://www.manas.com.ar/wp-content/uploads/2011/09/IMG_0846-300x198.jpg" alt="Technical Interoperability Assessment" width="300" height="198" /></a></p>
<p>Pacific Endeavor is also composed by other workshops, such as Phoenix Endeavor and <a href="http://www.dvidshub.net/news/75868/cyber-endeavor-2011-ends-will-return-next-year">Cyber Endeavor</a>. The goal of the former is to determine how to share the radio frequency spectrum during a disaster response situation, in which multiple teams arrive at the site, each with their own need for their share of spectrum in order to communicate. In order to address this situation, a web application was developed, that allows foreign countries and organizations to make requests to the host nation for radio spectrum, and allows the host nation to keep track of all these requests in a centralized location.</p>
<p>The latter, on the other hand, is focused on maintaining the security and availability of network communications during a disaster, ensuring the network is resilient to attacks. As stated by Staff Sgt. Carl Hudson:</p>
<blockquote><p>Cyber Endeavor is a new program under Pacific Endeavor 2011 which involves non-government organizations, national militaries across the Pacific Region, academic and industrial representatives. Focusing on protecting information in a collaborative environment, the intent was an information sharing workshop to develop core competencies amongst the Multinational Communications Interoperability Program countries and entities.</p></blockquote>
<p>The event involves different NGOs and Industry Leaders, leading to very interesting discussions on the interaction between the different parties on a disaster situation, and which should be the role of the military.</p>
<h2>Scenarios</h2>
<p>Multiple issues were under discussion by the time we arrived at the event. How to coordinate the local government, the military and the NGOs was, as usual, one of these trending topics. The discussions made clear the importance that the local government makes clear requests about what is needed, that information is shared across the different organization, and that the work is coordinated so that efforts are not duplicated.</p>
<p>Providing a reliable network infrastructure is another subject of critical importance. By assuring the different organizations deployed on the field that they will have the network access needed to perform their operations, the network can be better controlled and the disorganization generated by every team bringing their own equipment to attempt to set up their own network is eliminated. The role of providing network access for all teams deployed is then to be fulfilled by the military, and not by different private companies or each team individually.</p>
<p>Good bandwidth is also important, and it is vital during the first hours during which it is most scarce to determine the correct use for it. The NZ Red Cross estimates that only 0.1% of the transmitted information is actually operational, all the rest can be reduced by cutting down on videoconferences, sending formatted files instead of plain text, moving large unnecessary images, etc.</p>
<p>The key ongoing discussions were finally boiled down to four different scenarios, which had to be addressed by the present military representatives:</p>
<h3>Bandwidth</h3>
<p>Deployed organizations have need for bandwidth to conduct their operations, although this access is very scarce, specially during the first hours of the response. The solution is to develop a protocol and <a href="http://en.wikipedia.org/wiki/Standard_operating_procedure">SOP</a> so that the military can provide this much needed bandwidth with their infraestructure. It is worth noting that there is already a DOD initiative being implemented to address this very issue.</p>
<h3>Spectrum</h3>
<p>The organizations have requirements for usage of the radio spectrum during HA/DR; the military can lease certain frequencies during the response for these organizations to use. The aforementioned Mercury web application is a first step towards achieving this goal.</p>
<h3>Imagery</h3>
<p>Several organizations usually require access to highly detailed imagery data for decision-making; therefore, a protocol and SOP is to be developed for sharing this data between the military and the different organizations. It is critical that this point is implemented in such a way that the flow of detailed imagery does not consume the much needed bandwidth.</p>
<h3>Restoring Comms</h3>
<p>The first priority of the military during DR is to attend to the local government, whose communications are usually compromised during a disaster. As such, a SOP is to be developed to assemble a task force with the capabilities and know-how to help the local government restore their own communications as fast as possible.</p>
<h2>Technology Demonstrations</h2>
<p>On the last part of the event, the conclusions on the different scenarios were passed onto the Senior Communicators of each country. It was during this part that InSTEDD and other third party organizations entered the game.</p>
<p>It is worth noting that, even though representatives from different NGOs were present, such as <a href="http://www.tsfi.org/">Telecom Sans Frontiers</a> or the <a href="http://www.redcross.org.nz/cms_display.php">New Zealand Red Cross</a>, InSTEDD&#8217;s role during the event was of Solution Provider. The task as Solution Providers was to present the different tools and experience we have that can provide help for the military during HA/DR situations.</p>
<p>In our particular case, InSTEDD is in position to provide the <a href="http://instedd.org/blog/make-your-sms-apps-scale/">technological backbone for articulating SMS communications</a> with the people and different groups, aggregate this information, and <a href="http://blog.ushahidi.com/index.php/2010/02/11/project-4636-revisited-the-updated-info-graphic/">share it with the relevant organizations</a> during a disaster.</p>
<p>Most of the other solution providers were from the industry, and displayed very interesting tools. Several were related to satellite communications, to be used during the first hours when other means of communications are down; others focused on restoring damaged networks. Companies such as <a href="http://www.inmarsat.com/">Inmar-sat</a>, <a href="http://www.stee.com.sg/">ST Electronics</a>, <a href="http://www.delorme.com/">Delorme</a>, <a href="http://www.cisco.com">Cisco</a> or <a href="http://www.hp.com/">Hewlett Packard</a> were present.</p>
<p><strong>Cisco</strong> offered a team specialized in restoring networks at the disposal of any nation requesting them during a disaster, the <a href="http://www.cisco.com/web/about/doing_business/business_continuity/tacops.html">Disaster Incident Response Team</a>. <strong>Delorme</strong> presented a new satellite phone equipped with GPS that can send pre-canned SMS messages anywhere or augment an Android phone via Bluetooth with satellite capabilities. The <strong><a href="http://www.nps.edu/">Naval Postgraduate School</a></strong>, from Monterey, CA, was also in place, presenting multiple tools as well as their expertise on managing networks. A representative of the <a href="http://www.gvf.org/">Global VSAT Forum</a>, a worldwide association of satellite communications companies, presented the aid that can be provided by these organizations acting together during a disaster.</p>
<p><a href="http://www.cmu.edu/silicon-valley/faculty-staff/griss-martin.html">Dr Martin Griss</a>, from Silicon Valley Carnegie Mellon, made an excellent presentation introducing the usage of text messaging, smartphones and social media for obtaining information from the crowds during a disaster; presenting the work done within the <a href="http://www.cmu.edu/silicon-valley/dmi/">Disaster Management Initiative</a>. This opened the discussion for the usage of alternative ways of communication, both for communicating field workers, and for harnessing information from the different channels used by the people.</p>
<h2>The role of InSTEDD</h2>
<p>The exchange of ideas, experiences and requirements regarding these subjects was most fruitful. The <a href="http://ict4peace.wordpress.com/2010/05/11/instedds-response-in-haiti/">experience of the earthquake in Haiti</a>, now over a year ago, is still a key case study on HA/DR, so our findings during our participation on the <a href="http://instedd.org/blog/emergency-information-service-launched-in-haiti/">response</a> <a href="http://instedd.org/blog/ebyen-kile-tranbleman-de-te-a-ap-fin-pase-potoprens-en/">to</a> <a href="http://instedd.org/blog/haiti-sms-short-codes-ecosystem/">the</a> <a href="http://instedd.org/blog/haiti-sms-short-codes-ecosystem-update/">disaster</a> were most valuable. The role that text messaging had for both receiving reports from the people and sending localized pieces of key information back to them was of extreme importance; and the tools we used for channeling and analyzing the incoming flow of messages, as well as managing responses, were of interest for several parties.</p>
<p>We also presented other tools under development: an unreleased project, <strong>Pollit</strong>, formerly known as <a href="http://instedd.org/technologies/geochat-polls/">Geochat Polls</a>, was also the focus of much attention from many Senior Communicators, seeing in this SMS-based survey tool an easy way to perform damage assessment during a disaster.</p>
<p>Many other inquiries by the communicators were discussed. The problem of analyzing the large incoming flow of messages during an emergency, in order to be able to perform appropriate decision-making, was a trending topic. <a href="http://en.wikipedia.org/wiki/Crowdsourcing">Crowdsourcing</a> was the key concept when solving this matter on a disaster response situation, and <a href="http://taskmeup.org/">TaskMeUp</a>, a tool born from <a href="http://wiki.rhok.org/TaskMeUp">RHOK 2010</a> and inspired on the <a href="http://irevolution.net/2010/01/26/haiti-power-of-crowdsourcing/">information needs during Haiti&#8217;s Earthquake</a>, provided a simple way to implement it.</p>
<p>How to extract structured data from SMS was also an issue, one that we have been facing for a long time and has led us to many different solutions (some of them even <a href="http://ndt.instedd.org/2010/05/it-without-software.html">without relying on software</a>), which we presented during the event.</p>
<p>Some misconceptions were also clarified, stressing the fact that text messaging is <strong>not a replacement</strong> for all current communications. Radio and satellite communications are still the best choice for multiple scenarios, specially those related to keeping in touch the deployed S&amp;R teams during a disaster. But for other cases, such as obtaining information from the people, communicating back to them with localized key information for survival, or establishing a large network of local workers, SMS has proven to be a formidable tool: low cost communications, most people own a mobile and know how to text, SMS channels are more reliable than voice, text is easier to mine for information, etc.</p>
<h2>Call to Action</h2>
<p>Upon a disaster, receiving reports and requests for help from the people is vital on the <em>response</em> stage, as we found out on Haiti. During the <em>recovery</em> process SMS also fullfil an important role by empowering people with vital information for survival and development. The key for these to work is <em>preparedness</em>, and that is our call to action to the Senior Communicators.</p>
<p>Connecting with people at a massive level requires an agreement with the local telephone companies in order to achieve the necessary combination of high throughtput at low cost. These agreements can be set up beforehand, as well as the connections themselves to reduce the time for set up to a minimum.</p>
<p>Keeping a SMS-based network of local workers in key points is vital for damage assessment and coordination. A previously set up network of health workers for disease surveillance, for instance, can be quickly turned into a powerful damage assessment tool upon a disaster, and can be used to easily coordinate efforts between the network.</p>
<p>To sum up, it is critical to regard SMS as an important channel for communication during HA/DR scenarios, and to be prepared before the disaster happens in order to be able to deploy multiple solutions during the first vital hours.</p>
<h2>Conclusion</h2>
<p><a href="http://www.youtube.com/watch?v=zzc7ej3B8f8">Pacific Endeavor</a> is an event of critical importance for enabling communications between teams from different nations during an HA/DR mission, not only military, but also from NGOs and the industry sector. Coordination and communication between different parties is vital for an appropriate response.</p>
<p><iframe width="640" height="360" src="http://www.youtube.com/embed/zzc7ej3B8f8?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>We are very excited to participate in these events, and provide our experiences, tools and expertise to the representatives of countries from all South East Asia, to improve the communications between response teams and with the people in need.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/spalladino/2011/09/21/instedd-at-pacific-endeavor-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using xmpp for mocking SMS channel in Nuntium</title>
		<link>http://www.manas.com.ar/spalladino/2011/09/07/using-xmpp-for-mocking-sms-channel-in-nuntium/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-xmpp-for-mocking-sms-channel-in-nuntium</link>
		<comments>http://www.manas.com.ar/spalladino/2011/09/07/using-xmpp-for-mocking-sms-channel-in-nuntium/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 10:51:45 +0000</pubDate>
		<dc:creator>Santiago Palladino</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[Nuntium]]></category>
		<category><![CDATA[SMS]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/spalladino/?p=286</guid>
		<description><![CDATA[For those of you not familiar with it, Nuntium is an open-source tool we developed together with InSTEDD for easily building applications that rely on SMS for communication. Even though several other kinds of channels are supported, such as email or twitter, text messaging has been one of its most important features. A common scenario &#8230; <a href="http://www.manas.com.ar/spalladino/2011/09/07/using-xmpp-for-mocking-sms-channel-in-nuntium/">more</a>]]></description>
			<content:encoded><![CDATA[<p>For those of you not familiar with it, <a href="http://code.google.com/p/nuntium" target="_blank">Nuntium</a> is an open-source tool we developed together with <a href="http://instedd.org/technologies/nuntium/" target="_blank">InSTEDD</a> for easily building applications that rely on SMS for communication. Even though several other kinds of <a href="http://code.google.com/p/nuntium/wiki/Channels" target="_blank">channels</a> are supported, such as email or twitter, text messaging has been <a href="http://instedd.org/blog/make-your-sms-apps-scale/" target="_blank">one of its most important</a> features.</p>
<p><a href="http://code.google.com/p/nuntium/"><img class="alignright" src="http://instedd.org/wp-content/uploads/_post_blog_image/1195-5b23c41e.png?9d7bd4" alt="" width="234" height="130" /></a></p>
<p>A common scenario that we face is how to easily test the flow of text messages in a development or manual testing environment. While using a local gateway is a possibility, most likely you won&#8217;t want to spend actual text messages while developing; it is costly in the long run, and cumbersome to switch between the development station and a test cellphone.</p>
<p>One possibility to get around this is to use Nuntium&#8217;s very interface for generating <a href="http://code.google.com/p/nuntium/wiki/Messages#Application_Terminated_messages" target="_blank">AT</a> messages, and check the <a href="http://code.google.com/p/nuntium/wiki/Messages#Application_Originated_messages" target="_blank">AO</a> queue for responses.</p>
<div id="attachment_287" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.manas.com.ar/wp-content/uploads/2011/08/create-at.png"><img class="size-medium wp-image-287 " src="http://www.manas.com.ar/wp-content/uploads/2011/08/create-at-300x234.png" alt="Create Application Terminated Message in Nuntium" width="300" height="234" /></a><p class="wp-caption-text">Create Application Terminated Message in Nuntium</p></div>
<p>This interface can be a bit unsuited, however, for sending messages and receiving their responses instantly on the same page. It also requires access to the Nuntium account, so if you are looking forward to using it for testing, it implies granting access to everyone who is to test it to the account. An elegant solution, provided by my colleague <a href="http://www.linkedin.com/in/adrianromero/en">Adrian Romero</a>, is to use an XMPP channel for mocking the SMS flow.</p>
<h3>Setting up the XMPP channel</h3>
<p>Step one is to create a new account that you will use for testing the application and an application within the account. Now you are ready to set up the XMPP channel. For this, you will need an XMPP account to act as the channel; creating a test account in gmail is always a good candidate, in which case configuration is pretty straightforward:</p>
<div id="attachment_293" class="wp-caption alignright" style="width: 251px"><a href="http://www.manas.com.ar/wp-content/uploads/2011/08/channelxmpp.png"><img class="size-full wp-image-293" src="http://www.manas.com.ar/wp-content/uploads/2011/08/channelxmpp.png" alt="" width="241" height="294" /></a><p class="wp-caption-text">Configuration for xmpp channel with google talk</p></div>
<p>Make sure you <strong>change the protocol of the channel</strong> from XMPP to SMS. This will make Nuntium use this XMPP channel for routing SMS messages.</p>
<h3>Configuring AT Rules</h3>
<p>The trickiest part is setting up the <a href="http://code.google.com/p/nuntium/wiki/Rules">message rules</a> for mocking the SMS channel. The main challenge is that you want to simulate, from a single xmpp account, multiple SMS phones. In other words, you want to use your user@gmail.com account for pretending you are sending a text message from 555-1000, but also from 555-2000, 555-3000, and so on.</p>
<p>A nice solution to this is to actually specify the phone number as part of the message, and have Nuntium interpret it accordingly. So, if we send the message:</p>
<pre>5551000 Hello world</pre>
<p>We want Nuntium to convert it to a text message from <em>sms://5551000</em> with the content &#8220;Hello world&#8221;. To do this, we need to set up the following AT rules:</p>
<p style="text-align: center"><a href="http://www.manas.com.ar/wp-content/uploads/2011/08/atrules.png"><img class="aligncenter size-medium wp-image-296" src="http://www.manas.com.ar/wp-content/uploads/2011/08/atrules.png" alt="" width="600" height="74" /></a></p>
<pre>Condition: Body regex (d+)(s+)(.+)
Action:    From sms://${body.1}
           Body ${body.3}</pre>
<p>Let&#8217;s take a look at them more closely:</p>
<ul>
<li>The condition captures the number at the beginning in the message in a regex group, and leaves the rest of the message in another one. In our example, it would be 5551000 in the first group, and <em>Hello world</em> in the thrid one.</li>
<li>The first action forces the <em>from</em> field to be equal to the phone number; for this, it uses <a href="http://code.google.com/p/nuntium/wiki/Rules#Regular_Expressions" target="_blank">the ${field.group} syntax</a>. In this case, ${body.1} tells Nuntium to use the first capturing group from the regex matched on the body field, 5551000 in our example.</li>
<li>The second action removes the phone number from the body, leaving only <em>Hello world</em> in our example.</li>
</ul>
<p>Having these rules in place, we can use a single channel, xmpp in this case, to <em>fake</em> that we are multiple different senders.</p>
<h3>Configuring AO Rules</h3>
<p>Next step is to setup the AO rules now. Nuntium, instructed by your application, will attempt to send messages to phone numbers, and your xmpp channel will have a difficult time trying to send an IM to <em>sms://5552000</em>. So we want to divert these messages to our test jabber account, but still letting us know who was the original recipient for testing purposes.</p>
<p>So, if the application is trying to send a <em>Hello user!</em> message to <em>sms://5552000</em>, we want a message delivered to our jabber <em>username@gmail.com</em> account with the content:</p>
<pre>To: 5552000 Message: Hello user!</pre>
<p>To do this, we will set up the following set of rules that affect the <em>To</em> and the <em>Body</em> fields of the application originated message.</p>
<p style="text-align: center">
<a href="http://www.manas.com.ar/wp-content/uploads/2011/09/AORules1.png"><img class="aligncenter size-medium wp-image-308" src="http://www.manas.com.ar/wp-content/uploads/2011/09/AORules1.png" alt="" width="600" height="74" /></a>
</p>
<pre>Condition: To regex sms://(.+)
           Body regex (.*)
Actions:   To username@gmail.com
           Body To: ${to.1} Message: ${body.1}</pre>
<p>These rules are simpler than the previous ones. The conditions simply extract the phone number and body of the message; whereas the actions replace the target of the message with a hardcoded one, and condense the original target and the content in the body of the new message.</p>
<h3>To sum up&#8230;</h3>
<p>Nuntium offers greats flexibility by the usage of AT and AO rules, by granting the possibility to alter the flow of messages or their content. These tools, originally designed for connecting systems in real-world situations, can be also used for simplifying the development of a Nuntium-based application.<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/spalladino/2011/09/07/using-xmpp-for-mocking-sms-channel-in-nuntium/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reuse code, not user experience</title>
		<link>http://www.manas.com.ar/mverzilli/2011/06/15/reuse-code-not-user-experience/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=reuse-code-not-user-experience</link>
		<comments>http://www.manas.com.ar/mverzilli/2011/06/15/reuse-code-not-user-experience/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 22:47:23 +0000</pubDate>
		<dc:creator>Martin Verzilli</dc:creator>
				<category><![CDATA[Blogs]]></category>
		<category><![CDATA[interaction design]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://www.manas.com.ar/mverzilli/?p=46</guid>
		<description><![CDATA[Let&#8217;s face it: as a developer, I&#8217;m lazy. I just want to write just enough code. What is more, I want to reuse it whenever possible. I want it to be abstract. If I&#8217;m asked to implement a view which needs a sortable grid, one of those whose rows you can sort by any column by &#8230; <a href="http://www.manas.com.ar/mverzilli/2011/06/15/reuse-code-not-user-experience/">more</a>]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s face it: as a developer, I&#8217;m lazy. I just want to write <em>just enough</em> code.<em> </em>What is more, I want to reuse it whenever possible. I want it to be <em>abstract</em>.</p>
<p>If I&#8217;m asked to implement a view which needs a sortable grid, one of those whose rows you can sort by any column by clicking on it, I&#8217;ll either make use of any sortable grid widget available in the project&#8217;s UI framework of choice or build sorting functionality into a regular one. No matter the choice, I would end up with a grid widget that sorts strings lexicographically, dates chronologically and numbers&#8230; well, you get the idea.</p>
<p>Sounds reasonable, doesn&#8217;t it? Yes, it does&#8230; <em>in general</em>. <em>In general</em> means that statistically it IS the desired behavior,  <strong>provided that we know nothing else about the context of use.</strong> If we don&#8217;t focus on the task we&#8217;re trying to help the user accomplish, if we don&#8217;t <em>wear her shoes</em>, we risk ending up happy with our <em>reusable</em> grid at the cost of making it <em>less usable.</em></p>
<p>Let&#8217;s take a look at an example of  a team which clearly cares for its users:</p>
<div id="attachment_47" class="wp-caption aligncenter" style="width: 662px"><a href="http://www.manas.com.ar/wp-content/uploads/2011/06/Screen-shot-2011-06-15-at-6.20.23-PM.png"><img class="size-full wp-image-47" src="http://www.manas.com.ar/wp-content/uploads/2011/06/Screen-shot-2011-06-15-at-6.20.23-PM.png" alt="" width="652" height="243" /></a><p class="wp-caption-text">The grid in iTunes music library, sorted by song name</p></div>
<p>So, let&#8217;s see. Name is a string field, then we should sort it lexicographically&#8230; wait a minute! If it&#8217;s in lexicographical order why is it that &#8220;5 &#8211; In Your Eyes!!s&#8221; is placed between &#8220;05 &#8211; Heartbreaker&#8221; and &#8220;05 &#8211; Intermedio (JadeI)&#8221;? Because it makes sense in this context.</p>
<p>Odds are I don&#8217;t care whether the track number starts with zero or it doesn&#8217;t. By discarding the <em>general </em>approach to sorting strings, the iTunes&#8217; team made its users&#8217; experience a little bit more pleasant. And, at the end of the day, all those little details add up to make an overall satisfactory experience.</p>
<p>Now, if you ask me, at least when I sort my list by song name, I usually don&#8217;t even care about the track number and whichever other characters prefixing a song&#8217;s title. I would even ignore all those characters and start sorting by the first alphabetical character. But I can&#8217;t assure most iTunes users would agree with me. Maybe it&#8217;s a good example of a decision you would like to make based on the results of usability tests.</p>
<p>To sum up, if you&#8217;re a lazy developer like me, don&#8217;t let your appetite for abstraction and reuse leak into the user experience. Less is more, except sometimes when more is more!<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.manas.com.ar/mverzilli/2011/06/15/reuse-code-not-user-experience/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
