<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Sam de Freyssinet]]></title>
  <link href="http://def.reyssi.net/atom.xml" rel="self"/>
  <link href="http://def.reyssi.net/"/>
  <updated>2014-09-25T22:24:40-05:00</updated>
  <id>http://def.reyssi.net/</id>
  <author>
    <name><![CDATA[Sam de Freyssinet]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Dutch Mobile Conference 2014]]></title>
    <link href="http://def.reyssi.net/blog/2014/04/05/dutch-mobile-conference-2014/"/>
    <updated>2014-04-05T15:54:35-05:00</updated>
    <id>http://def.reyssi.net/blog/2014/04/05/dutch-mobile-conference-2014</id>
    <content type="html"><![CDATA[<p><a href="http://www.mobileconference.nl/">The Dutch Mobile Conference</a> in Amsterdam is once again being held this June. It is the third year this conference has been held in conjunction with its well established and lauded sibling, <a href="http://www.phpconference.nl/">The Dutch PHP Conference</a>. This year the focus of the Dutch Mobile Conference is returning to all things mobile, returning the subject matter to native mobile development as well as mobile web.</p>

<p>I was very fortunate to be able to speak at the inaugural <a href="http://lanyrd.com/2012/dutch-mobile-conference/">Dutch Mobile Conference</a> in 2012, and I am delighted to be asked to return to speak once more. This year I am going to be tackling the subject of Behaviour-Driven Development. BDD has been a subject that has found itself at home at many a PHP and Ruby conference in recent years. However it has been noticeably absent from the mobile gatherings. Very conscious of this, I have decided it is time to bring testing to the forefront of mobile development.</p>

<!-- more -->


<p>The talk I intend to deliver (<em>Ed:</em> yes it&rsquo;s still being written) will begin by explaining why BDD is so fundamentally important to software engineers, testers and business owners. This will give me the chance to introduce the uninitiated to the Gherkin domain-specific language for expressing behaviour, usually run by the Cucumber testing running framework. Once everyone is comfortable with Gherkin, I will switch my attention to delivering a mythical iOS application that has been defined in Gherkin. We will watch the tests fail inside of Calabash-Cucumber and then implement the logic to make them pass until finally, we have created a fully functioning awesome iOS application.</p>

<p>The talk is designed to be accessible for people of all levels of knowledge of BDD, Cucumber and Calabash. It is being constructed to be very demonstration driven, with live code and test examples throughout. All code will be available for people to try themselves from Github after the presentation.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Arbiter: A simple notification observer framework for Ruby]]></title>
    <link href="http://def.reyssi.net/blog/2013/07/21/arbiter-an-observer-pattern-framework-for-ruby/"/>
    <updated>2013-07-21T11:59:00-05:00</updated>
    <id>http://def.reyssi.net/blog/2013/07/21/arbiter-an-observer-pattern-framework-for-ruby</id>
    <content type="html"><![CDATA[<p>The team at <a href="http://www.sittercity.com">Sittercity</a> have been busy building a vast variety of software components for the organisation over the last twelve months. The array of components crafted range from iOS applications to single sign-on services. One problem that has consistently appeared is how to handle various use cases where multiple parties are interested in the result of a particular routine. Early in the lifecycle of a project these concerns are usually handled directly within the use case itself, and providing there are only one or two operations things are usually happy. But as it turns out, many use cases have a wide range of interested parties. Shovelling each interested parties concerns into the use case does not scale well, whilst simultaneously violating a number of good design practices.</p>

<p>To help explain the problem, consider this simple example.</p>

<!-- more -->




<figure class='code'><figcaption><span>lib/authenticate/context/email.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Authenticate</span>
</span><span class='line'>  <span class="k">module</span> <span class="nn">Context</span>
</span><span class='line'>    <span class="k">class</span> <span class="nc">Email</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">user_repository</span><span class="p">,</span> <span class="n">secret_hashing_policy</span><span class="p">)</span>
</span><span class='line'>        <span class="vi">@user_repository</span> <span class="o">=</span> <span class="n">user_repository</span>
</span><span class='line'>        <span class="vi">@secret_hashing_policy</span> <span class="o">=</span> <span class="n">secret_hashing_policy</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">email</span><span class="p">,</span> <span class="n">secret</span><span class="p">)</span>
</span><span class='line'>        <span class="n">authentic</span> <span class="o">=</span> <span class="kp">false</span>
</span><span class='line'>        <span class="n">user</span> <span class="o">=</span> <span class="vi">@user_repository</span><span class="o">.</span><span class="n">find_by_email</span><span class="p">(</span><span class="n">email</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">user</span>
</span><span class='line'>          <span class="n">authentic</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">secret</span><span class="o">.</span><span class="n">eql?</span><span class="p">(</span><span class="vi">@secret_hashing_policy</span><span class="o">.</span><span class="n">hash</span><span class="p">(</span><span class="n">secret</span><span class="p">))</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">authentic</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here we have a simple context that authenticates credentials based on the supplied email and secret. The context has two dependencies, as shown in Fig.1;</p>

<p><img src="http://def.reyssi.net/images/arbiter-dependency-graph-1.png" title="Fig.1 Dependency graph for Authenticate::Context::Email" ></p>

<ol>
<li>A user repository that provides access user data</li>
<li>A hashing policy that governs how secrets are hashed before storage</li>
</ol>


<p>Both of these dependencies are passed into the context during construction. The context provides a single <code>execute</code> method that tests the users authenticity based on the user identifier and secret provided.</p>

<p>This use case as it stands is doing its job very well. Additionally it is adhering to the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility principle</a>, as it only concerns itself with establishing the authenticity of the credentials supplied. It isn&rsquo;t concerned with loading user data or how to correctly hash a secret.</p>

<p>Later on, the business owner walks over and asks to have insight into the number of authentications occurring;</p>

<blockquote><p>When a user authenticates, the business must record the authentication data into our statistics data warehouse. We want to know who was authenticating and the result of their attempt.</p></blockquote>


<p>She wants to capture this data in another statistics repository owned by the Data Warehouse team. Given the data our business owner wants is related to authentications, it seems a simple requirement to fulfil.</p>

<figure class='code'><figcaption><span>lib/authenticate/context/email.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Authenticate</span>
</span><span class='line'>  <span class="k">module</span> <span class="nn">Context</span>
</span><span class='line'>    <span class="k">class</span> <span class="nc">Email</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">user_repository</span><span class="p">,</span> <span class="n">secret_hashing_policy</span><span class="p">,</span> <span class="n">stats_repository</span><span class="p">)</span>
</span><span class='line'>        <span class="vi">@user_repository</span> <span class="o">=</span> <span class="n">user_repository</span>
</span><span class='line'>        <span class="vi">@secret_hashing_policy</span> <span class="o">=</span> <span class="n">secret_hashing_policy</span>
</span><span class='line'>        <span class="vi">@stats_repository</span> <span class="o">=</span> <span class="n">stats_repository</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">email</span><span class="p">,</span> <span class="n">secret</span><span class="p">)</span>
</span><span class='line'>        <span class="n">authentic</span> <span class="o">=</span> <span class="kp">false</span>
</span><span class='line'>        <span class="n">user</span> <span class="o">=</span> <span class="vi">@user_repository</span><span class="o">.</span><span class="n">find_by_email</span><span class="p">(</span><span class="n">email</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">user</span>
</span><span class='line'>          <span class="n">authentic</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">secret</span><span class="o">.</span><span class="n">eql?</span><span class="p">(</span><span class="vi">@secret_hashing_policy</span><span class="o">.</span><span class="n">hash</span><span class="p">(</span><span class="n">secret</span><span class="p">))</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>        <span class="vi">@stats_repository</span><span class="o">.</span><span class="n">register_entry</span><span class="p">(</span><span class="n">email</span><span class="p">,</span> <span class="n">authentic</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">authentic</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Fulfilling the requirement to provide authentication statistics to the business has grown the authentication context slightly. As it now authenticates and updates statistics, you could argue that it has begun to violate the single responsibility principle. I would certainly agree with your argument if you were to make it. But given the scope of this change, most developers could probably live with that slight violation at this point.</p>

<p>A few days later, the business owner returns to ask for another great idea.</p>

<blockquote><p>When a user fails to authenticate, they should receive an email notification instructing them on how to access the service.</p></blockquote>


<p>Now we could just go back and modify the use case to include a mailer and the context would grow further. We are definitely going to violate the single responsibility principle now, as we are authenticating credentials and performing other notification tasks. A another approach would be to create an additional use case for authenticating credentials and notifying various parties. This use case would be a composite of the <code>Authenticate::Context::Email</code> context and two others. It would certainly be cleaner than trying to overload the existing use case with all these extra responsibilities. However, either solution starts introducing dependencies that authentication should not have.</p>

<p><img src="http://def.reyssi.net/images/arbiter-dependency-graph-2.png" title="Fig.2 Dependency graph for Authenticate::Context::Email demonstrating violation of the single responsibility principle" ></p>

<p>Fig.2 demonstrates that authenticating a set of credentials now requires a lot of knowledge about other unrelated parts of the system. The simple act of authentication has become confused by the other requirements that have been made.</p>

<p>At this point we should pause for a second and consider the problem again. What we really want to do is authenticate some credentials and then tell the system we are done <em>authenticating</em>. The system can then respond to the message from the authenticate context without having to provide knowledge of the intended behaviour.</p>

<p>We would like is the authentication context to execute and return a boolean value as it does now. Additionally, we want it to notify observers that is finished. The context would still require one additional dependency for notifying, but that would be it. The business owner could provide additional requirements in future for what should happen when authentication succeeds or fails, and the existing authentication context would not have to be altered.</p>

<p>Lets go back to our original implementation of <code>Authenticate::Context::Email</code> and add a basic concept of a notifier.</p>

<figure class='code'><figcaption><span>lib/authenticate/context/email.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Authenticate</span>
</span><span class='line'>  <span class="k">module</span> <span class="nn">Context</span>
</span><span class='line'>    <span class="k">class</span> <span class="nc">Email</span>
</span><span class='line'>
</span><span class='line'>      <span class="no">AUTHENTICATION_NOTIFICATION</span> <span class="o">=</span> <span class="s1">&#39;authenticate::context::email::complete&#39;</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">user_repository</span><span class="p">,</span> <span class="n">secret_hashing_policy</span><span class="p">,</span> <span class="n">notifier</span><span class="p">)</span>
</span><span class='line'>        <span class="vi">@user_repository</span> <span class="o">=</span> <span class="n">user_repository</span>
</span><span class='line'>        <span class="vi">@secret_hashing_policy</span> <span class="o">=</span> <span class="n">secret_hashing_policy</span>
</span><span class='line'>        <span class="vi">@notifier</span> <span class="o">=</span> <span class="n">notifier</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">email</span><span class="p">,</span> <span class="n">secret</span><span class="p">)</span>
</span><span class='line'>        <span class="n">authentic</span> <span class="o">=</span> <span class="kp">false</span>
</span><span class='line'>        <span class="n">user</span> <span class="o">=</span> <span class="vi">@user_repository</span><span class="o">.</span><span class="n">find_by_email</span><span class="p">(</span><span class="n">email</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">user</span>
</span><span class='line'>          <span class="n">authentic</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">secret</span><span class="o">.</span><span class="n">eql?</span><span class="p">(</span><span class="vi">@secret_hashing_policy</span><span class="o">.</span><span class="n">hash</span><span class="p">(</span><span class="n">secret</span><span class="p">))</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>        <span class="vi">@notifier</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="no">AUTHENTICATION_NOTIFICATION</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'>          <span class="s1">&#39;authentic&#39;</span> <span class="o">=&gt;</span> <span class="n">authentic</span><span class="p">,</span>
</span><span class='line'>          <span class="s1">&#39;email&#39;</span> <span class="o">=&gt;</span> <span class="n">email</span>
</span><span class='line'>        <span class="p">})</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">credentials_are_authentic</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we have introduced one additional dependency to the original two we defined. The notifier must be passed in, along with the user repository and hashing policy. But the notifier is the only additional dependency we will need. Now we can go and change the implementation of our statistics reporter and account access notifier to listen to the <code>authenticate::context::email::complete</code> notification coming from the notifier and perform their actions accordingly.</p>

<p><img src="http://def.reyssi.net/images/arbiter-dependency-graph-3.png" title="Fig.3 Dependency graph for Authenticate::Context::Email demonstrating dependency inversion" ></p>

<p>This implementation also does something interesting to the dependencies. Notice that <code>Authenticate::Context::Email</code> depends on the <code>Notifier</code>, but no longer depends on <code>Authenticate::Stats</code> or <code>Message::AccountAccess</code>. Instead, <code>Authenticate::Stats</code> and <code>Message::AccountAccess</code> depend on <code>Notifier</code>. The result is that <code>Authenticate::Context::Email</code> no longer has any knowledge of <code>Authenticate::Stats</code> or <code>Message::AccountAccess</code> and vice versa. We have inverted the dependencies and introduced a solid boundary between our authentication use case and other parties that need to know about authentication notifications but are not part of that domain.</p>

<p>The <code>Notifier</code> part of this implementation is currently an abstract concept. After looking around the Ruby world for an existing solution, we ended up devising and building a new observer pattern framework called <a href="http://github.com/sittercity/arbiter/">Arbiter</a>.</p>

<h2>Introducing Arbiter</h2>

<p>Arbiter is a simple notification framework. It provides a lightweight notification protocol that can be used across any Ruby application. Simply put, Arbiter is our solution to the <code>Notifier</code> class in the earlier example. There are only two component parts to Arbiter, the Arbiter and the Eventer. The Arbiter observes notifications and executes business logic associated with those notices.</p>

<p>The Eventer is the interface to post notices for observation. The Eventer consists of a transport (<code>Eventer.bus</code>) provided by the Arbiter and the <code>Eventer.post()</code> class method.</p>

<p>The Arbiter provides the transport for notifications posted by the Eventer. The Arbiter intercepts notifications on the transport and passes them to observers that register to a particular event. Registration of observers is done using the <code>Arbiter.add_listener()</code> class method.</p>

<p>Any class can observe notifications from Arbiter, provided it conforms to the Arbiter protocol by implementing the following methods;</p>

<ul>
<li><code>notify(message_name, args)</code>, called by Arbiter whenever it receives a notification that the observer is interested in.</li>
<li><code>subscribe_to()</code>,  used by Arbiter to establish the notifications that want to be observed. This method should return an array of notification names.</li>
</ul>


<p>This is all that is required to begin observing notifications with Arbiter. Did I mention it was simple?</p>

<h2>Example</h2>

<p>Revisiting the earlier example of an authentication context, now we will implement the context and the observers using the Arbiter framework.</p>

<p>First of all we need to decide on a transport for Arbiter to use. Given we want to keep this solution simple for the time being, lets use the standard Arbiter transport which uses an in-process execution model. To configure this, we&rsquo;ll setup the <code>Eventer.bus</code> in a configuration for the application.</p>

<figure class='code'><figcaption><span>/config/eventer.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;eventer&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;arbiter&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="no">Eventer</span><span class="o">.</span><span class="n">bus</span> <span class="o">=</span> <span class="no">Arbiter</span>
</span></code></pre></td></tr></table></div></figure>


<p>The configuration file sets up Eventer to use the basic Arbiter as the transport, referred to as the <code>bus</code>. With this we can write the observers.</p>

<figure class='code'><figcaption><span>/lib/stats/observer.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;stats/repository&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="k">module</span> <span class="nn">Stats</span>
</span><span class='line'>  <span class="k">class</span> <span class="nc">Observer</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">stats_repository</span><span class="p">)</span>
</span><span class='line'>      <span class="vi">@stats_repository</span> <span class="o">=</span> <span class="n">stats_repository</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">subscribe_to</span><span class="p">()</span>
</span><span class='line'>      <span class="k">return</span> <span class="o">[</span><span class="ss">:authenticate</span><span class="o">]</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">notify</span><span class="p">(</span><span class="n">message_name</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</span><span class='line'>      <span class="vi">@stats_repository</span><span class="o">.</span><span class="n">register_entry</span><span class="p">(</span><span class="n">args</span><span class="o">[</span><span class="s1">&#39;email&#39;</span><span class="o">]</span><span class="p">,</span> <span class="n">args</span><span class="o">[</span><span class="s1">&#39;authentic&#39;</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This observer provides the same functionality for recording authentications as implemented earlier. The actual recording of statistics is now completed within the observers <code>notify()</code> method.</p>

<p>We can use the same pattern for the observer that is responsible for the account access email message when authentication fails.</p>

<figure class='code'><figcaption><span>/lib/message/authentication/observer.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Message</span>
</span><span class='line'>  <span class="k">module</span> <span class="nn">Authentication</span>
</span><span class='line'>    <span class="k">class</span> <span class="nc">Observer</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">message_manager</span><span class="p">)</span>
</span><span class='line'>        <span class="vi">@message_manager</span> <span class="o">=</span> <span class="n">message_manager</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">subscribe_to</span><span class="p">()</span>
</span><span class='line'>        <span class="k">return</span> <span class="o">[</span><span class="ss">:authenticate</span><span class="o">]</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">notify</span><span class="p">(</span><span class="n">message_name</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</span><span class='line'>        <span class="vi">@message_manager</span><span class="o">.</span><span class="n">send_reaccess_account</span><span class="p">(</span><span class="n">args</span><span class="o">[</span><span class="s1">&#39;email&#39;</span><span class="o">]</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="n">args</span><span class="o">[</span><span class="s1">&#39;authentic&#39;</span><span class="o">]</span> <span class="o">==</span> <span class="kp">false</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we have our two observers configured and ready to receive messages, lets update the original <code>Authenticate::Context::Email</code> context to use Arbiter.</p>

<figure class='code'><figcaption><span>lib/authenticate/context/email.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Authenticate</span>
</span><span class='line'>  <span class="k">module</span> <span class="nn">Context</span>
</span><span class='line'>    <span class="k">class</span> <span class="nc">Email</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">user_repository</span><span class="p">,</span> <span class="n">secret_hashing_policy</span><span class="p">,</span> <span class="n">eventer</span><span class="p">)</span>
</span><span class='line'>        <span class="vi">@user_repository</span> <span class="o">=</span> <span class="n">user_repository</span>
</span><span class='line'>        <span class="vi">@secret_hashing_policy</span> <span class="o">=</span> <span class="n">secret_hashing_policy</span>
</span><span class='line'>        <span class="vi">@eventer</span> <span class="o">=</span> <span class="n">eventer</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">email</span><span class="p">,</span> <span class="n">secret</span><span class="p">)</span>
</span><span class='line'>        <span class="n">authentic</span> <span class="o">=</span> <span class="kp">false</span>
</span><span class='line'>        <span class="n">user</span> <span class="o">=</span> <span class="vi">@user_repository</span><span class="o">.</span><span class="n">find_by_email</span><span class="p">(</span><span class="n">email</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="n">user</span>
</span><span class='line'>          <span class="n">authentic</span> <span class="o">=</span> <span class="n">user</span><span class="o">.</span><span class="n">secret</span><span class="o">.</span><span class="n">eql?</span><span class="p">(</span><span class="vi">@secret_hashing_policy</span><span class="o">.</span><span class="n">hash</span><span class="p">(</span><span class="n">secret</span><span class="p">))</span>
</span><span class='line'>        <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>        <span class="vi">@eventer</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="ss">:authenticate</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'>          <span class="s1">&#39;authentic&#39;</span> <span class="o">=&gt;</span> <span class="n">authentic</span><span class="p">,</span>
</span><span class='line'>          <span class="s1">&#39;email&#39;</span> <span class="o">=&gt;</span> <span class="n">email</span>
</span><span class='line'>        <span class="p">})</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">credentials_are_authentic</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Our context is now using the Arbiter <code>Eventer</code> to post the <code>:authenticate</code> notification to the transport. We are almost ready to go, but there is a problem. Right now we are publishing our notifications correctly and have two observers ready to observe. But they are not going to observe anything yet as they have not been registered to the Arbiter. To do this we will need to tell the Arbiter that <code>Stats::Observer</code> and <code>Message::Authentication::Observer</code> want to listen to notifications. Where you do this in your application will depend on its structure. But for the sake of simplicity lets set up these observers in the <code>eventer.rb</code> configuration file we defined earlier.</p>

<figure class='code'><figcaption><span>/config/eventer.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;eventer&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;arbiter&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;lib/stats/observer&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;lib/message/authentication/observer&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;factory&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="no">Eventer</span><span class="o">.</span><span class="n">bus</span> <span class="o">=</span> <span class="no">Arbiter</span>
</span><span class='line'>
</span><span class='line'><span class="no">Arbiter</span><span class="o">.</span><span class="n">set_listeners</span><span class="p">(</span><span class="o">[</span><span class="ss">Stats</span><span class="p">:</span><span class="ss">:Observer</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">Factory</span><span class="o">.</span><span class="n">stats_repo</span><span class="p">),</span> <span class="ss">Message</span><span class="p">:</span><span class="ss">:Authentication</span><span class="o">::</span><span class="no">Observer</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">Factory</span><span class="o">.</span><span class="n">message_manager</span><span class="p">)</span><span class="o">]</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>We have successfully implemented the Arbiter framework to replace our Notifier abstraction used earlier. As a result the authentication use case is now maintaining its single responsibility of authenticating credentials and the two observers we wrote handle the recording of statistics and any other messages as a result of the authentication process.</p>

<h2>Notifications beyond the process boundary</h2>

<p>The example above uses Arbiter to send notifications in-process to observers. However there are times where there are concurrent processes operating with observers interested in a particular topic.</p>

<p>For example, a main application handles user requests, while a second process is devoted to statistical analysis and a third is devoted to messaging users. The primary process will be publishing notifications and the other two processes want to listen to those notifications. In these cases the Arbiter example shown above will not work.</p>

<p>Having also encountered this problem, we have created two additional Arbiter implementations to allow for concurrent process observations.</p>

<ol>
<li><code>ResqueArbiter</code> uses the <a href="http://github.com/resque/resque">Resque</a> framework as the <code>Eventer.bus</code></li>
<li><code>ZeromqArbiter</code> uses the <a href="http://www.zeromq.org">ZeroMQ</a> (ØMQ) messaging protocol as the <code>Eventer.bus</code>. This Arbiter transport provides the most flexibility, but can be the most complex to use.</li>
</ol>


<p>It is possible to use any transport for notifications, as long as you can provide the Arbiter implementation to handle the registering of observers and delivery of messages. However be aware that thread safety and other concurrent concerns still apply when using Arbiter across processes.</p>

<h2>Obtaining and using Arbiter</h2>

<p>We welcome and encourage everyone to use and improve this lightweight notification framework. Please fork and pull to your delight.</p>

<p>Arbiter is now released as an open source library available on <a href="http://github.com/sittercity/arbiter">GitHub</a> and pre-packaged as a Ruby <a href="https://rubygems.org/gems/arbiter">Gem</a>. Arbiter is licensed under the <a href="http://opensource.org/licenses/ISC">ISC License</a>.</p>

<p>Arbiter is maintained by the awesome Sittercity development team and was written by Robert Grider (<a href="http://twitter.com/mrerrormessage">@mrerrormessage</a>) and Jeremy Bush (<a href="http://twitter.com/jeremybush">@zombor</a>), based on a concept by Robert Grider and myself.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Compiling libzmq as a universal static library for iOS]]></title>
    <link href="http://def.reyssi.net/blog/2013/01/01/compiling-libzmq-as-a-universal-static-library-for-iOS/"/>
    <updated>2013-01-01T13:53:00-06:00</updated>
    <id>http://def.reyssi.net/blog/2013/01/01/compiling-libzmq-as-a-universal-static-library-for-iOS</id>
    <content type="html"><![CDATA[<p>I am currently playing around with the <a href="http://www.zeromq.org">ZeroMQ</a> messaging framework and iOS. This article describes the challenges encountered when integrating ZeroMQ with my iOS project and how I mitigated them. This resulted in a tool to automate producing a single libzmq static library for i386, armv7 and armv7s architectures.</p>

<p>For the uninitiated, ZeroMQ provides a simple way to pass messages between processes, whether they are local or remote. It can use TCP, UDP, inproc, multicast and several other protocols as the transport mechanism. In the past I have heard ZeroMQ (or <em>ØMQ</em> from now on) referred to as a &ldquo;sockets framework&rdquo;. Although this doesn&rsquo;t capture the entirety of what ØMQ is, it does provide a nice metaphor for my particular use case.</p>

<p>The project that required ØMQ in this instance was a proof of concept for another project I am currently engaged in. The application in question requires two iOS devices to communicate with each other in realtime across a network. ØMQ provides the communication layer, coupled with <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/about.html">Apple&rsquo;s Bonjour</a> zero config network discovery protocol. As ØMQ is a C based library, integrating it with an iOS project should require relatively little effort. Obtain the <a href="http://www.zeromq.org/area:download">libzmq source</a>, compile and add to the Xcode project. But as it turned out, producing a static library for iOS development took some additional effort.</p>

<!--more-->


<p>Because Apple provide an iOS simulator for development rather than a more useful iOS emulator, any static library must be compiled for the simulator and then again for iOS devices. When an iOS application runs in the simulator, it is using the <a href="http://en.wikipedia.org/wiki/X86">x86</a> (or i386) <a href="http://en.wikipedia.org/wiki/Instruction_set">instruction set</a>; iOS applications are not 64-bit, yet. However all iOS devices use an <a href="http://arm.com">ARM</a> based CPU of some description, requiring different instructions from their x86 cousins. Apple currently support two <a href="http://en.wikipedia.org/wiki/ARM_architecture">ARM architectures</a>, named armv7 for iPhone 4/4S plus the iPad 2 and 3. And armv7s for iPhone 5 and the new iPad 4 (retina display)<sup>[<a href="#f1">1</a>] [<a href="#f2">2</a>]</sup>. Due to this, my project requires ØMQ support for the i386, armv7 and armv7s architecture. It is possible to compile the ØMQ static library three times, add them all to the project and only link to the correct version at compile time. But this can be fiddly, especially when dealing with two almost identical architectures such as armv7 and armv7s.</p>

<p>Fortunately Apple have encountered this multiple architecture problem in their recent past, when moving from the PowerPC to the current x86/x86_64 architecture. Apple wanted to ensure the transitional period was as smooth as possible for their users. So they created the concept of the universal binary, which they imaginatively named a &ldquo;Universal Application&rdquo;. To the end user, they could execute the same application on their existing PowerPC powered iMac G5 or a shiny new Intel powered MacBook Pro without issue. However, this apparently magical execution of the same code on two different CPU architectures was nothing more than a cheap parlour trick. In reality a universal binary is a single resource that encapsulates the compiled code for two or more CPU architectures. Although the PowerPC to Intel transition was completed many years ago, the tools to produce these universal binaries remain. Only today iOS developers are combining x86 with ARM instructions.</p>

<p>It is worthwhile to note that universal libraries are also referred to as &ldquo;fat&rdquo; libraries, because they are larger than their single architecture counterparts. The overall iOS application size is a factor in how they can be downloaded to the device. Applications weighing in at over 20 megabytes will require a wifi connection to be downloaded, and all applications must be less than two gigabytes. Although the two gigabyte ceiling is unlikely to be a problem due to a fat library, the 20 megabyte limit could cause a concern. If size is a factor for your application then it is probably worthwhile to only include the correct library for the target architecture. I&rsquo;ll touch on this briefly a bit later.</p>

<p>The ØMQ page describing how to build <a href="http://www.zeromq.org/build:iphone">libzmq for iOS</a> details the <a href="http://en.wikipedia.org/wiki/Cross_compiler">cross-compiling</a> process. This is fine for creating an armv7 static library from an Intel powered Mac. But as discussed earlier, this library would not be usable by the iOS simulator. I require a universal static library for libzmq that can execute on all of the required architectures.</p>

<p>The process of creating a universal static library for libzmq is conceptually straight forward, but as it turns out it is quite cumbersome. In theory, all that is required is to compile the libzmq library for i386, armv7 and armv7s and then glue them together into the universal binary. Compiling the binary for ARM requires cross-compiling using a version of <a href="http://gcc.gnu.org">GCC</a> with instructions for that CPU. All Mac systems with Xcode installed contain the ARM version of the compiler. The only challenge is figuring out the correct compile flags to set to ensure the right binary is produced.</p>

<p>To save a lot of time I automated the process into a single bash script. I have placed this script on Github for anyone who requires it; available from <a href="https://github.com/samsoir/libzmq-ios-universal">https://github.com/samsoir/libzmq-ios-universal</a>. It is licensed under the open source <a href="http://opensource.org/licenses/ISC">ISC license</a>, so modifications and extensions are welcome. For those not willing to look at the <a href="https://github.com/samsoir/libzmq-ios-universal/blob/master/compile_libzmq_ios_universal">source</a>, the script performs the following operations.</p>

<ol>
<li>Compiles a libzmq static library for the armv7, armv7s and i386 architectures<sup>[<a href="#f3">3</a>]</sup>.</li>
<li>A tool called <a href="https://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/lipo.1.html">lipo</a> is used to perform the gluing of the three libraries into a single universal static library.</li>
<li>The header files for the library are copied into the universal folder.</li>
<li>Finally the script tidies up after itself and then provides instructions on what to do next.</li>
</ol>


<p>Once the script has finished executing successfully, there will be four versions of the libzmq library. One version for each architecture, and of course the universal version. The individual architecture versions of libzmq remain so that you can use them in place of the universal version if application size is an issue.</p>

<p>The current project on Github is work in progress. It mostly supports my specific requirements at this time. If you try it and it does not work as designed for you, please do tell me or create a pull request. I do intend to add features and more support over time.</p>

<h2>Tutorial</h2>

<p>To use the <strong>libzmq-ios-universal</strong> compilation script, you will require a Mac with OS X Snow Lion with Xcode 4.3.3 or later installed. Additionally, <a href="http://www.gnu.org/software/automake/">Automake</a> is also required. I used <a href="http://mxcl.github.com/homebrew/">Homebrew</a> to obtain Automake in this tutorial, but you can also download and compile it yourself if you do not have, or want to have, Homebrew installed.</p>

<p>First of all, if you have not done so already, install Automake using Homebrew. <em>Or build manually if preferred</em></p>

<p>In a terminal type;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>brew install automake</span></code></pre></td></tr></table></div></figure>


<p>To verify whether automake is successfully installed;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>automake --version</span></code></pre></td></tr></table></div></figure>


<p>This should produce output similar to what is shown below. If automake is not found try reinstalling.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>automake (GNU automake) 1.12.3
</span><span class='line'>Copyright (C) 2012 Free Software Foundation, Inc.
</span><span class='line'>License GPLv2+: GNU GPL version 2 or later &lt;http://gnu.org/licenses/gpl-2.0.html>
</span><span class='line'>This is free software: you are free to change and redistribute it.
</span><span class='line'>There is NO WARRANTY, to the extent permitted by law.
</span><span class='line'>
</span><span class='line'>Written by Tom Tromey &lt;tromey@redhat.com>
</span><span class='line'>       and Alexandre Duret-Lutz &lt;adl@gnu.org>.</span></code></pre></td></tr></table></div></figure>


<p>With Automake installed and available, it is time to get the compilation script and the libzmq source code. Start by cloning the libzmq-ios-universal project from Github. Change directory to a suitable location</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>clone https://github.com/samsoir/libzmq-ios-universal.git</span></code></pre></td></tr></table></div></figure>


<p>Once the clone operation is completed, <code>cd libzmq-ios-universal</code> so that the next step downloads into the project folder.</p>

<p>Now download and untar the latest stable build of libzmq from the <a href="http://www.zeromq.org/area:download">ZeroMQ download page</a>. At the time of writing this is version 3.2.2.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>wget http://download.zeromq.org/zeromq-3.2.2.tar.gz && tar xfz zeromq-3.2.2.tar.gz</span></code></pre></td></tr></table></div></figure>


<p>If everything is fine up until now, we are ready to begin compiling libzmq for iOS. To begin the process type;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>./compile_libzmq_ios_universal</span></code></pre></td></tr></table></div></figure>


<p>A successful compilation will result in the following output;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Initializing build directory...
</span><span class='line'>Initializing dependency directory...
</span><span class='line'>Compiling libzmq for armv7...
</span><span class='line'>Compiling libzmq for armv7s...
</span><span class='line'>Compiling libzmq for i386...
</span><span class='line'>Creating universal static library for armv7, armv7s and i386
</span><span class='line'>Copying libzmq headers into universal library...
</span><span class='line'>Tidying up...
</span><span class='line'>Finished compiling libzmq as a static library for iOS.
</span><span class='line'>
</span><span class='line'>Universal library can be found in build/universal;
</span><span class='line'>  "lib" folder contains "libzmq.a" static library
</span><span class='line'>  "include" folder contains headers
</span><span class='line'>
</span><span class='line'>To use in your project follow linking instructions
</span><span class='line'>available on the iOS zeromq page
</span><span class='line'>http://www.zeromq.org/build:iphone</span></code></pre></td></tr></table></div></figure>


<p>Note that in the unfortunate event that something goes wrong during the compilation, the root cause may not be immediately apparent. However the last command executed places all of its output into a file called <code>build/build.log</code> relative to the current path. The cause of the issue should be apparent within that file.</p>

<p>Assuming everything is fine, it is now possible to add the new universal static library to Xcode.</p>

<p>Change the directory to your Xcode project, for example;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>cd ~/Projects/&lt;project name></span></code></pre></td></tr></table></div></figure>


<p>I like to keep my external libraries in their own directory. So I create a directory called &ldquo;lib&rdquo; within the project to keep them in.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>mkdir -p &lt;project name>/lib && open &lt;project name>
</span><span class='line'>cp /&lt;path>/&lt;to>/&lt;libzmq>/build/universal &lt;project name>/lib/libzmq</span></code></pre></td></tr></table></div></figure>


<p>This will result in a new directory named <code>lib</code> created within the <code>MyZMQProject</code> code base directory. Within that folder with be a <code>libzmq</code> folder containing the static library and headers. Xcode will nest the directories like this to keep separate targets apart within a project. Finally the command will open a Finder window at <code>./MyZMQProject</code> allowing you to see the <code>lib</code> folder.</p>

<p><img src="http://def.reyssi.net/images/libzmq/lib.png" title="Finder showing lib folder" ></p>

<p>Ensure your Xcode project is open within Xcode and then drag the <code>lib</code> folder from Finder into the correct (and corresponding location) within Xcode. A dialog will appear asking how you want to add the folder to the project.</p>

<p><img src="http://def.reyssi.net/images/libzmq/xcode_add_to_project.png" title="Xcode add folder to project" ></p>

<p>Once you select &ldquo;Finish&rdquo; the <code>lib</code> folder will be added to your project.</p>

<p>Before you can start using the library you need to update your projects settings to link the library to your project at compile time, and include the headers in the search path.</p>

<p>Within the main target of your project, filter the <em>Build Settings</em> to only show items including &ldquo;Search Path&rdquo;. Then update <em>Header Search Paths</em> section to include your newly created <code>lib</code> folder. Add <code>${SRCROOT}/&lt;project name&gt;/lib/**</code> to the list and hit return. The double-star means search recursively through sub folders.</p>

<p><img src="http://def.reyssi.net/images/libzmq/header_search_paths.png" title="Header search paths in project settings" ></p>

<p>Xcode 4.5 automatically adds the correct path for libzmq to the <em>Library Search Paths</em> entry. However if this hasn&rsquo;t been added for you, the same entry that was added for <em>Header Search Paths</em> will work here.</p>

<p>Finally ensure you add the standard C++ library to the <code>Other Linker Flags</code> section of the <em>Build Settings</em>. Add <code>-lstdc++</code> to the list if it is not already present.</p>

<p><img src="http://def.reyssi.net/images/libzmq/xcode_other_linker.png" title="stdc++ C++ standard library linker flag" ></p>

<p>At this point you are ready to start using libzmq in your code. To use the ØMQ C library within your code, import the <code>zmq.h</code> header in any location where you require it. Libzmq will now run  in the simulator or on a device without modification.</p>

<h2>Footnotes</h2>

<p><strong id="f1">1.</strong> As the name implies, armv7 instructions can be executed on armv7s architectures, but the reverse is not true. The armv7s architecture provides additional instructions that provide faster execution of code over armv7, so it is worth supporting it when possible.</p>

<p><strong id="f2">2.</strong> Anyone that is put off by this, it is worth mentioning that there is an <a href="https://github.com/jeremy-w/objc-zmq">Objective C</a> higher level wrapper in existence.</p>

<p><strong id="f3">3.</strong> I decided not to support armv6 at this point. It may be added in in future if demand shows it is worthwhile. Or there is a pull request.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[WindyCityGo Mobile Conference Day 1]]></title>
    <link href="http://def.reyssi.net/blog/2012/04/08/windycitygo-mobile-conference-day-1/"/>
    <updated>2012-04-08T15:46:00-05:00</updated>
    <id>http://def.reyssi.net/blog/2012/04/08/windycitygo-mobile-conference-day-1</id>
    <content type="html"><![CDATA[<p><img src="http://windycitygo.org/theme/4d63eb23dabe9d771e0001cb/stylesheets/images/windycitygo-tagline.png" title="WindyCityGo logo" ></p>

<p>This week I attended the <a href="http://windycitygo.org/">WindyCityGo, Chicago&rsquo;s mobile developers conference</a>. WindyCityGo is in its second year and like musicians second album, it was potentially a tricky one. Chicago&rsquo;s tech scene is currently enjoying massive growth, no doubt helped by the success of locally based popular internet brands Threadless, 37signals, Orbitz, Career Builder and not forgetting Groupon. Recently many local startup companies have also been making a big name for themselves as well such as Peapod, GrubHub, Braintree and my employer Sittercity. With this in mind I expected a massive turnout for the WindyCityGo conference, so I was rather surprised to discover that this conference was a very intimate affair.</p>

<!-- more -->


<p>The conference was hosted at the Groupon headquarters, providing a rather cosy environment for proceedings. The lecture room chosen was probably not the most suitable for a conference of this nature. However the free wifi did provide a great service and didn&rsquo;t drop once in the two days, which was a refreshing change from most conferences. However, the room felt slightly awkward. Three large displays provided the slide presentations, one large plasma at the front flanked by two attached to pillars. Speaking of the pillars, they were bulky and spread throughout the room such that if you got caught behind one it hid the speaker completely. These details are only small negative points against the venue, but they did start to add up. Given that Groupon were also the platinum sponsor it did all feel a little bit too convenient. However, none of these details would stop the conference from commencing, and it did so with real gusto.</p>

<p>The first day started with a session from <a href="http://www.secondcity.com">The Second City, Chicago</a> Improv players, lead by <a href="http://twitter.com/CodyDove">Cody Dove</a>. The Second City group are famous for launching the careers of John Belushi, Bill Murray, Mike Myers, Dan Aykroyd, Steve Carell and Stephen Colbert among many many others. Needless to say the players for the conference weren&rsquo;t any of the above, but they were excellent nonetheless. The session started with an improvised sketch with all four players creating a short sketch whilst pausing for amusing input from the audience. This then lead into another four activities all requiring audience participation in one form or another. It was very energetic and more importantly fun. Once the players had finished, the audience was awake, alert and ready to learn. This was a novel and inspired way to kick things off and I tip my hat to the organisers. Not since <a href="http://www.youtube.com/watch?v=4NFKSxBas4g&amp;list=PL4850B9F46ADEFBE2&amp;index=1&amp;feature=plpp_video">Aral Balkan&rsquo;s opening performance</a> at Update Conference 2011 has such an introduction been so entertaining. Everyone was now ready for some technical talks.</p>

<p>First up was a talk on <em>Painless Localization</em> from <a href="http://twitter.com/wbyoung">Whitney Young</a> of <a href="http://fadingred.com">FadingRed</a>, the creators of the Mac personal finance application <a href="http://itunes.apple.com/us/app/koku/id408895448?mt=12">Koku</a>. Localization is often an overlooked aspect of mobile and desktop development, but vitally important in these competitive markets if you want to reach the broadest possible audience. Whitney provided some analysis of global language distribution to re-enforce his argument. It was fascinating to witness that the proportion of English as a first language speakers is relatively small on a global scale. However, a large proportion of the worlds population would use a computer within the english localization. The talk then went on to explain how you localize projects in Cocoa, which was nice to see as I&rsquo;m currently in the process of doing this myself. Localization is not trivial on any platform and you can see why in some cases developers choose to ignore it completely. This puts localization right up there with accessibility in this respect.</p>

<p>To ease the pain and make life easier for FadingRed localizing their applications, they created a tool called <a href="https://github.com/fadingred/Greenwich">Greenwich</a>. Greenwhich is a Cocoa framework that provides a great solution to the localization problem, enabling the users of the application to provide the translations. Whitney explained that before Greenwich was created, their end users would email them translations for their applications. So it was clear there was a hunger for user-created translations. Crowd sourcing localization obviously has some risks, but Greenwich provides some additional tooling to sanity check the provided translations. The real power of Greenwich is the instant feedback mechanism, allowing translations to be examined in the app immediately, without having to recompile the binary. Greenwhich looks like a great project and one I&rsquo;ll be watching closely. If you&rsquo;re building Cocoa apps for desktop or mobile, I suggest you checkout Greenwich.</p>

<p>Following Whitney was <a href="http://twitter.com/dlkinney">David Kinney</a> presenting <em>iOS Application and Data Security</em>. This talk provided some deep insight to some of the security issues that plague iOS, though in reality Android suffers the same vulnerabilities. The talk started with a general look at iOS security model;</p>

<ul>
<li><strong>Device Security</strong> via pass code or pin code</li>
<li><strong>Data Security</strong> via encryption and keychain</li>
<li><strong>Network Security</strong> via SSL/TLS and VPN</li>
<li><strong>Application Security</strong> via code signing and sandboxing</li>
</ul>


<p>First off David criticised people who protect their iOS devices using just the four digit pin code. The pin code protection provides ten-thousand possible variations, which is a lot for a person to go through. However software can defeat this in under two minutes. iOS does provide a string based pass code as an alternate method of securing the device and David recommended using that with a strong password. Personally I feel that if your device is stolen then you&rsquo;re screwed anyway. Modern hardware can process around <a href="http://www.elcomsoft.com/eprb.html#gpu">three billion passwords</a> per second. At that rate a password that a human can also recite will be broken in a reasonable amount of time.</p>

<p>Given the device is not safe, the next subject of Data Security was apt. If the device is not safe, then data must be made secure. David went into to detail about the OS security features, pointing out the flaws with using Keychain. Keychain is only as secure as the device. So if sensitive data is being stored then it is best to encrypt it using a custom method using the <a href="https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/Common%20Crypto.3cc.html#//apple_ref/doc/man/3cc/CommonCrypto">CommonCrypto library</a>. David went into more detail covering the preferred encryption algorithms to use such as AES and TripleDES, touching briefly on <a href="http://en.wikipedia.org/wiki/PBKDF2">Password Based Key Derivation Function v2</a>. Overall David&rsquo;s talk was highly informative and gave a great overview of the security pitfalls of developing for devices in general.</p>

<p>Next up was <a href="http://twitter.com/emeyer8thlight">Eric Meyer</a> from <a href="http://www.8thlight.com/">8th Light</a> presenting a talk on how to test code properly in Cocoa. This is the subject matter that I was most interested in seeing today and Eric did not disappoint. I have had the pleasure of briefly working with Eric so I was sure this would be an informative session. The session took the form of a brief introduction to the two tools Eric would be using, <a href="https://github.com/ericmeyer/ObjectiveCSlim">ObjectiveCSlim</a> and <a href="https://github.com/paytonrules/OCDSpec">OCDSpec</a>. <em>ObjectiveCSlim</em> is an Objective C implementation of the <a href="http://fitness.org">Fitness</a> wiki based acceptance test framework. <em>OCDSpec</em> is an implementation of <a href="http://rspec.info/">RSpec</a> (or <a href="http://www.phpspec.net/">PHPSpec</a>) in Objective C.</p>

<p>With the introduction to the tools complete, Eric switched into live demonstration mode. Using the board game <a href="http://en.wikipedia.org/wiki/Mastermind_(board_game)">Mastermind</a> as the use case, Eric demonstrated how to build the ObjectiveCSlim and OCDSpec tests and then implement code against them. Of course this is classic test driven development. Unfortunately there is not a lot to say, other than ObjectiveCSlim looks like it is a great tool for acceptance test verification. OCDSpec is also an interesting tool, although it is very verbose (even awkward) when compared to the implementation in its dynamic language cousins. But this is small criticism. Anyone writing Objective C would be used to this level of verbosity. Eric&rsquo;s presentation was competent and bar a few minor live demo issues, was delivered without issue. I must give Eric bonus points for doing a live demonstration.</p>

<p>After lunch <a href="http://twitter.com/jasonvanlue">Jason Van Lue</a> kicked off things with his talk on the mobile design process. This session was aimed squarely at web designers for all devices. The talk provided a walk through some best and worst practices of modern web design, touching on the differences between adaptive and <a href="http://www.alistapart.com/articles/responsive-web-design/">responsive web design</a>. As an aside, it is nice to see responsive design being talked about at conferences right now. Just as progressive enhancement was important in the last decade, responsive design is important in this one. In reality progressive enhancement and graceful degradation sit comfortably with responsive design. They are all members of the same discipline, the web as one platform for all.</p>

<p>Jason&rsquo;s talk was very well delivered and equally well received. I must be honest that I didn&rsquo;t learn very much myself, but I&rsquo;ve spent a lot of time researching this subject matter. I imagine everyone watching who wasn&rsquo;t aware of these deign concepts benefitted from Jason&rsquo;s talk.</p>

<p>After a break the subject matter returned to the technical. <a href="http://twitter.com/jchris">Chris Anderson</a> presented a talk on the CouchDB backed datastore, <a href="http://www.couchbase.com/">CouchBase</a>. CouchBase is basically a set of existing open source technologies woven together into a new product that resembles a document-store-come-memory-cache with a multitude of interfaces. Out of the box CouchBase has a RESTful HTTP and Memcache compatible interfaces for remote access, as well client libraries for C, Java, .NET, Ruby, Python and PHP. Chris provided a broad overview of how CouchBase builds upon CouchDB and Memcache to provide a highly scalable and resilient datastore.</p>

<p>At this point another related project called <a href="https://github.com/couchbaselabs/TouchDB-iOS">TouchDB</a> was introduced. TouchDB is an iOS datastore that is <em>sync compatible</em> with CouchDB, and by extension CouchBase. TouchDB basically provides the same data abstraction as Apple&rsquo;s CoreData, but using an open source implementation. This was all very intriguing, but I was beginning to wonder why I would use TouchDB over CoreData? The next subject covered categorically answered this question.</p>

<p><a href="http://www.couchbase.com/wiki/display/couchbase/Couchbase+Mobile+Syncpoint">CouchBase Mobile Syncpoint</a> is a new sync service (currently in beta) for CouchBase and TouchDB. CouchBase Mobile Syncpoint effectively provides an open source <a href="https://www.icloud.com/">iCloud</a> implementation. However unlike iCloud, multi-tenant synchronisation does not have to be limited to one user account. This potentially allows for some new, and until now impossible, synchronisation models.</p>

<p>There was a lot of information delivered by Chris over his 40 minutes, but little technical detail. At times it felt a little too light on technical details, but I guess this was a symptom of the number of subjects being introduced. The talk has certainly inspired me to look at CouchBase Mobile Syncpoint in more detail.</p>

<p>The last talk of the day came from <a href="http://twitter.com/escoz">Eduardo Scoz</a> on the subject of <a href="https://github.com/escoz/QuickDialog">QuickDialog</a>. QuickDialog is an iOS framework designed to make dialogs simple. Eduardo started his presentation exclaiming that the UIKit framework was great, but when it came to dialogs it found itself wanting. I tend to agree with this opinion. As a result most people ,including this author, use a <code>UITableView</code> to build custom forms.</p>

<p>QuickDialog aims to simplify this process, providing a framework that uses JSON documents to describe the dialog required. Once the JSON has been defined, the QuickDialog framework builds the <code>UITableView</code> with all the necessary controls. Additionally QuickDialog binds data between the view inputs and the model, so an update in either one will result in the other being updated. It was a great talk by Eduardo delivered with some excellent live demonstrations. I will certainly look into this framework in more detail.</p>

<p>After a long day of excellent talks I was ready for home. I decided to forego the after party and head straight home to start playing with some of the tools mentioned. Overall day was full of very insightful talks aimed at the iOS platform. The next day would be split between Android and Windows Mobile. The few negative factors mentioned at the beginning of this post did dampen the experience slightly, but the quality of the talks so far was helping to compensate for those minor issues. Given a better venue, this conference would have been excellent.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Refactoring the PhoneGap iOS DatePicker]]></title>
    <link href="http://def.reyssi.net/blog/2012/03/31/refactored-phonegap-ios-datepicker/"/>
    <updated>2012-03-31T10:00:00-05:00</updated>
    <id>http://def.reyssi.net/blog/2012/03/31/refactored-phonegap-ios-datepicker</id>
    <content type="html"><![CDATA[<p><img src="http://def.reyssi.net/images/datepicker.png" alt="iOS Date Picker" /></p>

<p>Recently my team were using <a href="http://phonegap.com/"><em>PhoneGap</em></a>, the HTML5 middleware that enables web developers to write mobile apps once using technologies they know; HTML5, CSS and Javascript. The completed PhoneGap/HTML5 application is then compiled into binaries for each of the <a href="http://phonegap.com/about/features">supported mobile platforms</a>. The idea is simple, developers write code once and deploy many times. PhoneGap can certainly be used for creating simple applications, but stray from the <a href="http://docs.phonegap.com/en/1.4.1/index.html">core API</a> and you quickly find yourself either writing a native extension; or more likely finding a plugin that does the task for you.</p>

<!-- more -->


<p>As part of a research project for my employer we were writing a mobile application using the PhoneGap platform. The application in question was a relatively simple concept, however it quickly became apparent we would have to go outside of the core API.</p>

<p>PhoneGap does not provide a standard way to create a date picker, instead delegating the vast majority of interface responsibilities to the application and/or framework chosen to author the application. In our case we were using <a href="http://jquerymobile.com/">JQuery Mobile</a> for the application user interface. There are others available, <a href="http://www.sencha.com/products/touch/">Sencha Touch</a> being another popular one from the creators of <a href="http://www.sencha.com/products/extjs/">ExtJS</a>.</p>

<p>Date input is a potentially complex operation on a mobile device. There are many components to a full date and making the user type it in is hardly the optimum interface. Of course there are many web forms that expect the user to do just that. But in the mobile context, there is a chance the operator will only be using one hand to interact with the device, so making them use a complex input control, such as a keyboard, would instantly have a negative effect on their experience.</p>

<p>Presenting the standard <a href="http://jquerymobile.com/demos/1.0a4.1/experiments/ui-datepicker/">JQuery Mobile Datepicker</a> or similar will equally frustrate the user. The JQuery Mobile Datepicker component is awful, but hardly the most efficient design. Also, PhoneGap gives the user the impression the app they are using is a native app, so they will most likely expect a native control in this situation. We could use an iOS DatePicker implementation in Javascript such as <a href="http://cubiq.org/spinning-wheel-on-webkit-for-iphone-ipod-touch">Cubiq&rsquo;s Spinning Wheel Plugin</a> to present a picker in the PhoneGap. But this ensures the picker will use an iOS interface even when used on an Android, Windows Mobile or one of the two remaining Blackberry devices. Another great way to ruin the user experience is to start presenting unfamiliar controls.</p>

<p>Luckily modern mobile operating systems have solved this problem by supplying a native Datepicker control. Using the native Datepicker control on each platform requires a native <a href="http://wiki.phonegap.com/w/page/36752779/PhoneGap%20Plugins">PhoneGap extension</a>. Naturally the first thing to do is look for an existing solution to this problem within the PhoneGap ecosystem. A quick search revealed that Datepicker extensions do exist for PhoneGap on <a href="https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/DatePicker">iOS</a> and <a href="https://github.com/phonegap/phonegap-plugins/tree/master/Android/DatePicker">Android</a>. After downloading the extension and plugging it into our PhoneGap application, the extension worked&hellip; but unfortunately it would only work the one time.</p>

<p>The application would display the <code>UIDatePicker</code> as expected, allowing the user to select a date and dismiss the action sheet. However when the date picker was summoned subsequently, it would not respond to the user input and the date selected was not returned to the application. It was time to look at the <a href="https://github.com/phonegap/phonegap-plugins/blob/59f3cf98c40ade9268c0546b7537ad769a17088e/iPhone/DatePicker/DatePicker.m">source code</a> to see if there was anything obvious going wrong. The source quickly revealed some issues that required urgent attention.</p>

<h2>Memory management</h2>

<p>First off there are some basic Objective C memory management violations within the DatePicker extension. The <code>DatePicker.h</code> header file defines <code>@property (nonatomic, retain) UIDatePicker *datePicker;</code> accessor method. Using this accessor for assignment will increment the reference count of the <code>UIDatePicker</code> object passed to it by one. If we examine the following code, we can see how the accessor is used within the DatePicker implementation.</p>

<figure class='code'><figcaption><span>DatePicker.m</span><a href='https://github.com/phonegap/phonegap-plugins/blob/59f3cf98c40ade9268c0546b7537ad769a17088e/iPhone/DatePicker/DatePicker.m#L34'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">self</span><span class="p">.</span><span class="n">datePicker</span> <span class="o">=</span> <span class="p">[[</span><span class="n">UIDatePicker</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithFrame:</span><span class="n">pickerFrame</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>alloc</code> method will allocate memory for the <code>UIDatePicker</code> object and increment the retain count by one. Assigning the resulting object to the <code>datePicker</code> instance variable via the accessor will increment the retain count a second time. Later in the implementation the <code>datePicker</code> property is released, decreasing the retain count. It is now <code>1</code> for those still counting. An object with a retain count greater that zero will be retained by the autorelease pool.</p>

<p>Memory management in Objective C is a vital concept to understand if you are going to write code any code. Even if you intend to use the new <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html">Automatic Reference Counting</a> features provided by Clang, I believe it&rsquo;s imperative to understand the fundamentals of memory management. I am not using ARC at the moment and do not intend to any time soon, the reasons behind this decision are probably worthy of another blog post at some other time. However there are three simple rules within the Objective C language for managing memory that, when followed, will see you good in the vast majority of situations;</p>

<blockquote><p>When creating a new object using either the;</p><p> 1. [alloc]<br/> 2. [new]<br/> 3. [copy] or derivatives, such as [mutableCopy]<br/> <br/>methods, then you are responsible for releasing that object when you are finished with it.</p></blockquote>


<p>The result of this oversight within the PhoneGap DatePicker extension is that the <code>UIDatePicker</code> object created in the code block shown above is never released, creating a memory leak. If there was only ever one instance of the <code>UIDatePicker</code> object then this would not be so much of an issue, although all memory leaks are bad. One leaking date picker is not going to consume vast amounts of memory. But this situation gets worse.</p>

<h2>Further inspection</h2>

<figure class='code'><figcaption><span>DatePicker.m</span><a href='https://github.com/phonegap/phonegap-plugins/blob/59f3cf98c40ade9268c0546b7537ad769a17088e/iPhone/DatePicker/DatePicker.m#L19-68'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">datePickerSheet</span> <span class="o">=</span> <span class="p">[[</span><span class="n">UIActionSheet</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithTitle:</span><span class="nb">nil</span> <span class="nl">delegate:</span><span class="nb">nil</span> <span class="nl">cancelButtonTitle:</span><span class="nb">nil</span> <span class="nl">destructiveButtonTitle:</span><span class="nb">nil</span> <span class="nl">otherButtonTitles:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="p">[</span><span class="n">datePickerSheet</span> <span class="nl">setActionSheetStyle:</span><span class="n">UIActionSheetStyleBlackTranslucent</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">CGRect</span> <span class="n">pickerFrame</span> <span class="o">=</span> <span class="n">CGRectMake</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="n">self</span><span class="p">.</span><span class="n">datePicker</span> <span class="o">=</span> <span class="p">[[</span><span class="n">UIDatePicker</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithFrame:</span><span class="n">pickerFrame</span><span class="p">];</span>
</span><span class='line'><span class="n">bool</span> <span class="n">allowOldDates</span> <span class="o">=</span> <span class="p">([[</span><span class="n">options</span> <span class="nl">objectForKey:</span><span class="s">@&quot;allowOldDates&quot;</span><span class="p">]</span> <span class="n">intValue</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span><span class="o">?</span><span class="nl">YES:</span><span class="n">NO</span><span class="p">;</span>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">allowOldDates</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">self</span><span class="p">.</span><span class="n">datePicker</span><span class="p">.</span><span class="n">minimumDate</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSDate</span> <span class="n">date</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">if</span> <span class="p">([</span><span class="n">mode</span> <span class="nl">isEqualToString:</span><span class="s">@&quot;date&quot;</span><span class="p">])</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">self</span><span class="p">.</span><span class="n">datePicker</span><span class="p">.</span><span class="n">datePickerMode</span> <span class="o">=</span> <span class="n">UIDatePickerModeDate</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">NSDateFormatter</span> <span class="o">*</span><span class="n">dateFormatter</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSDateFormatter</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">dateFormatter</span> <span class="nl">setTimeZone:</span><span class="p">[</span><span class="n">NSTimeZone</span> <span class="n">defaultTimeZone</span><span class="p">]];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">dateFormatter</span> <span class="nl">setDateFormat:</span><span class="s">@&quot;MM/dd/yyyy&quot;</span><span class="p">];</span>
</span><span class='line'>  <span class="n">NSDate</span> <span class="o">*</span><span class="n">date</span> <span class="o">=</span> <span class="p">[</span><span class="n">dateFormatter</span> <span class="nl">dateFromString:</span><span class="n">dateString</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">dateFormatter</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>  <span class="n">self</span><span class="p">.</span><span class="n">datePicker</span><span class="p">.</span><span class="n">date</span> <span class="o">=</span> <span class="n">date</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">else</span> <span class="k">if</span> <span class="p">([</span><span class="n">mode</span> <span class="nl">isEqualToString:</span><span class="s">@&quot;time&quot;</span><span class="p">])</span>
</span><span class='line'>  <span class="n">self</span><span class="p">.</span><span class="n">datePicker</span><span class="p">.</span><span class="n">datePickerMode</span> <span class="o">=</span> <span class="n">UIDatePickerModeTime</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="p">[</span><span class="n">datePickerSheet</span> <span class="nl">addSubview:</span><span class="n">self</span><span class="p">.</span><span class="n">datePicker</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>The code block above presents an abstract from the <code>DatePicker show:withDict:</code> method. This is the Objective C method the PhoneGap Javascript plugin is bound to. Each time a HTML control is selected that invokes the <code>show:withDict:</code> method, the code above is executed.</p>

<p>To provide some context, PhoneGap plugins in Objective C are lazily instantiated, thus loaded into memory only when they are first called. This is quite a nice feature. However PhoneGap relies on the extension code playing nicely and tiding up after itself when it is finished working, or if the operating system warns of low memory. PhoneGap calls the <code>onMemoryWarning</code> method within the extension if the operating system is running low on memory, allowing the plugin to release objects superfluous to requirements at that moment. No such luck here. It wouldn&rsquo;t be so bad if this extension was not leaking memory.</p>

<p>Returning to the code shown above. The <code>DatePicker show:withDict:</code> method is being invoked by PhoneGap when the plugins Javascript counterpart is executed. Each time this method is executed a new <code>UIDatePicker</code> is created and assigned to the plugin and action sheet. Because the previous <code>UIDatePicker</code> is not released and also not removed from the Action Sheet view, the new instance is placed over the existing instance. The new instance <em>should</em> always be placed on top, but who knows what happens when there are several <code>UIDatePicker</code> controls layered up on top of each other. This begins to explain the strange behaviour experienced earlier where repeated use of the <em>DatePicker</em> extension resulted in it not responding or returning the correct date.</p>

<h2>Delegates and Single Responsibility Principle</h2>

<p>One other implementation detail was also causing concern. The <code>UIActionSheet</code> class has a <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html">delegate protocol</a>. The Cocoa frameworks use the delegate pattern consistently across many user interface controls. Unfortunately the <em>DatePicker</em> extension does not use the provided delegates.</p>

<figure class='code'><figcaption><span>DatePicker.m</span><a href='https://github.com/phonegap/phonegap-plugins/blob/59f3cf98c40ade9268c0546b7537ad769a17088e/iPhone/DatePicker/DatePicker.m#L70-77'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">dismissActionSheet:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">sender</span> <span class="p">{</span>
</span><span class='line'>  <span class="p">[</span><span class="n">datePickerSheet</span> <span class="nl">dismissWithClickedButtonIndex:</span><span class="mi">0</span> <span class="nl">animated:</span><span class="n">YES</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">datePickerSheet</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">datePicker</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>  <span class="n">NSString</span><span class="o">*</span> <span class="n">jsCallback</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSString</span> <span class="nl">stringWithFormat:</span><span class="s">@&quot;window.plugins.datePicker._dateSelected(</span><span class="se">\&quot;</span><span class="s">%i</span><span class="se">\&quot;</span><span class="s">);&quot;</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)[</span><span class="n">self</span><span class="p">.</span><span class="n">datePicker</span><span class="p">.</span><span class="n">date</span> <span class="n">timeIntervalSince1970</span><span class="p">]];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">super</span> <span class="nl">writeJavascript:</span><span class="n">jsCallback</span><span class="p">];</span>
</span><span class='line'>  <span class="n">isVisible</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>dismissActionSheet:</code> method is invoked by a <code>UISegmentedControl</code> button defined earlier in the class. When the user selects the <em>Close</em> button, this method is invoked. But this method is performing a lot of actions, most are outside the scope of the method name. The current responsibilities of this method are;</p>

<ol>
<li>Remove the <code>UIActionSheet</code> from the interface</li>
<li>Free the <code>UIActionSheet</code> from memory</li>
<li>Free the <code>UIDatePicker</code> from memory (although we saw earlier this won&rsquo;t happen)</li>
<li>Return the date picked to the PhoneGap API</li>
<li>Update the state of the DatePicker extension</li>
</ol>


<p>That is a lot of responsibility for a simple UI action. A method named <code>dismissActionSheet:</code> should do just that, dismiss the action sheet. Therefore to maintain the single responsibility principle this method should only perform the action it prescribes.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">dismissActionSheet:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">sender</span> <span class="p">{</span>
</span><span class='line'>  <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">datePickerSheet</span> <span class="nl">dismissWithClickedButtonIndex:</span><span class="mi">0</span> <span class="nl">animated:</span><span class="n">YES</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The new implementation of <code>dismissActionSheet:</code> solely performs the action requested of it, namely removing the <code>UIActionSheet</code> from the interface. But in the process a lot of the other functionality previously performed has been lost. Returning to the list of responsibilities above, there are three tasks that are important to the functionality of this extension. (Memory management of UI elements should not be handled here)</p>

<ol>
<li><strong>Return the date picked to the PhoneGap API</strong></li>
<li><strong>Update the state of the DatePicker extension</strong></li>
<li>Remove the <code>UIActionSheet</code> from the interface</li>
</ol>


<p>Having already removed the <code>UIActionSheet</code> from the interface, only the first two tasks remain.</p>

<p>The first task is to return the date picked to the PhoneGap API. Because a value is being transmitted from the one platform (Objective C) to another (JS), there is potential for things to go wrong. It is probably best to send the value to the PhoneGap API before removing the <code>UIActionSheet</code>. The <code>UIActionSheetDelegate</code> class provides a method <code>actionSheet:willDismissWithButtonIndex:</code> that is invoked prior to the <code>UIActionSheet</code> being dismissed.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">actionSheet:</span><span class="p">(</span><span class="n">UIActionSheet</span> <span class="o">*</span><span class="p">)</span><span class="nv">actionSheet</span> <span class="nf">willDismissWithButtonIndex:</span><span class="p">(</span><span class="n">NSInteger</span><span class="p">)</span><span class="nv">buttonIndex</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">NSString</span><span class="o">*</span> <span class="n">jsCallback</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSString</span> <span class="nl">stringWithFormat:</span><span class="s">@&quot;window.plugins.datePicker._dateSelected(</span><span class="se">\&quot;</span><span class="s">%i</span><span class="se">\&quot;</span><span class="s">);&quot;</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)[</span><span class="n">self</span><span class="p">.</span><span class="n">datePicker</span><span class="p">.</span><span class="n">date</span> <span class="n">timeIntervalSince1970</span><span class="p">]];</span>
</span><span class='line'>  <span class="p">[</span><span class="n">super</span> <span class="nl">writeJavascript:</span><span class="n">jsCallback</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The date selected by the user is now passed to the PhoneGap API as the <code>UIActionSheet</code> is being dismissed by the system. If there are any problems passing the value back to PhoneGap, an alert can be provided before the interface is dismissed. (Ideally, there would be a <code>UIActionSheetDelegate</code> method for cancelling the dismiss; <code>actionSheet:shouldDismissWithButtonIndex:</code> for example. Unfortunately no such luck here)</p>

<p>The final task is to update the state of the extension. As the extension will hang around after the user has dismissed it, the state will be important for managing memory and components so it is worth keeping. Again the <code>UIActionSheetDelegate</code> protocol provides the perfect method for updating the state, <code>actionSheet:didDismissWithButtonIndex:</code>.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">actionSheet:</span><span class="p">(</span><span class="n">UIActionSheet</span> <span class="o">*</span><span class="p">)</span><span class="nv">actionSheet</span> <span class="nf">didDismissWithButtonIndex:</span><span class="p">(</span><span class="n">NSInteger</span><span class="p">)</span><span class="nv">buttonIndex</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">isVisible</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>All of the original functionality tied to the <em>Close</em> button has now been refactored into new methods that use the standard Objective C and Cocoa patterns. Additionally, the single responsibility principle has been maintained.</p>

<h2>The refactored DatePicker extension and PhoneGap</h2>

<p>There are many more improvements that can be made to this DatePicker extension for PhoneGap. It is easy to critique others code, but it is far more valuable to give back. As we had to fix the issues detailed here and a number of others, we pushed the changes back to GitHub and requested they were <a href="https://github.com/phonegap/phonegap-plugins/pull/363">pulled</a> into the project. To the credit of the PhoneGap plugins project, they <a href="https://github.com/phonegap/phonegap-plugins/commit/d07bc00fc3ce05df85dafd25bd1da8108e521fa9">merged the changes in</a> promptly.</p>

<p>There are no doubt other improvements that can be made to the DatePicker extension, however those improvements will not be completed by myself or the team. After attempting to build our application in PhoneGap, we ultimately abandoned the project and reverted to native development on iOS and Android.</p>

<p>Unfortunately PhoneGap promises a lot, but does not always deliver on these promises. Additionally many of the <em>official</em> extensions are implemented to a similar standard as the <em>DatePicker</em> extension examined here. The questionable quality extends beyond iOS to Android as well, as different but equally crippling issues were found with the Android <em>DatePicker</em> extension. I am sure that PhoneGap has a place in the mobile development space, but beyond basic brochure applications I am not entirely sure what it is.</p>

<h2>Update</h2>

<p>After this was initially posted, people have pointed out that HTML5 does provide a <a href="http://www.w3.org/TR/html5/states-of-the-type-attribute.html#date-and-time-state-type-datetime">datetime input type</a>. This would obviously provide a better way to perform all of the above within the browser context. The support is still rather patchy, so keep an eye on PPK&rsquo;s excellent work <a href="http://www.quirksmode.org/html5/inputs_mobile.html">testing HTML5 support</a> across all mobile devices. As with all things in the mobile web, support for <code>&lt;input type="datetime"&gt;&lt;/input&gt;</code> can only improve with time.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[DNS woes]]></title>
    <link href="http://def.reyssi.net/blog/2012/03/26/dns-woes/"/>
    <updated>2012-03-26T22:11:00-05:00</updated>
    <id>http://def.reyssi.net/blog/2012/03/26/dns-woes</id>
    <content type="html"><![CDATA[<p><img src="http://assets.diylol.com/hfs/2b6/858/748/resized/x-all-the-things-meme-generator-kill-all-the-servers-1abf33.jpg" title="Kill all the servers" ></p>

<p>Just a quick note to say that I have been experiencing some DNS woes with this domain. As a result, many of you have probably been seeing a far from beautiful <code>404 Not Found</code> served by my former hosting company. Initially I thought this was an issue with my <a href="http://heroku.com">Heroku</a> web dynos. However after doing some further DNS analysis and investigation I discovered that I had what can only be described as <em>DNS EpicFAIL</em>!</p>

<!-- more -->


<p>When this site was on the previous host it was hosting its own DNS server using <a href="http://www.isc.org/software/bind">Bind</a>. Because there was only one piece of bare metal there needed to be a backup DNS server hosted elsewhere. At the time I decided to use a free service offered by a company based out of California. I will not advertise them here, not through lack of service but to ensure their offerring is not abused by the masses. I setup the secondary DNS host to be a slave to the primary Bind DNS server hosted on my bare metal. This all worked beautifully for several years.</p>

<p>Fast forward to 2012 and the <a href="http://def.reyssi.net/blog/2012/01/14/hello-2012/">decision is made</a> to move to another host. Heroku is a wonderful service, but they don&rsquo;t do DNS within their core offering. There are <a href="http://addons.heroku.com">addons</a> that can provide DNS services, but I wanted to have more control than those services offered. In the end I opted to move to <a href="http://www.zonomi.com">Zonomi</a> as they offered the service required at a very resonably price.</p>

<p>After exporting the original Bind zone configration and importing it correctly into the new DNS server, I then updated the secondary slave DNS servers to pull from the new primary servers hosted by Zonomi. This is where the trouble started. I did not hang around to check if the secondary DNS host was able to pull the zone information from Zonomi using the <a href="http://en.wikipedia.org/wiki/DNS_zone_transfer">AXFR protocol</a>. As it turns out, Zonomi disallow this presently. I&rsquo;m not sure why but will certainly investigate. As the new DNS zone information was not updated at my secondary DNS host, the original data remained pointing at my old servers.</p>

<p>Over the last few months I have noticed that this site has intermittently been unvailable some of the time, all of the time or not at all. Initially I thought this was due to Heroku spinning down the web dynos on the free account I started with. However after spinning up a second dyno and still experiencing the issue, it became obvious the issue lie elsewhere. I noticed that <a href="http://www.pingdom.com">Pingdom</a> would report this site as being down even when I could navigate to it without issue. Currently I do not serve Cache-Control headers (coming soon), so it was not a stale cache persisting in a proxy. It was time to <a href="http://linux.die.net/man/1/dig">dig</a> a little into this issue.</p>

<figure class='code'><figcaption><span>Dig Report at Home </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>dig def.reyssi.net
</span><span class='line'>
</span><span class='line'>; &lt;&lt;>> DiG 9.7.3-P3 &lt;&lt;>> def.reyssi.net
</span><span class='line'>;; global options: +cmd
</span><span class='line'>;; Got answer:
</span><span class='line'>;; ->>HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 51223
</span><span class='line'>;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
</span><span class='line'>
</span><span class='line'>;; QUESTION SECTION:
</span><span class='line'>;def.reyssi.net.          IN  A
</span><span class='line'>
</span><span class='line'>;; ANSWER SECTION:
</span><span class='line'>def.reyssi.net.       599169  IN  A   46.105.102.142
</span><span class='line'>
</span><span class='line'>;; Query time: 4 msec
</span><span class='line'>;; SERVER: 10.0.1.1#53(10.0.1.1)
</span><span class='line'>;; WHEN: Mon Mar 26 22:36:25 2012
</span><span class='line'>;; MSG SIZE  rcvd: 48</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>Dig Report at Work </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>dig def.reyssi.net   
</span><span class='line'>
</span><span class='line'>; &lt;&lt;>> DiG 9.7.3-P3 &lt;&lt;>> def.reyssi.net
</span><span class='line'>;; global options: +cmd
</span><span class='line'>;; Got answer:
</span><span class='line'>;; ->>HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 65009
</span><span class='line'>;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 4, ADDITIONAL: 0
</span><span class='line'>
</span><span class='line'>;; QUESTION SECTION:
</span><span class='line'>;def.reyssi.net.          IN  A
</span><span class='line'>
</span><span class='line'>;; ANSWER SECTION:
</span><span class='line'>def.reyssi.net.       1851    IN  A   75.101.163.44
</span><span class='line'>def.reyssi.net.       1851    IN  A   174.129.212.2
</span><span class='line'>def.reyssi.net.       1851    IN  A   75.101.145.87
</span><span class='line'>
</span><span class='line'>;; AUTHORITY SECTION:
</span><span class='line'>reyssi.net.       171051  IN  NS  ns1.zonomi.com.
</span><span class='line'>reyssi.net.       171051  IN  NS  ns2.zonomi.com.
</span><span class='line'>reyssi.net.       171051  IN  NS  ns3.zonomi.com.
</span><span class='line'>reyssi.net.       171051  IN  NS  ns4.zonomi.com.
</span><span class='line'>
</span><span class='line'>;; Query time: 102 msec
</span><span class='line'>;; SERVER: 193.26.222.2#53(193.26.222.2)
</span><span class='line'>;; WHEN: Mon Mar 26 22:39:12 2012
</span><span class='line'>;; MSG SIZE  rcvd: 162</span></code></pre></td></tr></table></div></figure>


<p>As is very clear, the two records are quite different. The DNS servers that my work uses have the correct zone information for this domain. However my ISP for home seems to be using the record hosted by the secondary DNS server. Because the old secondary server is failing to update correctly, the zone information is sparse at best. It appears that different DNS nameservers were choosing to use different zone records hosted either at Zonomi or the secondary host. To fix the issue I have retired the secondary DNS service as Zonomi offer four separate nameservers.</p>

<p>Hopefully all these DNS woes will be behind me now. It will take around 72 hours for all the DNS changes to propagate across the network, so stability is a little way off. But hopefully <em>everyone</em> should be able to read this by the weekend.</p>

<p><em>Kill All The Servers image courtesy of <a href="http://diylol.com/meme-generator/all-the-things/memes/kill-all-the-servers">DIY LOL</a> &ndash; inspired by <a href="http://phpir.com/">Ian Barber&rsquo;s</a> Skype avatar</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Adding OCMock to Xcode 4 projects]]></title>
    <link href="http://def.reyssi.net/blog/2012/03/23/using-ocmock-with-xcode4/"/>
    <updated>2012-03-23T18:20:00-05:00</updated>
    <id>http://def.reyssi.net/blog/2012/03/23/using-ocmock-with-xcode4</id>
    <content type="html"><![CDATA[<p>Today I have been adding some new functionality to an iPhone application I am currently developing for <a href="http://www.sittercity.com">work</a>. Being the good developer that I am (<em>Ed</em>: and modest too), I started by writing some tests to cover the changes I needed to make. These days Xcode ships with the <a href="http://www.sente.ch/software/ocunit/">SenTest</a> Objective C unit testing framework, which is great. But SenTest does not include a mocking framework. Fortunately <a href="http://ocmock.org/">OCMock</a> exists to solve this very problem. However getting it into your iOS project and running takes a little bit of effort.</p>

<!-- more -->


<p>First of all grab the latest copy of the OCMock dmg from <a href="http://ocmock.org#download">http://ocmock.org#download</a>, <em>version 2.0.1</em> at the time of writing.
Next mount the disk image <code>ocmock-2.0.1.dmg</code> (version number may be different) so that
the directory structure of the mounted disk image should resemble the image below
   <img src="http://def.reyssi.net/images/OCMock.png" title="[OCMock directory structure]" ></p>

<p>At this point the OCMock library is ready to be added to your project. The disk image provided supplies linked libraries for iOS and Mac OS X, as well as the source. Naturally the project source is also available on <a href="http://github.com/erikdoe/ocmock">Github</a>. Be warned, the Github source will require compiling before it can be used in your own tests. If you are not comfortable with that concept, it is best just to use the precompiled binaries.</p>

<p>Before importing the OCMock library into your Xcode 4 project, create a folder for this library name something similar to <code>$(SRCROOT)/tests/libraries</code>. <code>$(SRCROOT)</code> is xcode shorthand for the project root folder for anyone scratching their head. In Xcode create a group within the <code>tests</code> group called <code>libraries</code> to mirror the layout of the filesystem. Now drag the contents of the OCMock <em>iOS</em> folder on the mounted drive to the new <code>libraries</code> group within Xcode created previously.</p>

<p>Xcode <em>might</em> be clever and realize what has just happened at this point and added <code>libOCMock.a</code> to your tests targets <em>Link Binary With Libraries</em> build phase. If it has not, then you can drag <code>libOCMock.a</code> from your filetree into the <em>Link Binary With Libraries</em> build phase to add it. Before continuing we need to ensure that Xcode is including the library and headers when the tests are run. Within the test target, check the <em>Build Settings</em> entry for <em>Header Search Paths</em> and <em>Library Search Paths</em> includes <code>$(SRCROOT)/tests/libraries</code>.</p>

<p>Finally and most <strong>importantly</strong> Xcode needs to tell the compiler to force load the OCMock libraries at compile time, otherwise your tests will crash. Within the <em>Build Settings</em> in your test build target add the following line to the <em>Other Linker Flags</em> entry; <code>-force_load $(SRCROOT)/tests/libraries/libOCMock.a</code>.</p>

<p>OCMock is now ready for use and bar any other unforseen problems, testing should be uneventful. The last step of instructing the compilier to force load the library caused me to loose 30 minutes this afternoon, so hopefully this will help anyone else having similar issues.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dutch Mobile Conference 2012]]></title>
    <link href="http://def.reyssi.net/blog/2012/02/29/dutch-mobile-conference-2012/"/>
    <updated>2012-02-29T20:56:00-06:00</updated>
    <id>http://def.reyssi.net/blog/2012/02/29/dutch-mobile-conference-2012</id>
    <content type="html"><![CDATA[<p><a href="http://mobileconference.nl"><img src="http://def.reyssi.net/images/dmc_wide_speaker.png" alt="Dutch Mobile Conference" /></a></p>

<p>I am delighted to announce that I&rsquo;ll be speaking at the <a href="http://mobileconference.nl">Dutch Mobile Conference</a> in June 2012. I will be presenting <em>Ten considerations for taking a web business to the mobile market</em> on the Friday. The talk will provide attendees with ten useful considerations for taking an existing online business into treacherous world of mobile. Moving an established online business into the mobile domain sounds simple to say. In some cases, it can be simple to perform. But the reality for a lot of companies is that their present model does not necessarily work within a mobile context.</p>

<!-- more -->


<p>It is always a pleasure to visit Amsterdam, a city that Siân and I enjoy visiting as often as possible. I will be staying on in Europe for a week after the conference to visit friends and family back in London. June cannot come soon enough for me.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Get blogging with Octopress on Heroku]]></title>
    <link href="http://def.reyssi.net/blog/2012/01/14/get-blogging-with-octopress-on-heroku/"/>
    <updated>2012-01-14T20:01:00-06:00</updated>
    <id>http://def.reyssi.net/blog/2012/01/14/get-blogging-with-octopress-on-heroku</id>
    <content type="html"><![CDATA[<p>Following on from <a href="http://def.reyssi.net/blog/2012/01/14/hello-2012/">my opening post</a>, one of the main hurdles preventing me from blogging was the setup of a blogging platform and the management of the hosting. Thankfully, <a href="http://octopress.org">Octopress</a> and <a href="http://heroku.com">Heroku</a> have provided a simple, effective and free way for me to publish my thoughts to the web. In this post I will explain how I got started with Octopress on Heroku.</p>

<!-- More -->


<h2>Setting up your development environment</h2>

<p>First off you are going to require <strong>Ruby 1.9.2</strong>. At this point your natural temptation may be to <a href="http://www.ruby-lang.org/en/downloads/">download and compile the source code</a> directly. This will work for the most part, but any seasoned Ruby developer will advice you against this path.</p>

<p>When doing any serious Ruby development you most likely will be working on numerous projects that use a variety of different frameworks, each with their own specific Ruby and <a href="http://rubygems.org/">Gem</a> version requirements. Managing multiple versions of Ruby and Gems manually is a rather daunting task.</p>

<p>Thankfully this problem has been solved already by <a href="http://beginrescueend.com/rvm/install/">RVM</a> and <a href="https://github.com/sstephenson/rbenv">Rbenv</a>. Both RVM and Rbenv provide an isolated Ruby environment, encapsulating Ruby and Gems to specific versions. This allows each Ruby project on your system to run a targeted version of Ruby and Gems per project without concerning yourself with conflicts. I am not going to advocate either of these tools over each other, but I will be using RVM in this article. There are instructions on using Rbenv on the Octopress <a href="http://octopress.org/docs/setup/">setup guide</a>.</p>

<p>To install <strong>RVM</strong>, in Terminal run;</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ bash &lt; &lt;(curl -s https://rvm.beginrescueend.com/install/rvm)</span></code></pre></td></tr></table></div></figure>


<p>Once <strong>RVM</strong> has finished installing, you should add it to your shell as a command. Run the following command to update your <code>.bash_profile</code>.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' &gt;&gt; ~/.bash_profile</span></code></pre></td></tr></table></div></figure>


<p>Finally you should update your Terminal sessions <code>.bash_profile</code> to load the new profile settings.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ source ~/.bash_profile</span></code></pre></td></tr></table></div></figure>


<p>Now you&rsquo;re ready to install Ruby 1.9.2.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ rvm install 1.9.2 && rvm use 1.9.2</span></code></pre></td></tr></table></div></figure>


<p>This command installs Ruby 1.9.2 as a Gem into your <code>~/.rvm/gems/</code> folder. Now within each Ruby application, you can create a <code>.rvmrc</code> file that defines the Gem sets required by the project. For example, Octopress instructs <em>RVM</em> to use Ruby 1.9.2 by including the following in its <code>.rvmrc</code> file.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>rvm use 1.9.2</span></code></pre></td></tr></table></div></figure>


<h2>Obtaining Octopress</h2>

<p>The best way of getting Octopress is directly from <a href="http://github.com//imathis/octopress">Github</a>. This will require your system to have <a href="http://git-scm.com">Git</a> installed. By cloning the Octopress project, obtaining updates in the future will be as easy as <code>git pull origin master</code>. However you may prefer to fork the project on Github and then clone from your own repository. In this case, I am happy with the standard build of Octopress, so will clone it directly.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ git clone http://github.com/imathis/octopress.git ~/Projects/octopress</span></code></pre></td></tr></table></div></figure>


<p>This will place an instance of Octopress into my <code>~/Projects/octopress</code> folder. You may wish to place it elsewhere, it does not matter where at this time.</p>

<p>Now change directory into the newly created local Git repository and check the Ruby version.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ cd ~/Projects/octopress
</span><span class='line'>$ ruby --version
</span><span class='line'>ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.2.0]</span></code></pre></td></tr></table></div></figure>


<p>You should see the Ruby version you previously installed, the exact version number and details will differ depending on your platform and other environment variables. The Ruby environment is controlled by the RVM  <code>~/Projects/octopress/.rvmrc</code> file I mentioned earlier.</p>

<p>Next we install <a href="http://gembundler.com/">Bundler</a> into this project.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ gem install bundler
</span><span class='line'>$ bundler install</span></code></pre></td></tr></table></div></figure>


<p>Taken from its own website, Bundler provides the following functionality;</p>

<blockquote><p>Bundler manages an application&#8217;s dependencies through its entire life across many machines systematically and repeatably.</p></blockquote>


<p>When the <code>bundler install</code> command is invoked, it reads the required list of Gems from the projects <code>Gemfile</code>. If you were inspect the <code>Gemfile</code> for Octopress, you can see the project dependencies.</p>

<p>Finally, to install the default Octopress theme run the following command.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ rake install</span></code></pre></td></tr></table></div></figure>


<p>This will setup your local instance of Octopress with the standard theme. In my instance this command failed with the following message.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ rake install
</span><span class='line'>rake aborted!
</span><span class='line'>You have already activated rake 0.9.2.2, but your Gemfile requires rake 0.9.2. Using bundle exec may solve this.
</span><span class='line'>
</span><span class='line'>(See full trace by running task with --trace)</span></code></pre></td></tr></table></div></figure>


<p>In some circumstances this will happen if you have a newer version of rake installed for whatever reason. Usually the Gemfile and Bundler will resolve these issues, but because both <code>rake 0.9.2</code> and <code>rake 0.9.2.2</code> are matched in the Gemfile this conflict is not resolved. However in this case, following prescribed advice provides the expected outcome.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ bundle exec rake install
</span><span class='line'>## Copying classic theme into ./source and ./sass</span></code></pre></td></tr></table></div></figure>


<p>Octopress is now installed locally and is ready for you to begin blogging. You can use a plethora of hosting services with Octopress. As Octopress compiles a static version of your site, it would be perfectly possible just to publish the <code>public/</code> folder to any standard HTTP server such as Apache or Nginx.</p>

<p>Because the contents of <code>public/</code> folder is compiled by Octopress, by default the Git clone of the project will contain a <code>.gitignore</code> file with an entry to ignore the <code>public</code> folder. We need to remove this entry to ensure the <code>public/</code> folders contents are included in our git repository and pushed upstream.</p>

<p>To remove the entry, edit the <code>.gitignore</code> file remove the offending line.</p>

<h2>Deployment to Heroku</h2>

<p><img src="http://f.cl.ly/items/0A0C3C1V173U2j0q213S/Image%202012.01.14%2021:22:21.png" title="Heroku Homepage" ></p>

<p>Deployment to Heroku requires an account on Heroku. If you do not have an existing Heroku account, <a href="https://api.heroku.com/signup">sign up</a> for one now.</p>

<p>I am discovering that as with most things in Ruby, there is usually a Ruby Gem tailored to working with whatever Ruby service you require. Heroku is no different.</p>

<p>To install the Heroku Gem.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ gem install heroku</span></code></pre></td></tr></table></div></figure>


<p>Now we have the Heroku Gem available, we can create our Heroku app. This does require an existing Heroku account, so if you still have not signed up with the Heroku service, now really is the time!</p>

<p><strong>Note:</strong> To proceed you will need to have a SSH public key. Github have a <a href="http://help.github.com/set-up-git-redirect/">great guide</a> if you require help.</p>

<p>First we need to tell Heroku about our account. Using the <code>heroku login</code> command, we can authenticate ourselves with the Heroku service.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ heroku login
</span><span class='line'>Enter your Heroku credentials.
</span><span class='line'>Email: &lt;your email address&gt;
</span><span class='line'>Password: &lt;your password&gt;
</span><span class='line'>Found existing public key: /Users/&lt;your user&gt;/.ssh/id_rsa.pub
</span><span class='line'>Uploading ssh public key /Users/&lt;your user&gt;/.ssh/id_rsa.pub</span></code></pre></td></tr></table></div></figure>


<p>If successful, the <code>heroku login</code> command will upload our SSH public key automatically to the Heroku service. The reason for this is that you publish to Heroku using Git, and just like any other standard Git remote repository, connection is made over SSH.</p>

<p>Next we create an application on Heroku that will host this blog.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ heroku create
</span><span class='line'>Creating smooth-beach-2208... done, stack is bamboo-mri-1.9.2
</span><span class='line'>http://smooth-beach-2208.heroku.com/ | git@heroku.com:smooth-beach-2208.git</span></code></pre></td></tr></table></div></figure>


<p>Well we have an app, but I don&rsquo;t like the name. Heroku will generate a random name for applications if no name is supplied. After doing some research, I discovered you can provide a name, thus <code>heroku create defreyssinet</code> would result in an application located at <code>defreyssinet.heroku.com</code>. However it is not a disaster as we can rename any Heroku application after creating it.</p>

<p>To rename our app, within the <code>~/Projects/octopress</code> folder type.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ heroku rename defreyssinet
</span><span class='line'>http://defreyssinet.heroku.com/ | git@heroku.com:defreyssinet.git
</span><span class='line'>Git remote heroku updated</span></code></pre></td></tr></table></div></figure>


<p>This has updated the hostname on Heroku and rather neatly tidied up our local Git repository remote entries. It is possible to do this manually by logging on to the Heroku site and renaming the app there, then updating the local git repository remote entries.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ git rm heroku
</span><span class='line'>$ git remote add heroku git@heroku.com:defreyssinet.git</span></code></pre></td></tr></table></div></figure>


<p>At this point we have a Heroku app linked to our local Octopress Git repository. We can verify that our app has indeed been created by logging onto Heroku and selecting the <em>My Apps</em> menu item.</p>

<p><img src="http://f.cl.ly/items/3c1p3l021j2w0V011Y1I/Image%202012.01.14%2022:52:15.png" title="Heroku Applications" ></p>

<p>Now we have a local instance of Octopress and a host ready to accept our new blog. What is lacking is the content.</p>

<h2>Creating a new post</h2>

<p>As I mentioned earlier, Octopress is a static site generator rather than a fully fledged web application like <em>Wordpress</em> or <em>Habari</em>. This implies that there is a specific workflow in relation to publishing using Octopress. The typical publication flow for an article is as follows;</p>

<ol>
<li>Create an article &ndash; <code>rake new_post["title"]</code></li>
<li>Write some content</li>
<li>Review &ndash; <code>rake generate</code></li>
<li><em>If more work required, return to step 2.</em></li>
<li>Publish &ndash; <code>git push heroku &lt;your app&gt;</code></li>
</ol>


<p>Before we get ahead of ourselves, it is time to setup the configuration of the blog using the <code>_config.yaml</code> file found in the root of the project. All of the configuration settings are described in detail on the <a href="http://octopress.org/docs/configuring/">Configuring Octopress</a> page. Before creating any content it is probably worth familiarizing and editing this file to your specific needs.</p>

<p>Creating an article simply uses the <code>rake new_post["title"]</code> task. This creates a new file in the <code>~/Projects/octopress/source/_posts/</code> folder with a name composed of the date and the title. The extension will be <code>.markdown</code> as we will be writing in <a href="http://daringfireball.net/projects/markdown/">Markdown</a> format.</p>

<p><strong>Note:</strong> We will only be working in the <code>source/</code> folder and we <em>must</em> not touch the <code>public/</code> folder. The <code>rake generate</code> task compiles the contents of <code>source/</code> and places it into <code>public/</code>.</p>

<p>So lets go ahead and create an article. (<em>I&rsquo;m prefixing with <code>bundle exec</code> here to avoid the version complaint seen earlier.</em>)</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>bundle exec rake new_post["Get blogging with Octopress on Heroku"]</span></code></pre></td></tr></table></div></figure>


<p>Rake helpfully creates a new Markdown file called <code>2012-01-14-get-blogging-with-octopress-on-heroku</code> in the <code>source/_posts</code> folder. Opening up the new file will reveal the following.</p>

<figure class='code'><figcaption><span>source/_posts/2012-01-14-get-blogging-with-octopress-on-heroku</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>---
</span><span class='line'>layout: post
</span><span class='line'>title: "Get blogging with Octopress on Heroku"
</span><span class='line'>date: 2012-01-14 12:24:32
</span><span class='line'>comments: true
</span><span class='line'>categories:
</span><span class='line'>---</span></code></pre></td></tr></table></div></figure>


<p>The header of this file contains some <a href="https://github.com/mojombo/jekyll/wiki/yaml-front-matter">yaml front matter</a>. There are additional options available to control the publication state and author. <del>Personally I think the time of publication is less important, but this is easily fixed by removing the time index from the <code>date:</code> entry</del> (<strong>Edit:</strong> don&rsquo;t do this as it will mess up article ordering!). I have also turned off comments <code>comments: false</code> for this post.</p>

<p>It is now possible to add some content to this page in Markdown format. We don&rsquo;t need a title as the filename will become the title, so don&rsquo;t start with a <code># my  title</code> block.</p>

<figure class='code'><figcaption><span>source/_posts/2012-01-14-get-blogging-with-octopress-on-heroku</span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>---
</span><span class='line'>layout: post
</span><span class='line'>title: "Get blogging with Octopress on Heroku"
</span><span class='line'>date: 2012-01-14
</span><span class='line'>comments: false
</span><span class='line'>categories: [blogging, heroku]
</span><span class='line'>---
</span><span class='line'>
</span><span class='line'>Following on from [my opening post](http://def.reyssi.net/blog/2012/01/14/hello-2012/), one of the main hurdles preventing me from blogging was the setup of the blog platform and the management of the hosting. Thankfully, [Octopress](http://octopress.org) and [Heroku](http://heroku.com) have provided a simple, effective and free way for me to publish my thoughts to the web. In this post I will explain how I got started with Octopress on Heroku.
</span><span class='line'>
</span><span class='line'>...</span></code></pre></td></tr></table></div></figure>


<p>With some content in place, we can generate our first blog post using the <code>rake generate</code> task.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ bundle exec rake generate
</span><span class='line'>## Generating Site with Jekyll
</span><span class='line'>unchanged sass/screen.scss
</span><span class='line'>Configuration from /Users/sdefreyssinet/Projects/octopress/_config.yml
</span><span class='line'>Building site: source -&gt; public
</span><span class='line'>Successfully generated site: source -&gt; public</span></code></pre></td></tr></table></div></figure>


<p>At this point Octopress has compiled the static version of our site. It is possible to load the <code>public/index.html</code> folder into any browser to view our new content.</p>

<p>If like me you are using Mac OS X, you can use the <a href="http://pow.cx/">POW</a> zero-configuration rack server from <a href="http://37signals.com/">37signals</a> to host your site locally.</p>

<p>With POW installed, the following commands will create a local web server hosting our Octopress site.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ cd ~/.pow
</span><span class='line'>$ ln -s ~/Projects/octopress octopress</span></code></pre></td></tr></table></div></figure>


<p>Now it is simply a case of navigating to <a href="http://octopress.dev">http://octopress.dev</a> to see the site we just compiled. This is a really quick and simple way of reviewing changes.</p>

<p><em>Users on other platforms that wish to test in a server environment locally just need to host their local <code>public/</code> folder.</em></p>

<p>Previewing the site should present a page that looks similar to the screenshot below.</p>

<p><img src="http://f.cl.ly/items/0a392t0q132I340W2s2Z/Image%202012.01.15%2000:43:09.png" title="'Our first post hosted by POW'" ></p>

<p>Looking good. At this point we can carry on editing and reviewing until we&rsquo;re happy the post is ready. Remember that each time a change is made, an additional <code>rake generate</code> task needs to be invoked before any changes will be available in the <code>public/</code> folder.</p>

<h2>Publishing to Heroku</h2>

<p>The first post is completed, reviewed and ready for publication. This is where Heroku makes our lives very easy. First we need to check all of our changes into the local Git repository.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ git add .
</span><span class='line'>$ git commit -am 'Initial commit with our first post, get blogging with octopress on heroku'</span></code></pre></td></tr></table></div></figure>


<p>With the local repository up to date, we can push the repository to Heroku. Once the repository is pushed to Heroku it will be available <em>immediately</em>, so be sure you&rsquo;re <em>really</em> ready to publish before pushing upstream.</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ git push heroku master
</span><span class='line'>Counting objects: 77, done.
</span><span class='line'>Delta compression using up to 8 threads.
</span><span class='line'>Compressing objects: 100% (39/39), done.
</span><span class='line'>Writing objects: 100% (49/49), 4.80 KiB, done.
</span><span class='line'>Total 49 (delta 26), reused 0 (delta 0)
</span><span class='line'>
</span><span class='line'>-----&gt; Heroku receiving push
</span><span class='line'>-----&gt; Ruby/Sinatra app detected
</span><span class='line'>-----&gt; Gemfile detected, running Bundler version 1.0.7
</span><span class='line'>       All dependencies are satisfied
</span><span class='line'>-----&gt; Compiled slug size is 784K
</span><span class='line'>-----&gt; Launching... done, v8
</span><span class='line'>       http://def.reyssi.net deployed to Heroku
</span><span class='line'>
</span><span class='line'>To git@heroku.com:defreyssinet.git
</span><span class='line'>   efce197..2f3ed54  master -&gt; master</span></code></pre></td></tr></table></div></figure>


<p>Once the repository is pushed upstream, it will be available from the address configured in the Heroku app. In this case we named the app <code>defreyssinet</code> so the site will be available from <a href="http://defreyssinet.heroku.com">http://defreyssinet.heroku.com</a>.</p>

<h2>In conclusion</h2>

<p>In this blog post we have covered how to create a blog using the Octopress static site generator and host it on Heroku. In particular we have covered provisioning a local development environment with POW, obtaining Ruby using RVM and cloning the Octopress project from Github. We created a new Heroku application instance using the Heroku cli interface. Then we discovered how the publishing cycle works with Octopress, revolving around the <code>rake generate</code> task. Finally we pushed the Octopress blog to Heroku for production.</p>

<p>This article only scratches the surface of what Octopress offers, for more information I encourage everyone to read the official  <a href="http://octopress.org/docs">documentation</a>.</p>

<p>Octopress will certainly not be to everyones tastes, but I hope this article has provided those with the slightest curiosity about Octopress some impetus to try it from themselves.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hello 2012]]></title>
    <link href="http://def.reyssi.net/blog/2012/01/14/hello-2012/"/>
    <updated>2012-01-14T12:31:00-06:00</updated>
    <id>http://def.reyssi.net/blog/2012/01/14/hello-2012</id>
    <content type="html"><![CDATA[<p>As is so common at this time of year, I have made a new years resolution to restart blogging. I have attempted to blog in the past, twice in actuality. However both excursions failed to hold in the longterm due to a number of factors, most regularly <em>real life syndrome</em>. Of course I have not been totally silent in this time, writing for the popular PHP blog <a href="http://techportal.ibuildings.com">TechPortal</a> on the subject of HMVC <a href="http://techportal.ibuildings.com/2010/02/22/scaling-web-applications-with-hmvc/">Scalability</a> and <a href="http://techportal.ibuildings.com/2010/11/16/optimising-hmvc-web-applications-for-performance/">Optimization</a>.</p>

<p><em>So why start writing again now?</em> Well there are a number of reasons, too many to list here in full without leaving you bored. But three are worth mentioning;</p>

<!-- More -->


<ol>
<li><p>For some time I have wanted to create a new blog, but none of the existing popular blogging platforms attracted me. <em>Wordpress</em> is, in this authors opinion, an awfully architected application that has unfortunately become popular the world over. I also considered writing my own system, but given I hardly had time to write article developing my own platform would be too big an ask- I did have a <a href="https://github.com/samsoir/def.reyssi.net-legacy">stab at it</a> though! I had thoughts on developing a very rudimentary platform that would take my notes in Markdown format and publish them to either my server or another cloud-based server. Then I read a <a href="http://mattgemmell.com/2011/09/12/blogging-with-octopress/">blog post</a> by Matt Gemmell on <a href="http://octopress.org">Octopress</a> and realized it had already been done well.</p></li>
<li><p>I have started a new job in Chicago. This in itself is nothing special. However my new role involves using a new programming language in the form of <a href="http://ruby-lang.org">Ruby</a>. For over ten years I have been developing for the web using <a href="http://php.net">PHP</a> and related tools. So the move from LAMP to Nginx+Unicorn and; PHPUnit+Behat to Rspec+Cucumber will be a great opportunity for me to record my experience changing languages and toolsets. In all honesty I am delighted to be moving away from PHP, but that is a topic I&rsquo;ll be writing about separately. On top of the challenges associated with mastering new language,I have also moved into a strange new country, which presents another aspect of my life to write about. Chronicling the trials and tribulations of moving country for work may be useful to others, if not a reminder to myself that international moving is not for the faint hearted!</p></li>
<li><p>Since my previous blogs disappeared, Twitter and Google+ have provided me with some online expression capabilities. But they do not provide me with enough characters to express myself in any kind of meaningful way, especially when writing about complicated subjects such as the OAuth Protocol; Continuous Deployment pipelines; or the Hierarchical Model-View-Controller architectural pattern. I really needed the freedom to construct articles as I have been used to writing for other publishers. Thus, restarting a blog seemed the only appropriate action.</p></li>
</ol>


<p>I am turning off any ability to comment from the outset. The reasons for doing so has already been <a href="http://mattgemmell.com/2011/11/29/comments-off/">argued most eloquently</a> by the aforementioned Matt Gemmell on his blog, so no need repeat that here. Needless to say, feel free to comment on anything I say on your own blog, Twitter, Facebook, Google+, or by email.</p>

<p>Lets see what 2012 has to offer.</p>
]]></content>
  </entry>
  
</feed>
