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

  <title><![CDATA[about:benjie]]></title>
  <link href="http://www.benjiegillam.com/atom.xml" rel="self"/>
  <link href="http://www.benjiegillam.com/"/>
  <updated>2012-03-06T00:09:38+00:00</updated>
  <id>http://www.benjiegillam.com/</id>
  <author>
    <name><![CDATA[Benjie Gillam]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Node, Websockets, Safari and Emoji]]></title>
    <link href="http://www.benjiegillam.com/2012/03/node-websockets-safari-and-emoji/"/>
    <updated>2012-03-05T14:02:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/03/node-websockets-safari-and-emoji</id>
    <content type="html"><![CDATA[<p>It seems <a href="http://en.wikipedia.org/wiki/Emoji">emoji</a> break <a href="https://github.com/LearnBoost/websocket.io">websocket.io</a> (the websocket
library used by socket.io). They seem to cause the message payload to
terminate prematurely.</p>

<p>After a morning of research on the subject, I was pointed at <a href="https://gist.github.com/1707371">mranney&#8217;s
essay on the subject</a> by <em>`3rdEden</em> on
<a href="irc://irc.freenode.net/socket.io">irc.freenode.net/socket.io</a>. It turns out that the issue
is due to V8 (the JavaScript engine used by Node.JS) using the
<a href="http://en.wikipedia.org/wiki/Universal_Character_Set">UCS</a> encoding internally rather than the more modern UTF-16. Emoji
require 17 bits (their code values are larger than 65,535), which is
more than UCS can give (UCS uses exactly 2 bytes (16 bits) to represent
every character, so it only supports values between 0 and 65,535;
whereas UTF-8 and UTF-16 use a variable number of bytes).</p>

<p>I&#8217;ve confirmed with WireShark that Safari is sending valid UTF-8
(11110000 10011111 10011000 10010011 or hex: f0 9f 98 93, which
<a href="http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=f0+9f+98+93&amp;mode=bytes">gives</a> the Unicode codepoint U+1F613 😓). So I know it&#8217;s Node&#8217;s
issue receiving and processing it.</p>

<p>This is probably one of the reasons that Google Chrome doesn&#8217;t support
Emoji - check out <a href="http://en.wikipedia.org/wiki/Emoji">this page</a> in Chrome, then view it in Safari
to see what you&#8217;re missing! (Chrome uses the V8 engine.)</p>

<h2>Solution</h2>

<p>You could base64-encode your payload, or simply <code>escape()</code>/<code>unescape()</code>
it. Or you could trim anything outside of UCS. Or you could do a custom
encode/unencode such as <a href="http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt">this one</a>. I&#8217;m not really happy with
any of these, so I&#8217;m still looking for a solution.</p>

<h2>Update</h2>

<p>In the end I implemented <a href="http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html">this</a> (see below) on the client
side, and left the data encoded server side. All clients are responsible
for <code>encode()</code>ing characters when sending, and <code>decode()</code>ing upon
receiving (whether that be via websockets or HTTP). It seems to work
quite well and doesn&#8217;t massively inflate the content-size for ASCII and
possibly more[citation needed], so I&#8217;m relatively happy with it.
Besides it&#8217;s midnight and I need to get some sleep.</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_174966718');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_174966718');">.js</a></span><span>Patch UTF-8 issues</span></figcaption><div clang='CoffeeScript' id="figure_174966718"><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='coffee-script'><span class='line'><span class="nv">encode = </span><span class="nf">(s) -&gt;</span> <span class="nx">unescape</span> <span class="nb">encodeURIComponent</span> <span class="nx">s</span>
</span><span class='line'><span class="nv">decode = </span><span class="nf">(s) -&gt;</span> <span class="nb">decodeURIComponent</span> <span class="nx">escape</span> <span class="nx">s</span>
</span></code></pre></td></tr></table></div></div><div clang='JavaScript' id="figure_174966718_alt" style="display:none;"><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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">decode</span><span class="p">,</span> <span class="nx">encode</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">encode</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">unescape</span><span class="p">(</span><span class="nb">encodeURIComponent</span><span class="p">(</span><span class="nx">s</span><span class="p">));</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">decode</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">decodeURIComponent</span><span class="p">(</span><span class="nx">escape</span><span class="p">(</span><span class="nx">s</span><span class="p">));</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Switching to Vim: The Easy Way - Commands]]></title>
    <link href="http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way-ii/"/>
    <updated>2012-02-27T16:50:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way-ii</id>
    <content type="html"><![CDATA[<p>Continuing from what we learned in <a href="http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way/">part 1</a> (installing and
vimtutor), I&#8217;d now like to focus on the fundamental commands in Vim.</p>

<h2>Commands</h2>

<p>Vim commands are formed from a combination of <em>verbs</em> and <em>targets</em>. The
targets could be <em>objects</em> (words, sentences, paragraphs, lines, the
contents of parentheses) or <em>movements</em> (jump to end of word, jump to end
of paragraph, jump forward until the letter &#8216;e&#8217;, etc). Forming objects
generally involves the use of a <em>modifier</em>. You can also add
a <em>count</em> to perform the action <em>count</em> times.</p>

<h3>Verbs</h3>

<p>Here&#8217;s some example verbs (don&#8217;t try and learn them all at once!):</p>

<!--more-->


<ul>
<li><code>i</code> - insert - enter <em>insert mode</em></li>
<li><code>a</code> - append - enter <em>insert mode</em> after the carat</li>
<li><code>I</code> - enter <em>insert mode</em> at the beginning of the line</li>
<li><code>A</code> - append to line (enter <em>insert mode</em> at the end of the line)</li>
<li><code>o</code> - open a line after the current one and enter <em>insert mode</em></li>
<li><code>O</code> - open a line before the current one and enter <em>insert mode</em></li>
<li><code>d</code>[target] - delete (remove from the document and put in buffer)</li>
<li><code>y</code>[target] - yank (copy)</li>
<li><code>p</code> - paste the buffer after the cursor</li>
<li><code>P</code> - paste the buffer before the cursor</li>
<li><code>c</code>[target] - change (delete and then enter <em>insert mode</em>)</li>
<li><code>r</code>[char] - replace the character under the cursor with [char]</li>
<li><code>x</code> - delete the character under the cursor</li>
<li><code>u</code> - undo the last command</li>
<li><code>Ctrl-R</code> - redo the last undo (sidenote: in vim undo/redo forms a tree, changes aren&#8217;t lost)</li>
<li><code>/</code> - enter regex search mode</li>
<li><code>n</code> - find the next instance of the search term</li>
<li><code>N</code> - find the previous instance of the search term</li>
<li><code>.</code> - repeat last change (extremely powerful)</li>
</ul>


<h3>Nouns/Movements</h3>

<p>Nouns or movements are commands for moving within the document or
representing an area within a document. Common movements are:</p>

<ul>
<li><code>h</code>, <code>j</code>, <code>k</code>, <code>l</code> - equivalent to the arrow keys left, down, up,
right - you should aim to use these rather than the arrow keys, but
don&#8217;t worry too much yet!</li>
<li><code>0</code> - move to the very beginning of the current line</li>
<li><code>^</code> - move to the first non-whitespace character on the line</li>
<li><code>$</code> - move to the end of the line</li>
<li><code>w</code>, <code>b</code> - move to the next/previous word</li>
<li><code>W</code>, <code>B</code> - as <code>w</code>/<code>b</code> only Words are bigger</li>
<li><code>)</code>, <code>(</code> - move to the next/previous sentence</li>
<li><code>}</code>, <code>{</code> - move to the next/previous paragraph</li>
<li><code>f</code>/<code>F</code>[char] - find the next/previous instance of [char] in the current line</li>
<li><code>t</code>/<code>T</code>[char] - un<strong>t</strong>il - find the character before the next/after the
previous instance of [char]</li>
<li><code>/</code>[regexp]<Enter> - like <code>t</code> but instead of finding a character it finds a
regexp</li>
<li><code>%</code> - jump to the matching parenthesis (vim understands nested
parenthesis)</li>
<li><code>_</code> - move to the current line (useful for making commands
line-based)</li>
</ul>


<h3>Trying it out</h3>

<p>Try some of these movements out now - you can start to imagine how much faster
document navigation will be once you&#8217;ve mastered them.</p>

<p><span class='pullquote-right' data-pullquote='Once you&#8217;ve got a feel for the movements, try combining them with verbs. '>
Once you&#8217;ve got a feel for the movements, try combining them with verbs.
Don&#8217;t forget that if you do something wrong (or don&#8217;t understand what
happened) you can simply press <code>u</code> to undo, and try it again. For example:
</span></p>

<ul>
<li><code>cw</code> - &#8220;change&#8221; until the end of the current &#8220;word&#8221;</li>
<li><code>d}</code> - delete until the next paragraph (note that this is line-based
delete, so it will delete the whole of the current line too)</li>
<li><code>d_</code> - delete the current line (this can also be entered as <code>dd</code> for
brevity)</li>
<li><code>y_</code> - yank the current line (this can also be entered as <code>yy</code> for
brevity)</li>
<li><code>p</code>/<code>P</code> - paste the previously yanked (or deleted) text. If
the yank (or delete) was character based then it will paste after/before the
cursor, otherwise it will paste on the next/previous line.</li>
<li><code>27x</code> - delete the next 27 characters</li>
</ul>


<h3>Counts</h3>

<p>You can generally precede a verb or a movement with a count, which will
perform that action that many times. For example:</p>

<ul>
<li><code>3w</code> move to the third word from here</li>
<li><code>3j</code> move the cursor three lines down</li>
<li><code>3}</code> move to the 3rd paragraph from here</li>
<li><code>3i</code> will write whatever you insert 3 times. (The duplication will
not occur until you exit <em>insert mode</em>, e.g. with <code>&lt;Esc&gt;</code>.)</li>
<li><code>c3w</code> will change the next 3 words (i.e. it will delete 3 words and
then enter <em>insert mode</em>).</li>
<li><code>3dd</code> will delete the current line and the two below it</li>
<li><code>3p</code> will paste the contents of the buffer three times</li>
</ul>


<h3>Modifiers</h3>

<p>Modifiers can be used between a verb and a noun to modify its meaning.
For example, let&#8217;s look at <code>i</code>[noun]  (inner/inside):</p>

<ul>
<li><code>ciw</code> - change the whole word the cursor is on, even if the cursor is
in the middle of the word</li>
<li><code>ci)</code> - change the contents of the current pair of brackets
(understands bracket pairing)</li>
<li><code>ci"</code> - change the contents of the current pair of doublequotes
(understands quote escaping)</li>
</ul>


<p>another modifier is <code>a</code> - around - like <code>i</code> but includes the container.</p>

<h2>Next time</h2>

<p>In my next post, I&#8217;ll talk about some of the ways I&#8217;ve used these
basic commands to speed up my editing.</p>

<!--
Next time
---------

In the next posts I plan to cover:

 * visual mode
 * accessing help
 * some of the commands you can enter in *command-line mode*
 * modifying your vimrc, e.g. remapping commands
 * buffers
 * macros
 * `.`
-->



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Switching to Vim: The Easy Way - Installing/Vimtutor]]></title>
    <link href="http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way/"/>
    <updated>2012-02-24T10:20:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way</id>
    <content type="html"><![CDATA[<p>This post is the first in a series aiming to encourage you to switch to
Vim without going through that first period of significantly reduced
productivity that you&#8217;re bound to go through if you follow certain Vim
gurus advice such as disabling your arrow keys and what not. (This is
good advice once you&#8217;ve fully mastered the basics of Vim as a way to
kick bad habits, but I certainly wouldn&#8217;t advise starting that way!)</p>

<h2>Installing Vim</h2>

<p>I recommend you start with a graphical Vim app so that you can use your
mouse to click around. Ultimately you will rarely if ever use your mouse
(moving your had that far will seem really inefficient) but to start
with it can significantly alleviate the frustration of being slow with
the movement commands.</p>

<h3>Mac</h3>

<p>If you use <a href="http://mxcl.github.com/homebrew/">Homebrew</a> then you can simply issue</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 macvim</span></code></pre></td></tr></table></div></figure>


<p>And voila, you&#8217;re done. Otherwise take a look at the <a href="https://github.com/b4winckler/macvim">MacVim</a>
instructions.</p>

<!-- more -->


<p></p>

<h3>Linux</h3>

<p>You&#8217;re going to want gVim, probably. Install it through your usual
package manager.</p>

<h3>Windows</h3>

<p>You&#8217;re on <a href="http://www.vim.org/download.php#pc">your own</a>.</p>

<h3>Themes and Plugins</h3>

<p>To get you off to a running start, I highly recommend using the <a href="https://github.com/carlhuda/janus">Janus
vim distribution</a> for all your plugin needs. You can install more
plugins later, but these will cover all the basics you&#8217;re likely to need
including syntax highlighting for a huge number of languages (even stuff
like Markdown files) and a vast array of colour schemes (I like
<code>ir_black</code>).</p>

<p>If security is not a concern for you then you can run their installer
via 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'>$ curl -Lo- http://bit.ly/janus-bootstrap | bash</span></code></pre></td></tr></table></div></figure>


<h2>Vimtutor</h2>

<p><span class='pullquote-right' data-pullquote='if you haven&#8217;t used a modal editor before then I assure you it will be worth the time spent learning it'>
The first thing you should do is to try out <code>vimtutor</code> - just run it
from the command line assuming it was installed with Vim. Spend 25
minutes going through everything to get a feel for Vim. Don&#8217;t worry if
it doesn&#8217;t all click into place at first - you can use Vim just like a
plain text editor at first, and slowly improve your skill over time. If
you&#8217;re a programmer and
if you haven&#8217;t used a modal editor before then I assure you it will be worth the time spent learning it.
</span></p>

<h2>Absolute basics</h2>

<p>Initially you only need to worry about two of vims modes: <em>normal mode</em>
and <em>insert mode</em>.</p>

<h3>Normal mode</h3>

<p>This is the mode that vim opens in. Here you can type commands. Typing
<code>:w&lt;Enter&gt;</code> saves the current document. Typing <code>i</code> puts you into <em>insert
mode</em>.</p>

<h3>Insert mode</h3>

<p><span class='pullquote-right' data-pullquote='Initially you can just treat it like a plain text editor '>
<em>Insert mode</em> is where you enter text.
Initially you can just treat it like a plain text editor
- write stuff and use the arrow keys to move around. When
you want to save, press <code>&lt;Esc&gt;</code> to exit back to <em>normal mode</em> and then
type <code>:w&lt;Enter&gt;</code> to save. You can then press <code>i</code> to get back into <em>insert
mode</em>.
</span></p>

<p>When you are in <em>insert mode</em> you will see <code>-- INSERT --</code> displayed at the
bottom of the screen. Remember: to exit <em>insert mode</em> just press <code>&lt;Esc&gt;</code>.</p>

<h3>Moving on</h3>

<p>Take a while to master the above before moving on. It doesn&#8217;t matter how
long it takes you - the key is to avoid the frustration of having
deminished productivity by trying to do too much at once.</p>

<p>My next post will focus on the fundamental commands in Vim, like those
you touched upon in vimtutor.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[12 Years Together!]]></title>
    <link href="http://www.benjiegillam.com/2012/02/12-years-together/"/>
    <updated>2012-02-14T00:38:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/12-years-together</id>
    <content type="html"><![CDATA[<p>Today marks the 12th anniversary of Jem and I being a couple. We met
when we were 13 on a Scout camp site near Southampton and immediately
became pen-pals. We wrote each other frequently, meeting up when we
could, and soon fell in love. That love, despite being the painfully
intense all-encompassing love of teenagers, has matured, strengthened
and grown over time. We&#8217;ve now been married for over 3 years and our son
Xander is 16 months old.</p>

<p>I want to take this opportunity to thank my wife for everything. Her
constant loving support helps me in every aspect of my life from
fatherhood to business to helping me deal with my health problems. I
honestly don&#8217;t know what I would do without her.</p>

<p style='font-size:4em;text-align:center;font-weight:bold;line-height:1.3em;margin-bottom:0.3em'>&#10084; Thank You, Jem &#10084;</p>


<p>You&#8217;re the most wonderful woman I know - beautiful, funny, clever,
loving, honest, understanding and a fantastic wife and mother.  Every
day I feel grateful to have you in my life and I truly hope I make you
feel loved and appreciated. Together we make a brilliant team - the
future never scares me, because I know whatever happens you&#8217;ll be there,
by my side.</p>

<h2 style='background:none'>
To me, you are perfect; I love you now, always and forever.
</h2>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A Plugin-Free Web]]></title>
    <link href="http://www.benjiegillam.com/2012/02/a-plugin-free-web/"/>
    <updated>2012-02-06T11:11:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/a-plugin-free-web</id>
    <content type="html"><![CDATA[<p>It&#8217;s almost unheard of for me to complement something that Microsoft are
doing - especially when it comes to Internet Explorer - but I&#8217;m 100%
behind a <a href="http://daringfireball.net/linked/2012/02/02/plugin-free-web">plugin-free web</a>:</p>

<blockquote><p>John Hrvatin from the Internet Explorer team:</p><p><blockquote> The transition to a plug-in free Web is happening today. Any site that uses plug-ins needs to understand what their customers experience when browsing plug-in free. Lots of Web browsing today happens on devices that simply don’t support plug-ins. Even browsers that do support plug-ins offer many ways to run plug-in free.</p><p>Metro style IE runs plug-in free to improve battery life as well as security, reliability, and privacy for consumers.<br/></blockquote></p><p>How long until Google joins the party?</p><footer><strong>John Gruber</strong> <cite><a href='http://daringfireball.net/linked/2012/02/02/plugin-free-web'>Microsoft Pushes for Plugin-Free Web</a></cite></footer></blockquote>


<p>Frequently friends and acquaintances will talk to me about iOS/Android
(being an iOS developer) and one of the things they&#8217;ll very often say is
&#8220;yeah, but iOS can&#8217;t run Flash,&#8221; as if that&#8217;s a bad thing! Banning Flash
from the platform is one of the things that makes me really glad I own
iOS devices and that they&#8217;ve such a large market share. Many websites
that use Flash are being forced to redevelop into open and
standards-compliant HTML &amp; JS so that they can capture the mobile
audience.</p>

<p><span class='pullquote-right' data-pullquote='If a shopping website forces me to use Flash to shop there then I simply don&#8217;t shop there '>
Flash is the most common browser plugin, and I loathe it. I block it on
all my computers and only use it where I absolutely have to.
If a shopping website forces me to use Flash to shop there then I simply don&#8217;t shop there.
And with HTML5 Video/Audio and the speed of JavaScript
in todays modern browsers (even the latest Internet Explorers!) there&#8217;s
less and less justification for websites to use Flash (or worse:
Silverlight! I&#8217;m looking at you, <a href="http://blog.lovefilm.com/uncategorized/why-were-switching-from-flash-to-silverlight.html">LoveFilm</a>).
</span></p>

<p>I&#8217;m not the only person who hates Flash, just look at <a href="http://www.google.co.uk/search?q=flash+block">all the plugins to
disable it</a>!</p>

<h2>Why Plugins Suck</h2>

<p>There&#8217;s many reasons, but mostly it comes down to non-compatibility,
performance, security and user experience.</p>

<h2>Compatibility</h2>

<p>For a long time there was no Flash player for Linux. Then
there was no Flash player for 64-bit operating systems. There&#8217;s no Flash
for iOS. My Blu-ray player can&#8217;t run Flash. Apparently my Tivo does run
Flash, but it&#8217;s god-awful. <!-- more --></p>

<p>And because Flash runs in a different rendering pipeline it doesn&#8217;t
integrate well with web browsers - especially when it comes to
transparency. (See &#8220;User Experience&#8221;)</p>

<h2>Performance</h2>

<p>A lot of devices are easily capable of rendering HTML/CSS/JS but Flash
pushes them too hard and the experience is ugly and slow. And Flash
seems to be used for a whole host of things that HTML/CSS/JS have been
capable of for years and years across all major browsers - one thing
that comes to mind is all the advertising you get on certain websites.</p>

<h2>Security</h2>

<p><span class='pullquote-right' data-pullquote='sharing your webcam feed without your permission '>
Bugs in plugins expose your browser to a lot of potential security risks -
<a href="http://www.google.co.uk/search?&amp;q=flash+xss">cross site scripting</a> (XSS) attacks, <a href="http://www.google.co.uk/search?q=flash+information+disclosure">information
disclosure</a>, even
sharing your webcam feed without your permission
(<a href="http://news.yahoo.com/blogs/technology-blog/adobe-flash-exploit-allows-websites-access-webcam-without-010049284.html">October 2011</a>)! Over
the years a lot of these issues have been patched, but it should be the
job of the web browser to allow/deny access to various resources, having
two interfaces increases the attack surface - it&#8217;s risky and confusing.
</span></p>

<h2>User Experience</h2>

<p>When hover to expand adverts first came out they used to actually block
the viewing of the content on some websites in some browsers - so I
simply stopped visiting those website. What&#8217;s the point if you can&#8217;t
actually read the content?!</p>

<p>Once you&#8217;ve got 10 Flash adverts on a web-page the scroll performance
drops significantly, even on a dual core computer. There&#8217;s also the
increased render time of the webpage as a whole, and the annoying (and
unnecessary!) loading indicators.</p>

<p>When you close a browser tab, plugins on that page playing audio/etc
often won&#8217;t close until a couple of seconds later, which can be very
irritating. And don&#8217;t get me started on websites that suddenly start
talking to you without you asking them to! (Though this is possible with
HTML5 also&#8230;)</p>

<h2>Acceptable Use Cases</h2>

<p>There used to be lots of use-cases for which I found the use of Flash
acceptable, but many of these are being diminished due to HTML5 adding
extra capabilities to the browser:</p>

<ul>
<li>3D browser games (soon to be invalid due to <a href="http://get.webgl.org/">WebGL</a> and similar technologies)</li>
<li>streaming video/audio (now supported by <a href="https://developer.mozilla.org/en/Using_HTML5_audio_and_video">HTML5 audio and
video</a>)</li>
<li>video conferencing (there&#8217;s many workarounds for this - W3 are
attempting to create a <a href="http://www.w3.org/TR/html-media-capture/">Media Capture API</a>)</li>
<li>real-time socket communications (<a href="http://www.w3.org/TR/websockets/">WebSockets</a> allow real time
communications by extending HTTP)</li>
</ul>


<h2>Progress</h2>

<p>Apple, Microsoft I applaud your efforts to rid the internet of these
plugins (despite the existence of QuickTime, Silverlight and the like).</p>

<p>Google, I&#8217;m disappointed with you integrating Flash so deeply into
Chrome, but Chrome&#8217;s an awesome browser for development so I shall use it
anyway. Good work on making YouTube work with HTML5 though :)</p>

<p>HTML5 guys, you all rock.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Happy New Year 2012]]></title>
    <link href="http://www.benjiegillam.com/2012/02/happy-new-year-2012/"/>
    <updated>2012-02-04T18:57:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/happy-new-year-2012</id>
    <content type="html"><![CDATA[<p><span class='pullquote-right' data-pullquote='Xander&#8217;s walking well and he&#8217;s getting closer to speech every day '>
How&#8217;s your year going so far? Mine&#8217;s going really well.
Xander&#8217;s walking well and he&#8217;s getting closer to speech every day
which is fascinating and exciting to watch, my health has improved
massively over last year and I&#8217;m really enjoying work.
</span></p>

<p><span class='pullquote-right' data-pullquote='making an effort to do the things I&#8217;ve always meant to '>
This year I&#8217;m
making an effort to do the things I&#8217;ve always meant to
do as a programmer; and playing with lots of new (to me)
tech in the process! I intend to cover a few of these topics in the coming
weeks/months, but here&#8217;s a summary:
</span></p>

<h2>Vim</h2>

<p><span class='pullquote-right' data-pullquote='you don&#8217;t have to jump in at the deep end '>
I&#8217;ve started using Vim as my main editor (or more specifically
<a href="https://github.com/b4winckler/macvim">MacVim</a> with the <a href="https://github.com/carlhuda/janus">Janus vim distribution</a>) and I&#8217;m
loving it. It&#8217;s a relatively steep learning curve, but
you don&#8217;t have to jump in at the deep end
 - just start with <code>i</code> to enter insert mode, use
it like a normal editor, and then to save press <code>&lt;Esc&gt;</code> followed by
<code>:wq&lt;Enter&gt;</code> to save and exit (drop the <code>q</code> if you don&#8217;t want to exit).
Slowly start learning new things and forcing yourself to start using
normal mode (where you type commands like <code>:wq</code>) and as you use more,
you&#8217;ll want to use more. Once you&#8217;ve mastered the whole
count-verb-modifier-noun grammar of vim you&#8217;ll be using it for
everything and wondering how you ever managed without it!
</span></p>

<h2>Node.js</h2>

<p><span class='pullquote-right' data-pullquote='anything that needs scripting I now do in JavaScript '>
Okay, so I&#8217;ve been playing with this for a while, but I&#8217;m using it for
more and more stuff now - pretty much
anything that needs scripting I now do in JavaScript, or more specifically&#8230;
</span></p>

<h2>CoffeeScript</h2>

<p><span class='pullquote-right' data-pullquote='I&#8217;m now using it for ALL my JavaScript programming '>
&#8230; <a href="http://coffeescript.org/">CoffeeScript</a>, which is bloody brilliant - especially
if you have a strong understanding of how the underlying JavaScript
functions. This isn&#8217;t new either, but
I&#8217;m now using it for ALL my JavaScript programming
</span></p>

<h2>Underscore.js</h2>

<p><a href="http://documentcloud.github.com/underscore/">Underscore.js</a> is a light library of helpful JavaScript
functions for working with objects, arrays, collections and other such
things in a functional programming way <strong>without</strong> extending the native
objects. jQuery/Mootools use <code>$</code> for working with DOM nodes, Underscore
uses <code>_</code> for dealing with everything else.</p>

<h2>Backbone.js</h2>

<p><a href="http://documentcloud.github.com/backbone/">Backbone.js</a> extends Underscore.js and is a great way of
keeping the code of your <abbr title="Rich Internet Applications">RIAs</abbr> organised.
It&#8217;s very flexible and can fit to many different programming
methodologies, but I use it in a M-VC (Model, ViewController) manner not
dissimilar to iOS. Event are emitted from models when they&#8217;re added,
changed or deleted; hooking into these events allows your app to always
display consistent information without the need for lots of hard-coded
callbacks.</p>

<h2>jQuery</h2>

<p>It seems everybody uses jQuery, and has done since its inception. I
refused to jump on the band-wagon and I&#8217;m glad I did - I coded
everything in JavaScript from scratch using native methods and help
from articles across the web (especially sites such as
<a href="http://www.quirksmode.org/">QuirksMode</a>) and now have a very good understanding of how
different parts of a modern web browser piece together and the
faster/slower ways to go about doing things.</p>

<p><span class='pullquote-right' data-pullquote='reduces programming time significantly and adds flexibility '>
However, jQuery is a lot faster than it used to be, as are modern web
browsers, so my concerns are much less valid. It
reduces programming time significantly and adds flexibility
so I now use it (or
<a href="http://zeptojs.com/">Zepto.js</a>) in a lot of my projects and I really like it - it
doesn&#8217;t take long to learn and master, and it improves the speed of
Backbone development considerably.
</span></p>

<h2>Git</h2>

<p><span class='pullquote-right' data-pullquote='brilliant - lightening fast and so flexible '>
Git, as you should know, is a brilliant version control software. I&#8217;ve
been using SVN for years but have had the itch to switch for ages, so
with some new projects that I&#8217;ve been working on (using the above
technologies) I decided to switch to using git where possible. It&#8217;s
brilliant - lightening fast and so flexible. The lack of the old
<code>/trunk/</code>, <code>/branches/</code>, <code>/tags/</code> layout makes me happy too - trying to
convince employees to use <code>svn switch</code> was very hard, but with <code>git</code>
they&#8217;ve got no choice but to do it the right way! I&#8217;m using
<a href="https://github.com/sitaramc/gitolite">gitolite</a> for our internal (private) hosting, and &#8230;
</span></p>

<h2>Open Sourcing</h2>

<p><span class='pullquote-right' data-pullquote='I intend to release much more in the coming year '>
&#8230; I&#8217;ve started Open Sourcing some stuff I&#8217;m working on to GitHub.
First is my <a href="https://github.com/BenjieGillam/ec2-prune-snapshots">ec2-prune-snapshots</a> script for
automatically deleting sensibly old <abbr title="Elastic Block Storage">EBS</abbr>
snapshots on <a href="https://aws.amazon.com/">AWS</a>, but
I intend to release much more in the coming year, and
possibly even retro-actively releasing some older software,
such as <a href="http://code.google.com/p/mythpywii/">MythPyWii</a> which currently sits on Google Code.
</span></p>

<h2>What Are You Using?</h2>

<p>If you&#8217;re using some cool technology for web/mobile application
development that you think I might enjoy experimenting with then please
let me know in the cooments!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Multiple Asynchronous Callbacks]]></title>
    <link href="http://www.benjiegillam.com/2011/11/multiple-asynchronous-callbacks/"/>
    <updated>2011-11-23T10:01:00+00:00</updated>
    <id>http://www.benjiegillam.com/2011/11/multiple-asynchronous-callbacks</id>
    <content type="html"><![CDATA[<p>When programming in JavaScript (or CoffeeScript) you sometimes face a
situation where you need to complete multiple independant asynchronous
methods before continuing. This situation crops up in web browsers but
its much more common when writing server-side JavaScript, e.g. with
<a href="http://nodejs.org/">Node.js</a> - for example you might need to fetch from a
database, fetch from a <abbr title="Key value store, e.g. Redis/Memcached">KVS</abbr>,
read a file and perform a remote HTTP request before outputting the
compiled information to the end user.</p>

<p>One method of solving this issue is to chain the asynchronous calls, however this
means that they&#8217;re run one after the other (serially) - and thus it will
take longer to complete them. A better way would be to run them in
parallel and have something track their completion. This is exactly what
my very simple <code>AsyncBatch</code> class does:</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_1834956');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_1834956');">.js</a></span><span>AsyncBatch class, triggers event once all wrapped callbacks complete</span></figcaption><div clang='CoffeeScript' id="figure_1834956"><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='coffee-script'><span class='line'><span class="k">class</span> <span class="nx">AsyncBatch</span> <span class="k">extends</span> <span class="nx">EventEmitter</span>
</span><span class='line'>  <span class="nv">constructor: </span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="vi">@_complete = </span><span class="p">{}</span>
</span><span class='line'>    <span class="vi">@_scheduled = </span><span class="p">{}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">wrap: </span><span class="nf">(name,cb) -&gt;</span>
</span><span class='line'>    <span class="nx">@_scheduled</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span>
</span><span class='line'>    <span class="k">return</span> <span class="o">=&gt;</span>
</span><span class='line'>      <span class="nx">@_complete</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">cb</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">@</span><span class="p">,</span> <span class="nx">arguments</span>
</span><span class='line'>      <span class="k">if</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_complete</span><span class="p">).</span><span class="nx">length</span> <span class="o">==</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_scheduled</span><span class="p">).</span><span class="nx">length</span>
</span><span class='line'>        <span class="nx">@emit</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nx">@_complete</span>
</span></code></pre></td></tr></table></div></div><div clang='JavaScript' id="figure_1834956_alt" style="display:none;"><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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">AsyncBatch</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">__hasProp</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">__extends</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">child</span><span class="p">,</span> <span class="nx">parent</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">parent</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="nx">__hasProp</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">parent</span><span class="p">,</span> <span class="nx">key</span><span class="p">))</span> <span class="nx">child</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span> <span class="p">}</span> <span class="kd">function</span> <span class="nx">ctor</span><span class="p">()</span> <span class="p">{</span> <span class="k">this</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">=</span> <span class="nx">child</span><span class="p">;</span> <span class="p">}</span> <span class="nx">ctor</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span> <span class="nx">child</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ctor</span><span class="p">;</span> <span class="nx">child</span><span class="p">.</span><span class="nx">__super__</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span> <span class="k">return</span> <span class="nx">child</span><span class="p">;</span> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">AsyncBatch</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">_super</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">__extends</span><span class="p">(</span><span class="nx">AsyncBatch</span><span class="p">,</span> <span class="nx">_super</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="kd">function</span> <span class="nx">AsyncBatch</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">_complete</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">_scheduled</span> <span class="o">=</span> <span class="p">{};</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">AsyncBatch</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">wrap</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">cb</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">_this</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">_scheduled</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>      <span class="nx">_this</span><span class="p">.</span><span class="nx">_complete</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">cb</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">_this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">_this</span><span class="p">.</span><span class="nx">_complete</span><span class="p">).</span><span class="nx">length</span> <span class="o">===</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">_this</span><span class="p">.</span><span class="nx">_scheduled</span><span class="p">).</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nx">_this</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;done&#39;</span><span class="p">,</span> <span class="nx">_this</span><span class="p">.</span><span class="nx">_complete</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>  <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">return</span> <span class="nx">AsyncBatch</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="p">})(</span><span class="nx">EventEmitter</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></div></figure>


<p>To use AsyncBatch, just do the following</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_983064895');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_983064895');">.js</a></span><span>How to use AsyncBatch</span></figcaption><div clang='CoffeeScript' id="figure_983064895"><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>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="cm">Create a new AsyncBatch instance</span>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="nv">batch = </span><span class="k">new</span> <span class="nx">AsyncBatch</span>
</span><span class='line'>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="cm">Wrap your callbacks with batch.wrap and a name</span>
</span><span class='line'><span class="cm">The name is used to store the result returned by your callback</span>
</span><span class='line'><span class="cm">(Don&#39;t forget to return the result you&#39;re interested in!)</span>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="nx">delay</span> <span class="mi">50</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;timer&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="k">return</span> <span class="s">&quot;Timer complete&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="cm">Add a completion handler that accepts `results` as a parameter.</span>
</span><span class='line'><span class="cm">  `results` is a JS object where the keys are the callback names </span>
</span><span class='line'><span class="cm">  from above and the values are the return values of the</span>
</span><span class='line'><span class="cm">  callbacks.</span>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="nx">batch</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nf">(results) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Batch complete, timer result: </span><span class="si">#{</span><span class="nx">results</span><span class="p">.</span><span class="nx">timer</span><span class="si">}</span><span class="s">&quot;</span>
</span></code></pre></td></tr></table></div></div><div clang='JavaScript' id="figure_983064895_alt" style="display:none;"><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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="cm">/*</span>
</span><span class='line'><span class="cm">Create a new AsyncBatch instance</span>
</span><span class='line'><span class="cm">*/</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">batch</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">batch</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">AsyncBatch</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="cm">/*</span>
</span><span class='line'><span class="cm">Wrap your callbacks with batch.wrap and a name</span>
</span><span class='line'><span class="cm">The name is used to store the result returned by your callback</span>
</span><span class='line'><span class="cm">(Don&#39;t forget to return the result you&#39;re interested in!)</span>
</span><span class='line'><span class="cm">*/</span>
</span><span class='line'>
</span><span class='line'><span class="nx">delay</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s1">&#39;timer&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="s2">&quot;Timer complete&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">}));</span>
</span><span class='line'>
</span><span class='line'><span class="cm">/*</span>
</span><span class='line'><span class="cm">Add a completion handler that accepts `results` as a parameter.</span>
</span><span class='line'><span class="cm">  `results` is a JS object where the keys are the callback names </span>
</span><span class='line'><span class="cm">  from above and the values are the return values of the</span>
</span><span class='line'><span class="cm">  callbacks.</span>
</span><span class='line'><span class="cm">*/</span>
</span><span class='line'>
</span><span class='line'><span class="nx">batch</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;done&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">results</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;Batch complete, timer result: &quot;</span> <span class="o">+</span> <span class="nx">results</span><span class="p">.</span><span class="nx">timer</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></div></figure>


<p><strong>NOTE</strong>: If your asynchronous method accepts both a success and
  failure callback then simply wrap both individually but
  ensure you use the same name for both.</p>

<p><strong>NOTE</strong>: Other than the case in the previous NOTE, all callbacks
  should have different names.</p>

<p>A full example (including a stub <code>EventEmitter</code> implementation) follows:</p>

<!-- more -->




<figure class='code'><figcaption><span>An example of how to use AsyncBatch to handle parallel callbacks in Node.JS  (async-batch.coffee)</span> <a href='http://www.benjiegillam.com/code/async-batch.coffee'>download</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>
<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>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="c1">#EventEmitter = require(&#39;events&#39;).EventEmitter</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="o">!</span><span class="nx">EventEmitter</span><span class="o">?</span>
</span><span class='line'>  <span class="k">class</span> <span class="nx">EventEmitter</span>
</span><span class='line'>    <span class="kc">on</span><span class="o">:</span> <span class="nf">(eventName, cb) -&gt;</span>
</span><span class='line'>      <span class="vi">@_eventCallbacks = </span><span class="p">{}</span> <span class="nx">unless</span> <span class="nx">@_eventCallbacks</span>
</span><span class='line'>      <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span> <span class="nx">unless</span> <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span>
</span><span class='line'>      <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">].</span><span class="nx">push</span> <span class="nx">cb</span>
</span><span class='line'>    <span class="nv">emit: </span><span class="nf">(eventName, args...) -&gt;</span>
</span><span class='line'>      <span class="k">if</span> <span class="nx">@_eventCallbacks</span> <span class="o">&amp;&amp;</span> <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span>
</span><span class='line'>        <span class="nx">cb</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">@</span><span class="p">,</span> <span class="nx">args</span> <span class="k">for</span> <span class="nx">cb</span> <span class="k">in</span> <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nx">AsyncBatch</span> <span class="k">extends</span> <span class="nx">EventEmitter</span>
</span><span class='line'>  <span class="nv">constructor: </span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="vi">@_complete = </span><span class="p">{}</span>
</span><span class='line'>    <span class="vi">@_scheduled = </span><span class="p">{}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">wrap: </span><span class="nf">(name,cb) -&gt;</span>
</span><span class='line'>    <span class="nx">@_scheduled</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span>
</span><span class='line'>    <span class="k">return</span> <span class="o">=&gt;</span>
</span><span class='line'>      <span class="nx">@_complete</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">cb</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">@</span><span class="p">,</span> <span class="nx">arguments</span>
</span><span class='line'>      <span class="k">if</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_complete</span><span class="p">).</span><span class="nx">length</span> <span class="o">==</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_scheduled</span><span class="p">).</span><span class="nx">length</span>
</span><span class='line'>        <span class="nx">@emit</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nx">@_complete</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="nv">batch = </span><span class="k">new</span> <span class="nx">AsyncBatch</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span><span class="c1">##</span>
</span><span class='line'><span class="c1"># Task 1</span>
</span><span class='line'><span class="nv">http = </span><span class="nx">require</span> <span class="s">&#39;http&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">options =</span>
</span><span class='line'>  <span class="nv">host: </span><span class="s">&#39;www.example.com&#39;</span>
</span><span class='line'>  <span class="nv">port: </span><span class="mi">80</span>
</span><span class='line'>  <span class="nv">path: </span><span class="s">&#39;/&#39;</span>
</span><span class='line'>  <span class="nv">method: </span><span class="s">&#39;GET&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">req = </span><span class="nx">http</span><span class="p">.</span><span class="nx">request</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;status&#39;</span><span class="p">,</span> <span class="nf">(res) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;STATUS: &#39;</span> <span class="o">+</span> <span class="nx">res</span><span class="p">.</span><span class="nx">statusCode</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;HEADERS: &#39;</span> <span class="o">+</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">res</span><span class="p">.</span><span class="nx">headers</span><span class="p">)</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">res</span><span class="p">.</span><span class="nx">statusCode</span>
</span><span class='line'>
</span><span class='line'><span class="nx">req</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;error&#39;</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;status&#39;</span><span class="p">,</span> <span class="nf">(e) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;problem with request: &#39;</span> <span class="o">+</span> <span class="nx">e</span><span class="p">.</span><span class="nx">message</span>
</span><span class='line'>  <span class="k">return</span> <span class="kc">null</span>
</span><span class='line'>
</span><span class='line'><span class="nx">req</span><span class="p">.</span><span class="nx">end</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span><span class="c1">#</span>
</span><span class='line'><span class="c1"># Task 2</span>
</span><span class='line'><span class="nv">fs = </span><span class="nx">require</span> <span class="s">&#39;fs&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span> <span class="s">&#39;/etc/hosts&#39;</span><span class="p">,</span> <span class="s">&#39;utf8&#39;</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;hosts&#39;</span><span class="p">,</span> <span class="nf">(err, data) -&gt;</span>
</span><span class='line'>  <span class="k">if</span> <span class="nx">err</span>
</span><span class='line'>    <span class="k">return</span> <span class="kc">null</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">data</span>
</span><span class='line'>  <span class="nv">tmp = </span><span class="nx">data</span><span class="p">.</span><span class="nx">split</span> <span class="s">&quot;\n&quot;</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">tmp</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="nx">tmp</span><span class="p">.</span><span class="nx">length</span><span class="p">)]</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span>
</span><span class='line'><span class="c1"># Task 3</span>
</span><span class='line'><span class="nv">delay = </span><span class="nf">(ms, cb) -&gt;</span> <span class="nx">setTimeout</span> <span class="nx">cb</span><span class="p">,</span> <span class="nx">ms</span>
</span><span class='line'>
</span><span class='line'><span class="nx">delay</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="mi">100</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;timer&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Timer done&quot;</span>
</span><span class='line'>  <span class="k">return</span> <span class="kc">true</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span><span class="c1">#</span>
</span><span class='line'><span class="c1"># Completion:</span>
</span><span class='line'><span class="nx">batch</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nf">(results) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;\n\n\nThe fetch, read and timer have completed.&quot;</span>
</span><span class='line'>  <span class="k">if</span> <span class="nx">results</span><span class="p">.</span><span class="nx">status</span> <span class="o">!=</span> <span class="kc">null</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Fetch succeeded, status code: </span><span class="si">#{</span><span class="nx">results</span><span class="p">.</span><span class="nx">status</span><span class="si">}</span><span class="s">&quot;</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">error</span> <span class="s">&quot;Fetch failed!&quot;</span>
</span><span class='line'>  <span class="k">if</span> <span class="nx">results</span><span class="p">.</span><span class="nx">hosts</span> <span class="o">!=</span> <span class="kc">null</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Random entry from /etc/hosts: &quot;</span><span class="o">+</span><span class="nx">results</span><span class="p">.</span><span class="nx">hosts</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">error</span> <span class="s">&quot;Couldn&#39;t read /etc/hosts!&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">batch</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nf">(results) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;\nSecond completion callback, raw results:&quot;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">results</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CoffeeScript: JavaScript, but clearer]]></title>
    <link href="http://www.benjiegillam.com/2011/11/coffeescript-lets-you-express-yourself-more-clearly/"/>
    <updated>2011-11-21T13:59:00+00:00</updated>
    <id>http://www.benjiegillam.com/2011/11/coffeescript-lets-you-express-yourself-more-clearly</id>
    <content type="html"><![CDATA[<p>I really like <a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a>. I really like JavaScript. I&#8217;m trying to get
into the habit of writing more CoffeeScript since, I think, it allows
me to express myself clearer and more concisely, leading to shorter
code and (hopefully) less mistakes/bugs. It also makes jumping back into
code a month later a much simpler affair since reading the code is so
much easier.</p>

<p>The following JavaScript creates a new closure inside each iteration of
a loop to store the value of the loop variable <code>i</code> into the closure&#8217;s
local variable <code>j</code> so that we don&#8217;t just <code>console.log(10)</code> 10 times.
Though this example is trivial it represents a common method for
solving closure related issues in asynchronous code.</p>

<figure class='code'><figcaption><span>JavaScript closure required for correct output</span></figcaption><div clang='' id="figure_827325263"><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='js'><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="p">(</span><span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">j</span> <span class="o">=</span> <span class="nx">i</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">delay</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>      <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">j</span><span class="p">);</span>
</span><span class='line'>    <span class="p">});</span>
</span><span class='line'>  <span class="p">})();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_827325263_alt" style="display:none;"></div></figure>


<p>The following CoffeeScript does exactly the same thing, you can see the
translation line by line from the JavaScript. However once you&#8217;ve learnt
the syntax you can much more clearly see what is going on with the
CoffeeScript than in the verbose and parenthesis-heavy JavaScript above.</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_222520542');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_222520542');">.js</a></span><span>CoffeeScript to do the same thing</span></figcaption><div clang='CoffeeScript' id="figure_222520542"><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='coffee-script'><span class='line'><span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">..</span><span class="mi">10</span><span class="p">]</span>
</span><span class='line'>  <span class="nx">do</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nv">j = </span><span class="nx">i</span>
</span><span class='line'>    <span class="nx">delay</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>      <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">j</span>
</span></code></pre></td></tr></table></div></div><div clang='JavaScript' id="figure_222520542_alt" style="display:none;"><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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">var</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">_fn</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">_fn</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">j</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">j</span> <span class="o">=</span> <span class="nx">i</span><span class="p">;</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">delay</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">j</span><span class="p">);</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">_fn</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></div></figure>


<p>As a bonus, <!-- more -->when CoffeeScript compiles to JavaScript it automatically takes the anonymous function
from line 2 and defines it outside the loop so that it need not be
interpretted/defined 10 times - it does this optimization for us.
(Though I suspect most modern JavaScript engines do this automatically
anyway.) To view the compiled JavaScript press the <code>View JavaScript</code>
link at the top left of the code snippet.</p>

<p>Note the delay function is just a reordered <code>setTimeout</code>:</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_168071296');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_168071296');">.js</a></span><span>The delay function</span></figcaption><div clang='CoffeeScript' id="figure_168071296"><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='coffee-script'><span class='line'><span class="nv">delay = </span><span class="nf">(ms, cb) -&gt;</span> <span class="nx">setTimeout</span> <span class="nx">cb</span><span class="p">,</span> <span class="nx">ms</span>
</span></code></pre></td></tr></table></div></div><div clang='JavaScript' id="figure_168071296_alt" style="display:none;"><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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">delay</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">delay</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">ms</span><span class="p">,</span> <span class="nx">cb</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">cb</span><span class="p">,</span> <span class="nx">ms</span><span class="p">);</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ASA Regulating 'Unlimited' Broadband Claims]]></title>
    <link href="http://www.benjiegillam.com/2011/09/asa-regulating-unlimited-broadband-claims/"/>
    <updated>2011-09-29T11:08:00+01:00</updated>
    <id>http://www.benjiegillam.com/2011/09/asa-regulating-unlimited-broadband-claims</id>
    <content type="html"><![CDATA[<blockquote><p>Strict guidelines will also be placed on internet providers which offer so-called  unlimited packages, where users can download to their heart&#8217;s content. If a user incurs an &#8220;additional charge or suspension of service as a consequence of exceeding a usage threshold,&#8221; the ISP cannot use the term &#8220;unlimited&#8221;.</p><footer><strong>Wired</strong> <cite><a href='http://www.wired.co.uk/news/archive/2011-09/29/asa-cracks-down-on-isps'>ASA Cracks Down on ISPs</a></cite></footer></blockquote>


<p>Finally! I hate it when providers are allowed to say &#8216;Unlimited&#8217; and then place a limit on it (generally via a Fair Usage Policy). What a load of rubbish! I&#8217;m a heavy bandwidth user so it makes it very hard for me to find out what the limit is and then choose the relevant provider. Fortunately Virgin&#8217;s XXL broadband <a href="http://shop.virginmedia.com/help/traffic-management/traffic-management-policy.html">truly is unlimited</a> - at least for now!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Octopress/Disqus Issue Resolved]]></title>
    <link href="http://www.benjiegillam.com/2011/09/octopress-slash-disqus-issue-resolved/"/>
    <updated>2011-09-28T11:26:00+01:00</updated>
    <id>http://www.benjiegillam.com/2011/09/octopress-slash-disqus-issue-resolved</id>
    <content type="html"><![CDATA[<p>I was having an issue with Disqus - my comments were showing on post pages, but the comment count was not. Turns out that this is a simple to fix error relating to JavaScript scope.</p>

<h2>Scope issue</h2>

<p>The developer of Octopress has kindly protected our <code>window</code> object from being polluted with various unnecessary variables by wrapping the whole disqus include in an anonymous function.</p>

<p>However, the Disqus script actually injects another <code>&lt;script&gt;</code> element into the <code>&lt;head&gt;</code> (or <code>&lt;body&gt;</code>) of the page. This injected script, like all other scripts on the page, inherits the global (<code>window</code>) scope, and not the scope of this anonymous function. Thus it will not have access to the disqus variables such as <code>disqus_shortname</code> defined within the anonymous function. Without these variables, disqus loads such domains as <code>undefined.disqus.com</code> which is not very helpful!</p>

<h2>Solution</h2>

<p>The fix is simple - open up <code>source/_includes/disqus.html</code> and move the beginning of the anonymous function (<code>(function () {</code>) so that it does not contain the <code>var disqus_*</code> variable definitions. We also need to insert a couple of semi-colons that have been <abbr title="at the end of the `var disqus_script =` lines">accidentally omitted</abbr>. Here&#8217;s the resulting file:</p>

<figure class='code'><figcaption><span>Modified disqus include to fix disqus comment count. (disqus.html)</span> <a href='http://www.benjiegillam.com/code/source/_includes/disqus.html'>download</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>
</pre></td><td class='code'><pre><code class='html'><span class='line'>{% comment %} Load script if disquss comments are enabled and `page.comments` is either empty (index) or set to true {% endcomment %}
</span><span class='line'>{% if site.disqus_short_name and page.comments != false %}
</span><span class='line'><span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>      <span class="kd">var</span> <span class="nx">disqus_shortname</span> <span class="o">=</span> <span class="s1">&#39;{{ site.disqus_short_name }}&#39;</span><span class="p">;</span>
</span><span class='line'>      <span class="p">{</span><span class="o">%</span> <span class="k">if</span> <span class="nx">page</span><span class="p">.</span><span class="nx">comments</span> <span class="o">==</span> <span class="kc">true</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>        <span class="p">{</span><span class="o">%</span> <span class="nx">comment</span> <span class="o">%</span><span class="p">}</span> <span class="err">`</span><span class="nx">page</span><span class="p">.</span><span class="nx">comments</span><span class="err">`</span> <span class="nx">can</span> <span class="nx">be</span> <span class="nx">only</span> <span class="nx">be</span> <span class="nx">set</span> <span class="nx">to</span> <span class="kc">true</span> <span class="nx">on</span> <span class="nx">pages</span><span class="o">/</span><span class="nx">posts</span><span class="p">,</span> <span class="nx">so</span> <span class="nx">we</span> <span class="nx">embed</span> <span class="nx">the</span> <span class="nx">comments</span> <span class="nx">here</span><span class="p">.</span> <span class="p">{</span><span class="o">%</span> <span class="nx">endcomment</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>        <span class="c1">// var disqus_developer = 1;</span>
</span><span class='line'>        <span class="kd">var</span> <span class="nx">disqus_identifier</span> <span class="o">=</span> <span class="s1">&#39;{{ site.url }}{{ page.url }}&#39;</span><span class="p">;</span>
</span><span class='line'>        <span class="kd">var</span> <span class="nx">disqus_url</span> <span class="o">=</span> <span class="s1">&#39;{{ site.url }}{{ page.url }}&#39;</span><span class="p">;</span>
</span><span class='line'>        <span class="kd">var</span> <span class="nx">disqus_script</span> <span class="o">=</span> <span class="s1">&#39;embed.js&#39;</span><span class="p">;</span>
</span><span class='line'>      <span class="p">{</span><span class="o">%</span> <span class="k">else</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>        <span class="p">{</span><span class="o">%</span> <span class="nx">comment</span> <span class="o">%</span><span class="p">}</span> <span class="nx">As</span> <span class="err">`</span><span class="nx">page</span><span class="p">.</span><span class="nx">comments</span><span class="err">`</span> <span class="nx">is</span> <span class="nx">empty</span><span class="p">,</span> <span class="nx">we</span> <span class="nx">must</span> <span class="nx">be</span> <span class="nx">on</span> <span class="nx">the</span> <span class="nx">index</span> <span class="nx">page</span><span class="p">.</span> <span class="p">{</span><span class="o">%</span> <span class="nx">endcomment</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>        <span class="kd">var</span> <span class="nx">disqus_script</span> <span class="o">=</span> <span class="s1">&#39;count.js&#39;</span><span class="p">;</span>
</span><span class='line'>      <span class="p">{</span><span class="o">%</span> <span class="nx">endif</span> <span class="o">%</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>      <span class="kd">var</span> <span class="nx">dsq</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s1">&#39;script&#39;</span><span class="p">);</span> <span class="nx">dsq</span><span class="p">.</span><span class="nx">type</span> <span class="o">=</span> <span class="s1">&#39;text/javascript&#39;</span><span class="p">;</span> <span class="nx">dsq</span><span class="p">.</span><span class="nx">async</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'>      <span class="nx">dsq</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="s1">&#39;http://&#39;</span> <span class="o">+</span> <span class="nx">disqus_shortname</span> <span class="o">+</span> <span class="s1">&#39;.disqus.com/&#39;</span> <span class="o">+</span> <span class="nx">disqus_script</span><span class="p">;</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;head&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">||</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]).</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">dsq</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}());</span>
</span><span class='line'><span class="nt">&lt;/script&gt;</span>
</span><span class='line'>{% endif %}
</span></code></pre></td></tr></table></div></figure>


<h2>One more thing</h2>

<p><del>If you want your links to be absolute, then you should set <code>root</code> to be your root URL in <code>_config.yml</code> - e.g. <code>root: http://www.benjiegillam.com/</code> rather than <code>root: /</code>. This may or may not be required for Disqus to function properly.</del></p>

<p><strong>EDIT</strong>: @Octopress has confirmed that root must not contain the scheme and host.</p>

<h2>Bonus</h2>

<p>Now that I&#8217;ve implemented the above, my comments and comment counts successfully display on my <code>benjiegillam.dev</code> site (hosted by Pow)!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Sed to Replace Newlines]]></title>
    <link href="http://www.benjiegillam.com/2011/09/using-sed-to-replace-newlines/"/>
    <updated>2011-09-27T12:17:00+01:00</updated>
    <id>http://www.benjiegillam.com/2011/09/using-sed-to-replace-newlines</id>
    <content type="html"><![CDATA[<p>Annoyingly, Mac OS X&#8217;s sed is not 100% compatible with GNU sed, and so this solution from StackOverflow did not immediately solve my issue:</p>

<blockquote><p>Or use this solution with sed:<br/>  <code>sed ':a;N;$!ba;s/\n/ /g'</code><br/>This will read the whole file in a loop, then replaces the newline(s) with a space.</p><p>Update: explanation.</p><p><ol><li>create a register via <code>:a</code></li><li>append the current and next line to the register via <code>N</code></li><li>if we are before the last line, branch to the created register <code>$!ba</code> (<code>$!</code> means not to do it on the last line (as there should be one final newline)).</li><li>finally the substitution replaces every newline with a space on the pattern space (which is the contents of the a register = the whole file.</li></ol></p><footer><strong>Zsolt Botykai</strong> <cite><a href='http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n'>Sed: How Can I Replace a Newline (\n)?</a></cite></footer></blockquote>


<p>The solution I found was to split the command up into explicit separate commands:</p>

<figure class='code'><figcaption><span>Replacing newlines with spaces using sed on Mac OS X</span></figcaption><div clang='' id="figure_726223002"><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='bash'><span class='line'>sed -e <span class="s1">&#39;:a&#39;</span> -e <span class="s1">&#39;N&#39;</span> -e <span class="s1">&#39;$!ba&#39;</span> -e <span class="s1">&#39;s/\n/ /g&#39;</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_726223002_alt" style="display:none;"></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Octopress UTF-8 Issues]]></title>
    <link href="http://www.benjiegillam.com/2011/09/octopress-utf-8-issues/"/>
    <updated>2011-09-25T11:23:00+01:00</updated>
    <id>http://www.benjiegillam.com/2011/09/octopress-utf-8-issues</id>
    <content type="html"><![CDATA[<p>After running <code>exitwp</code> to <a href="http://www.benjiegillam.com/2011/09/from-wordpress-to-octopress/">import my blog</a>, I ran <code>rake generate</code> to build it, and got the following issue:</p>

<pre><code>$ rake generate
(in /Users/benjiegillam/Documents/Blog/octopress)
## Generating Site with Jekyll
Configuration from /Users/benjiegillam/Documents/Blog/octopress/_config.yml
unchanged sass/screen.scss
Building site: source -&gt; public
/Users/benjiegillam/Documents/Blog/octopress/plugins/raw.rb:11:in `gsub': invalid byte sequence in UTF-8 (ArgumentError)
    from /Users/benjiegillam/Documents/Blog/octopress/plugins/raw.rb:11:in `unwrap'
    [...]
</code></pre>

<p>However, converting the file was perfectly valid UTF-8 as confirmed by an <code>iconv -c</code> conversion followed by a <code>diff -u</code>.</p>

<p>After a quick bit of hacking in the <code>octopress/plugins/raw.rb</code> file to spit out the content that was being converted, I found the file at fault. After some iteration I got to the root of the issue - Octopress&#8217; default markdown parser, <code>rdiscount</code>, REALLY doesn&#8217;t like UTF-8 characters in URLs. I&#8217;ve built a test here:</p>

<!-- more -->




<figure class='code'><figcaption><span>rdiscount UTF-8 URL test (rdiscounttest.rb)</span> <a href='http://www.benjiegillam.com/code/rdiscounttest.rb'>download</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>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="c1"># encoding: utf-8</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;rdiscount&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nb">print</span> <span class="s2">&quot;Test 1: &quot;</span>
</span><span class='line'><span class="n">markdown</span> <span class="o">=</span> <span class="no">RDiscount</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">&quot;http://commons.wikipedia.org/wiki/Image:Dara_Ó_Briain.jpg&quot;</span><span class="p">)</span>
</span><span class='line'><span class="k">if</span> <span class="n">markdown</span><span class="o">.</span><span class="n">to_html</span> <span class="o">==</span> <span class="s2">&quot;&lt;p&gt;http://commons.wikipedia.org/wiki/Image:Dara&lt;em&gt;Ó&lt;/em&gt;Briain.jpg&lt;/p&gt;</span><span class="se">\n</span><span class="s2">&quot;</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;PASS&quot;</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'>  <span class="nb">print</span> <span class="s2">&quot;FAIL: &quot;</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="n">markdown</span><span class="o">.</span><span class="n">to_html</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="c1"># Correct: &lt;p&gt;http://commons.wikipedia.org/wiki/Image:Dara&lt;em&gt;Ó&lt;/em&gt;Briain.jpg&lt;/p&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nb">print</span> <span class="s2">&quot;Test 2: &quot;</span>
</span><span class='line'><span class="n">markdown</span> <span class="o">=</span> <span class="no">RDiscount</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">&quot;[Wikipedia](http://commons.wikipedia.org/wiki/Image:Dara_Ó_Briain.jpg)&quot;</span><span class="p">)</span>
</span><span class='line'><span class="k">if</span> <span class="n">markdown</span><span class="o">.</span><span class="n">to_html</span> <span class="o">==</span> <span class="s2">&quot;&lt;p&gt;&lt;a href=</span><span class="se">\&quot;</span><span class="s2">http://commons.wikipedia.org/wiki/Image:Dara_%C3%93_Briain.jpg</span><span class="se">\&quot;</span><span class="s2">&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;</span><span class="se">\n</span><span class="s2">&quot;</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="s2">&quot;PASS&quot;</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'>  <span class="nb">print</span> <span class="s2">&quot;FAIL: &quot;</span>
</span><span class='line'>  <span class="nb">puts</span> <span class="n">markdown</span><span class="o">.</span><span class="n">to_html</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="c1"># Incorrect: &lt;p&gt;&lt;a href=&quot;http://commons.wikipedia.org/wiki/Image:Dara_?%93_Briain.jpg&quot;&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;</span>
</span><span class='line'><span class="c1"># Note the invalid character (represented by ?)</span>
</span><span class='line'><span class="c1"># Expected: &lt;p&gt;&lt;a href=&quot;http://commons.wikipedia.org/wiki/Image:Dara_%C3%93_Briain.jpg&quot;&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>It was converting <code>Dara_Ó_Briain.jpg</code> to <code>Dara_?%93_Briain.jpg</code>, where the ? is an invalid UTF-8 character. (Should be <code>Dara_%C3%93_Briain.jpg</code>)</p>

<h2>Solution?</h2>

<p>Annoyingly <code>pandoc</code> (a tool employed by <code>exitwp</code>) seems to be converting the link from <code>Dara_%C3%93_Briain.jpg</code> to <code>Dara_Ó_Briain.jpg</code> in the markdown file, which is then breaking when it is <code>rdiscount</code>ed. As it only affected 2 characters in my entire blog history I&#8217;ve not bothered with an automated fix - I just manually re-encoded the characters. I&#8217;ve commented on <a href="https://github.com/jgm/pandoc/issues/284#issuecomment-2209098">this issue with pandoc</a> so that hopefully they will fix it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[From Wordpress to Octopress]]></title>
    <link href="http://www.benjiegillam.com/2011/09/from-wordpress-to-octopress/"/>
    <updated>2011-09-25T10:09:00+01:00</updated>
    <id>http://www.benjiegillam.com/2011/09/from-wordpress-to-octopress</id>
    <content type="html"><![CDATA[<p>I had a few issues going from Wordpress to Octopress (which I did <a href="https://github.com/thomasf/exitwp">using <code>exitwp</code></a>) - I&#8217;ve outlined
some of my solutions below.</p>

<h2>Crossed out code blocks</h2>

<p>The markdown produced by <code>pandoc</code> (an <code>exitwp</code> dependency) uses <code>~~~~</code> in places to denote a code block. However, <code>rdiscount</code> (Octopress&#8217; markdown parser) recognises this instead as strike-through. A simple <code>bash</code> command fixes this (though it does lose some of the formatting details).</p>

<figure class='code'><figcaption><span>Fix tildes generated by pandoc (exitwptildes.sh)</span> <a href='http://www.benjiegillam.com/code/exitwptildes.sh'>download</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>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="k">for </span>I in *; <span class="k">do </span>sed -i <span class="s1">&#39;&#39;</span> -e <span class="s1">&#39;s/^~~~~ {\(.*\)}$/\</span>
</span><span class='line'><span class="s1">&lt;!-- \1 --&gt;\</span>
</span><span class='line'><span class="s1">```/g&#39;</span> <span class="nv">$I</span>; <span class="k">done</span>
</span><span class='line'><span class="k">for </span>I in *; <span class="k">do </span>sed -i <span class="s1">&#39;&#39;</span> -e <span class="s1">&#39;s/^~~~~$/\</span>
</span><span class='line'><span class="s1">```/g&#39;</span> <span class="nv">$I</span>; <span class="k">done</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Line breaks in links</h2>

<p>For some reason <code>exitwp</code> added some linebreaks into the markup for the links. And for some other
reason, octopress&#8217; style sheet (<code>screen.css</code>) tells <code>&lt;a&gt;</code> tags that their <code>white-space</code> should be
<code>pre-wrap</code>, these two issues combine to give you broken links.</p>

<p>This next command should fix these newline issues. <!-- more --> (It finds a <code>[</code> with no matching <code>]</code> on the
same line and then removes the newline at the end of said line, being careful not to have two newlines next to one another.)</p>

<figure class='code'><figcaption><span>Fix newlines in links  </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>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="k">for </span>I in <span class="nb">source</span>/*.markdown <span class="nb">source</span>/*/*.markdown; <span class="k">do </span>sed -E -i <span class="s1">&#39;&#39;</span> -e <span class="s1">&#39;:a&#39;</span> -e <span class="s1">&#39;N&#39;</span> -e <span class="s1">&#39;$!ba&#39;</span> -e <span class="s1">&#39;s/\n?(\[[^]]*)\n([\t ]*)/\</span>
</span><span class='line'><span class="s1">\2\1 /g&#39;</span> <span class="s2">&quot;$I&quot;</span>; <span class="k">done</span>
</span></code></pre></td></tr></table></div></figure>


<p>NOTE: You may need to run the above more than once.</p>

<p>Turns out that the above broke some image embeds - no worries - here&#8217;s the solution:</p>

<figure class='code'><figcaption><span>Fix image embeds that were broken by previous command  </span></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='sh'><span class='line'><span class="k">for </span>I in *; <span class="k">do </span>sed -i <span class="s1">&#39;&#39;</span> -e <span class="s1">&#39;:a&#39;</span> -e <span class="s1">&#39;N&#39;</span> -e <span class="s1">&#39;$!ba&#39;</span> -e <span class="s1">&#39;s/\!\n\[/\![/g&#39;</span> <span class="nv">$I</span>; <span class="k">done</span>;
</span></code></pre></td></tr></table></div></figure>


<h2>Ordered/Unordered Lists</h2>

<p><code>exitwp</code> does not insert newlines before lists, and rdiscount does not always recognise it as a list unless it has a blank newline above it. Simply find the &#8216;1.&#8217; entry and put a blank newline above it.</p>

<h2>Tildes</h2>

<p><code>rdiscount</code> doesn&#8217;t do strikethrough with a single tilde, so it does not need escaping. However <code>exitwp</code> escapes them, so text like ~/Documents/ will become \~/Documents/</p>

<h2>Random quotes</h2>

<p><code>rdiscount</code> and <code>exitwp</code> seem to disagree on indentation - causing <code>rdiscount</code> to render indentation (sometimes) as a quote rather than continuation of the text.</p>

<h2>URLs with brackets in</h2>

<p><code>exitwp</code> or <code>pandoc</code> do not correctly escape (with a backslash) brackets within links, so captions/etc may be broken. Simply add in the backslashes.</p>

<h2>/blog/archives/</h2>

<p>I was looking for a line in <code>_config.yml</code> to configure the archives. Turns out it&#8217;s actually the file <code>source/blog/archives/index.html</code> that needs to move - I moved this to <code>source/archives/index.html</code></p>

<figure class='code'><figcaption><span>Change /blog/archives/ to just /archives/  </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>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>mv <span class="nb">source</span>/blog/archives <span class="nb">source</span>/
</span><span class='line'>rmdir <span class="nb">source</span>/blog/
</span></code></pre></td></tr></table></div></figure>


<p>And then updated the navigation <code>source/_includes/custom/navigation.html</code></p>

<h2>Hosting on S3</h2>

<p>I followed <a href="http://www.ianwootten.co.uk/2011/09/09/hosting-an-octopress-blog-on-amazon-s3">these instructions</a> from Ian Wootten (which reference <a href="http://www.jerome-bernard.com/blog/2011/08/20/quick-tip-for-easily-deploying-octopress-blog-on-amazon-s3/">these instructions</a> by Jerome Bernard) - thanks!</p>

<h2><em>Disclaimer</em></h2>

<p>This code in this post worked for me, but it is quite dumb - you will need to check the changes it
makes are good (this is where <code>git diff</code> comes in handy - make sure to commit before running these
commands!)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[NodeJS: Express/Socket.io/RedisStore headache]]></title>
    <link href="http://www.benjiegillam.com/2011/09/nodejs-expresssocket-ioredisstore-headache/"/>
    <updated>2011-09-02T17:35:59+01:00</updated>
    <id>http://www.benjiegillam.com/2011/09/nodejs-expresssocket-ioredisstore-headache</id>
    <content type="html"><![CDATA[<p>If you&#8217;re trying to access a session from a socket.io socket, I
recommend following
<a href="http://www.danielbaulig.de/socket-ioexpress/">these instructions</a> which work
great for <code>MemoryStore</code>. However for <code>RedisStore</code> you need to do a
little more work because the object returned is just a JSON object - not
a <code>Session</code> object as it should be. I&#8217;ve hacked around it by adding the
following (CoffeeScript) to convert it to a <code>Session</code> object if
required:</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_932423557');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_932423557');">.js</a></span><span>Make sure &#8216;session&#8217; is a Session object</span></figcaption><div clang='CoffeeScript' id="figure_932423557"><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='coffee-script'><span class='line'><span class="k">if</span> <span class="nx">session</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">session</span><span class="p">.</span><span class="nx">prototype</span><span class="o">?</span><span class="p">.</span><span class="nx">reload</span>
</span><span class='line'>    <span class="nv">req =</span>
</span><span class='line'>        <span class="nv">sessionStore: </span><span class="nx">sessionStore</span>
</span><span class='line'>        <span class="nv">sessionID: </span><span class="nx">data</span><span class="p">.</span><span class="nx">sessionID</span>
</span><span class='line'>    <span class="nv">session = </span><span class="k">new</span> <span class="nx">express</span><span class="p">.</span><span class="nx">session</span><span class="p">.</span><span class="nx">Session</span> <span class="nx">req</span><span class="p">,</span> <span class="nx">session</span>
</span></code></pre></td></tr></table></div></div><div clang='JavaScript' id="figure_932423557_alt" style="display:none;"><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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">req</span><span class="p">,</span> <span class="nx">session</span><span class="p">,</span> <span class="nx">_ref</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="nx">session</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="p">((</span><span class="nx">_ref</span> <span class="o">=</span> <span class="nx">session</span><span class="p">.</span><span class="nx">prototype</span><span class="p">)</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span> <span class="nx">_ref</span><span class="p">.</span><span class="nx">reload</span> <span class="o">:</span> <span class="k">void</span> <span class="mi">0</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">req</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">sessionStore</span><span class="o">:</span> <span class="nx">sessionStore</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">sessionID</span><span class="o">:</span> <span class="nx">data</span><span class="p">.</span><span class="nx">sessionID</span>
</span><span class='line'>  <span class="p">};</span>
</span><span class='line'>  <span class="nx">session</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">express</span><span class="p">.</span><span class="nx">session</span><span class="p">.</span><span class="nx">Session</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">session</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></div></figure>


<p>immediately inside the <code>sessionStore.get</code> callback.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Node.JS: Clean Restart and Faster Development with nodemon]]></title>
    <link href="http://www.benjiegillam.com/2011/08/node-js-clean-restart-and-faster-development-with-nodemon/"/>
    <updated>2011-08-31T16:35:22+01:00</updated>
    <id>http://www.benjiegillam.com/2011/08/node-js-clean-restart-and-faster-development-with-nodemon</id>
    <content type="html"><![CDATA[<p>If you&#8217;re using <code>nodemon</code> to automatically restart your node/coffee
server process when the source code changes (and if you&#8217;re not, why
not?), you may find (as I did) that it can kill the script quite
abruptly. If you want to do something when the code shuts down (close
connections, flush to database/disk/redis/memcached, etc) then you can
do so by intercepting the <code>SIGUSR2</code> signal that <code>nodemon</code> uses to kill
your script, and then setting up these actions. Once these actions are
complete, you can exit but <code>nodemon</code> will see that as a crash and stop
restarting. So you then have to kill your app again with <code>SIGUSR2</code>.
Here&#8217;s the solution I use in CoffeeScript (click Toggle for JavaScript):</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_644674153');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_644674153');">.js</a></span><span>Cleanly restart on nodemon SIGUSR2 (.coffee)</span></figcaption><div clang='CoffeeScript' id="figure_644674153"><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>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="c1"># Alias for setTimeout that reorders parameters to look neater in CoffeeScript</span>
</span><span class='line'><span class="nv">delay = </span><span class="nf">(ms, cb) -&gt;</span> <span class="nx">setTimeout</span> <span class="nx">cb</span><span class="p">,</span> <span class="nx">ms</span>
</span><span class='line'><span class="c1"># Handle SIGUSR2, but only once</span>
</span><span class='line'><span class="nx">process</span><span class="p">.</span><span class="nx">once</span> <span class="s">&#39;SIGUSR2&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;Doing shutdown tasks...&#39;</span>
</span><span class='line'>  <span class="nx">delay</span> <span class="mi">5000</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;All done, exiting&#39;</span>
</span><span class='line'>    <span class="c1"># Kill ourself with the SIGUSR2 signal again</span>
</span><span class='line'>    <span class="nx">process</span><span class="p">.</span><span class="nx">kill</span> <span class="nx">process</span><span class="p">.</span><span class="nx">pid</span><span class="p">,</span> <span class="s">&#39;SIGUSR2&#39;</span>
</span><span class='line'><span class="c1"># This next line just to prevent the app exiting in case you want to use this as a demo</span>
</span><span class='line'><span class="nx">delay</span> <span class="mi">99999999</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;App exiting naturally&#39;</span>
</span></code></pre></td></tr></table></div></div><div clang='JavaScript' id="figure_644674153_alt" style="display:none;"><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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">delay</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">delay</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">ms</span><span class="p">,</span> <span class="nx">cb</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">cb</span><span class="p">,</span> <span class="nx">ms</span><span class="p">);</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="nx">process</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="s1">&#39;SIGUSR2&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Doing shutdown tasks...&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">delay</span><span class="p">(</span><span class="mi">5000</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;All done, exiting&#39;</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="nx">process</span><span class="p">.</span><span class="nx">kill</span><span class="p">(</span><span class="nx">process</span><span class="p">.</span><span class="nx">pid</span><span class="p">,</span> <span class="s1">&#39;SIGUSR2&#39;</span><span class="p">);</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'>
</span><span class='line'><span class="nx">delay</span><span class="p">(</span><span class="mi">99999999</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;App exiting naturally&#39;</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></div></figure>


<p>By the way, to install nodemon globally: <code>sudo npm install -g nodemon</code></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to interface your Cocoa app with Skype]]></title>
    <link href="http://www.benjiegillam.com/2011/08/how-to-interface-your-cocoa-app-with-skype/"/>
    <updated>2011-08-25T15:22:02+01:00</updated>
    <id>http://www.benjiegillam.com/2011/08/how-to-interface-your-cocoa-app-with-skype</id>
    <content type="html"><![CDATA[<p><strong>Update: </strong> Give up now. Skype
<a href="http://forum.skype.com/index.php?showtopic=820705">never</a>
<a href="https://jira.skype.com/browse/SPA-749">responds</a> to the
<code>[SkypeAPI connect]</code> method!</p>

<p>Original post follows:</p>

<!-- more -->


<p>(Mac OS X only, works on Lion, Xcode 4.1,
Skype 5.3)</p>

<p>Here are the steps to starting work on interfacing a Cocoa
app with Skype:</p>

<ol>
<li><p>Don&#8217;t try and get the framework from the documentation (what the hell?!
Invalid link? PowerPC issues? Come on Skype!)</p></li>
<li><p>Copy <code>Skype.framework</code> into your app&#8217;s folder from within Skype (find
the Skype app, control-click and Show Package Contents)</p></li>
<li><p>Drag your copy of <code>Skype.framework</code> into the <code>Frameworks</code> folder of your
Xcode project.</p></li>
<li><p>In Project -> Target -> Build Phases:</p>

<ol>
<li>Ensure Link Binary with Libraries contains <code>Skype.framework</code></li>
<li>Add a new build phase (Editor -> Add Build Phase -> New Copy Files
Build Phase), set the destination to Frameworks, the Subpath blank,
and drag <code>Skype.framework</code> here from your <code>Frameworks</code> group.</li>
</ol>
</li>
<li><p>In the relevant source file add</p></li>
</ol>


<figure class='code'><figcaption><span></span></figcaption><div clang='' id="figure_137162016"><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="cp">#include &lt;Skype/Skype.h&gt;</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_137162016_alt" style="display:none;"></div></figure>


<ol>
<li>Code your app. You might want to start with the following:</li>
</ol>


<figure class='code'><figcaption><span></span></figcaption><div clang='' id="figure_535651046"><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">if</span> <span class="p">([</span><span class="n">SkypeAPI</span> <span class="n">isSkypeRunning</span><span class="p">])</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;Skype is running.&quot;</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_535651046_alt" style="display:none;"></div></figure>


<p>Hopefully that&#8217;ll get you started a bit faster than I did! API
documentation can be found here:
<a href="https://developer.skype.com/resources/public_api_ref.zip" title="Skype Accessory API">https://developer.skype.com/resources/public_api_ref.zip</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Manually Migrating FileVault]]></title>
    <link href="http://www.benjiegillam.com/2011/07/manually-migrating-filevault/"/>
    <updated>2011-07-22T01:00:34+01:00</updated>
    <id>http://www.benjiegillam.com/2011/07/manually-migrating-filevault</id>
    <content type="html"><![CDATA[<p><strong>DISCLAIMER</strong>: As always, you follow this at your own risk - keep good
backups, I take no responsibility for you doing anything whatsoever -
you&#8217;re a big boy/girl now, take responsibility for your own actions!</p>

<p>So you can&#8217;t use MigrationAssistant because you already have an account
(<code>Alice</code>) on the new machine (<code>New</code>) and don&#8217;t have the disk space on the
old one (<code>Old</code>) to turn FV off? Fear not! Let&#8217;s assume the username you
want to move is <code>steve</code>:</p>

<ol>
<li>As <code>Alice</code>, create (admin) account on <code>New</code> with
same username, <code>steve</code>, and same password, and enable FileVault.</li>
<li>Replace <code>/Users/steve/steve.sparsebundle</code> on <code>New</code> with
<code>/Users/.steve/steve.sparsebundle</code> from <code>Old</code>. (you could use <code>scp</code>, <code>rsync</code>, an
external hard drive, or any other method to transfer these files) (I did
this with <code>steve</code> logged in, but you can do it logged out though the path
may be different)</li>
<li>As <code>Alice</code>, fix the permissions on <code>New</code>: <code>chown -R steve /Users/steve</code></li>
<li>Log in as <code>steve</code> on <code>New</code> (unlocks the sparsebundle
and mounts it at <code>/Users/steve</code>)</li>
<li>Fix permissions again: either as <code>Alice</code>
or <code>steve</code>, <code>sudo chown -R steve /Users/steve</code></li>
<li>Log out an in again Voila!</li>
</ol>


<p>If this helps you, please drop me a comment :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[JavaScript: Defining a window level function with eval()]]></title>
    <link href="http://www.benjiegillam.com/2011/04/javascript-defining-a-window-level-function-with-eval/"/>
    <updated>2011-04-14T13:05:39+01:00</updated>
    <id>http://www.benjiegillam.com/2011/04/javascript-defining-a-window-level-function-with-eval</id>
    <content type="html"><![CDATA[<p>You know the old mantra: <code>eval</code> is evil! Well, maybe, but I hate writing
lots of code, and I like my debugging to be simple. I wanted to write a
shorter way to extend a parent class into a child class. Long way (too
much repeated code/risk of copy &amp; paste errors):</p>

<figure class='code'><figcaption><span>Long winded: manually creating each class from scratch</span></figcaption><div clang='' id="figure_88564284"><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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="c1">// Root class</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">MyClass</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Create first subclass</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">MyChildClass1</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="nx">MyChildClass1</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyClass</span><span class="p">();</span>
</span><span class='line'><span class="nx">MyChildClass1</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">parent</span> <span class="o">=</span> <span class="nx">MyClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
</span><span class='line'><span class="nx">MyChildClass1</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">=</span> <span class="nx">MyChildClass1</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Create second subclass</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">MyChildClass2</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="nx">MyChildClass2</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyClass</span><span class="p">();</span>
</span><span class='line'><span class="nx">MyChildClass2</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">parent</span> <span class="o">=</span> <span class="nx">MyClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
</span><span class='line'><span class="nx">MyChildClass2</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">=</span> <span class="nx">MyChildClass2</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Create an instance</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyChildClass2</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_88564284_alt" style="display:none;"></div></figure>


<p>Shorter way (gives wrong output on <code>console.log()</code>):</p>

<!--more-->




<figure class='code'><figcaption><span>Shorter, but class names are console.log&#8217;d incorrectly</span></figcaption><div clang='' id="figure_94728414"><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='javascript'><span class='line'><span class="c1">// Automation function</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">NS_Extend</span><span class="p">(</span><span class="nx">parentClass</span><span class="p">,</span><span class="nx">childClassName</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">childClass</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span><span class="k">return</span> <span class="k">this</span><span class="p">;}</span>
</span><span class='line'>  <span class="nx">childClass</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">parentClass</span><span class="p">();</span>
</span><span class='line'>  <span class="nx">childClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">parent</span> <span class="o">=</span> <span class="nx">parentClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">childClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">=</span> <span class="nx">childClass</span><span class="p">;</span>
</span><span class='line'>  <span class="nb">window</span><span class="p">[</span><span class="nx">childClassName</span><span class="p">]</span> <span class="o">=</span> <span class="nx">childClass</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Root Class</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">MyClass</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// First subclass</span>
</span><span class='line'><span class="nx">NS_Extend</span><span class="p">(</span><span class="nx">MyClass</span><span class="p">,</span><span class="s1">&#39;MyChildClass1&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Second subclass</span>
</span><span class='line'><span class="nx">NS_Extend</span><span class="p">(</span><span class="nx">MyClass</span><span class="p">,</span><span class="s1">&#39;MyChildClass2&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Create an instance</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyChildClass2</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_94728414_alt" style="display:none;"></div></figure>


<p>Simple, but when I <code>console.log(instance)</code> it gives me
<code>NS_Extend.childClass</code> as the name of the class, rather than
<code>MyChildClass2</code> as was done previously. It makes no difference to the
functionality of the app, but it makes my debugging a bit harder. And I
really don&#8217;t want to have to keep defining
<code>function ClassName(){return this;}</code> everywhere - waste of time typing
all that repeated code! So I figured I&#8217;d define the function through
<code>eval()</code>. However that didn&#8217;t work, until I realised that <code>eval()</code> is
scoped to the current context. So to define a window level function we&#8217;d
need to change the scope to the <code>window</code> object. Easy enough with
JavaScript&#8217;s <code>.call()</code> method. The solution:</p>

<figure class='code'><figcaption><span>Eval isn&#8217;t always evil: Fixing the console.log class names.</span></figcaption><div clang='' id="figure_719032441"><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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">MyClass</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">NS_Extend</span><span class="p">(</span><span class="nx">parentClass</span><span class="p">,</span><span class="nx">childClassName</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">js</span> <span class="o">=</span> <span class="s1">&#39;function &#39;</span><span class="o">+</span><span class="nx">childClassName</span><span class="o">+</span><span class="s1">&#39;(){return this;}&#39;</span><span class="p">;</span>
</span><span class='line'>  <span class="nb">eval</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nb">window</span><span class="p">,</span><span class="nx">js</span><span class="p">);</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">childClass</span> <span class="o">=</span> <span class="nb">window</span><span class="p">[</span><span class="nx">childClassName</span><span class="p">];</span>
</span><span class='line'>  <span class="nx">childClass</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">parentClass</span><span class="p">();</span>
</span><span class='line'>  <span class="nx">childClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">parent</span> <span class="o">=</span> <span class="nx">parentClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">childClass</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">=</span> <span class="nx">childClass</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="nx">NS_Extend</span><span class="p">(</span><span class="nx">MyClass</span><span class="p">,</span><span class="s1">&#39;MyChildClass1&#39;</span><span class="p">);</span>
</span><span class='line'><span class="nx">NS_Extend</span><span class="p">(</span><span class="nx">MyClass</span><span class="p">,</span><span class="s1">&#39;MyChildClass2&#39;</span><span class="p">);</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MyChildClass2</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_719032441_alt" style="display:none;"></div></figure>


<p>Now when I <code>console.log(instance)</code> it tells me <code>MyChildClass2</code> - WIN!</p>

<p>[I use Chrome under Linux, by the way.]</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Backup Your Logs to S3]]></title>
    <link href="http://www.benjiegillam.com/2010/12/backup-your-logs-to-s3/"/>
    <updated>2010-12-13T15:17:42+00:00</updated>
    <id>http://www.benjiegillam.com/2010/12/backup-your-logs-to-s3</id>
    <content type="html"><![CDATA[<p>We&#8217;ve got an auto-scaling EC2 setup, so servers can appear and disappear
at will, each with random IP addresses. We want to store the apache
error/access logs (or system logs) from these servers for later
debugging and analysis, but nothing else on them matters (they&#8217;re
basically read only, except the logging). How should we store these?</p>

<p>Seems like a simple problem, there must be a simple solution? Here are the existing options:</p>

<!-- more -->


<ul>
<li><strong>syslog-ng</strong> or <strong>rsyslogd</strong> logging to a central location.

<ul>
<li>Pros: centralized logs, easy to analyse, near to real time</li>
<li>Cons: Requires extra dedicated server (costs). Have to extend
storage or upload to S3 periodically. PITA setup from a security POV
unless you know the IP addresses in advance (you can set up your own
CA and generate a number of SSL keys and distribute these to the
servers on startup and use TLS encrypted communications, but I
really CBA with all that hassle - not to mention that last time I
checked Amazon Linux didn&#8217;t support these servers <abbr title="Out of the box">OOTB</abbr> so I&#8217;d have
to install them from a CentOS RPM or similar)</li>
</ul>
</li>
<li><strong>Message queuing</strong>

<ul>
<li>(I don&#8217;t think this is a perfect match)</li>
</ul>
</li>
<li><strong>Hadoop cluster</strong>

<ul>
<li>Pros: awesome data crunching ability</li>
<li>Cons: we don&#8217;t have 10million users yet, I think this is a bit
heavy handed (not to mention expensive)</li>
</ul>
</li>
<li><strong>Scribe</strong> from Facebook

<ul>
<li>Cons: too much setup, requires specific server heirarchy.</li>
</ul>
</li>
<li><strong>logg.ly</strong> and <strong>splunk</strong>

<ul>
<li>Cons: too expensive/untested/unknown for now</li>
</ul>
</li>
<li><strong>Something else</strong>&#8230;</li>
</ul>


<p>I chose &#8220;something else&#8221; - a custom solution using logrotate to upload
the logs to S3 on a per server basis. Here&#8217;s how I did it (and how you
can do it too) You&#8217;ll need:</p>

<ul>
<li><code>/usr/local/bin/s3uploadfile</code> <a href="http://www.benjiegillam.com/code/s3uploadfile">download</a> - a script that uploads a file to
S3&#8230;</li>
<li><code>/etc/init.d/s3logrotate</code> <a href="http://www.benjiegillam.com/code/s3logrotate">download</a> - an init script to trigger <code>logrotate</code> to be called (twice) on server shutdown</li>
<li><code>/usr/local/bin/s3logrotated</code> <a href="http://www.benjiegillam.com/code/s3logrotated">download</a> - a callback script to call when a log
has been rotated (process the logrotate message and calls
<code>s3uploadfile</code> to upload the log to S3)</li>
<li><code>/root/.s3env</code> - a simple bash script to export your s3 credentials
for <code>s3uploadfile</code> to work</li>
</ul>


<figure class='code'><figcaption><span>/root/.s3env</span></figcaption><div clang='' id="figure_967091492"><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='bash'><span class='line'><span class="c">#!/bin/bash</span>
</span><span class='line'><span class="nb">export </span><span class="nv">S3BUCKETNAME</span><span class="o">=</span><span class="s1">&#39;MyBucketNameHere&#39;</span>
</span><span class='line'><span class="nb">export </span><span class="nv">AWS_ACCESS_KEY_ID</span><span class="o">=</span><span class="s1">&#39;MyKeyHere&#39;</span>
</span><span class='line'><span class="nb">export </span><span class="nv">AWS_SECRET_ACCESS_KEY</span><span class="o">=</span><span class="s1">&#39;MySecretHere&#39;</span>
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_967091492_alt" style="display:none;"></div></figure>


<p><strong>Step 1</strong>: Create your dedicated server log S3 bucket (if you haven&#8217;t
already)</p>

<p><strong>Step 2</strong>: Download and install the scripts above to the
locations above, including <code>/root/.s3env</code> and then make them all executable (<code>chmod +x [file]</code>)</p>

<p><strong>Step 3</strong>: Modify the relevant logrotate scripts to use <code>s3logrotated</code>
callback and add <code>s3logrotate</code> to the server <code>rc*.d</code> scripts. When
configuring <code>logrotate</code>: don&#8217;t use <code>dateext</code>, do use <code>compress</code>, do use
<code>delaycompress</code>, do use <code>sharedscripts</code>. Here&#8217;s what I use:</p>

<figure class='code'><figcaption><span>Commands I run to configure logrotate/etc, REQUIRE MODIFICATION</span></figcaption><div clang='' id="figure_279495600"><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='bash'><span class='line'><span class="c"># This is what I used, you will need to customize this for your own servers</span>
</span><span class='line'>sed -i /etc/logrotate.conf -e <span class="s1">&#39;s/^dateext$/#dateext/;s/^weekly$/daily/;s/^rotate 4$/rotate 15/;s/^#compress$/compress/&#39;</span>
</span><span class='line'>sed -i /etc/logrotate.d/httpd -e <span class="s1">&#39;s/^\([ \t]*endscript\)/\t\/usr\/local\/bin\/s3logrotated &quot;.2.gz&quot; &quot;\$1&quot;\n\1/&#39;</span>
</span><span class='line'>sed -i /etc/logrotate.d/syslog -e <span class="s1">&#39;s/^\([ \t]*sharedscripts\)/\1\n    delaycompress/;s/^\([ \t]*endscript\)/\t\/usr\/local\/bin\/s3logrotated &quot;.2.gz&quot; &quot;\$1&quot;\n\1/&#39;</span>
</span><span class='line'><span class="c"># This is the centos command to add the s3logrotate script (it contains the levels configuration within it) - </span>
</span><span class='line'><span class="c"># you&#39;ll want to update-rc.d or similar on debian/ubuntu hosts.</span>
</span><span class='line'>chkconfig --add s3logrotate
</span></code></pre></td></tr></table></div></div><div clang='' id="figure_279495600_alt" style="display:none;"></div></figure>


<p>Note that the <code>".2.gz"</code> is because of <code>compress</code> and <code>delaycompress</code> -
it is the first compressed log file. This is why we need
<code>/etc/init.d/s3logrotate</code> to run &#8221;<code>logrotate -f</code>&#8221; twice on server
shutdown.</p>

<p><strong>Step 4</strong>: Analytics I suppose, but I leave that as an
exercise for the reader. I hope this helps someone - if it does, let me
know in the comments. If you want any help setting it up (or
understanding how it works) then just shoot me an email or leave me a
comment.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dear Virgin Media...]]></title>
    <link href="http://www.benjiegillam.com/2010/10/dear-virgin-media/"/>
    <updated>2010-10-06T10:30:38+01:00</updated>
    <id>http://www.benjiegillam.com/2010/10/dear-virgin-media</id>
    <content type="html"><![CDATA[<p>I think I <strong>do</strong> have fibre optic at my house already, even though it does
not show up on your system. The previous resident had V+HD, and they
left their equipment, so I&#8217;ve plugged it in to show the system working:</p>

<!-- more -->


<p>The cable comes out of the wall:</p>

<p><a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot842.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot842-202x300.jpg" title="screenshot842" alt="image" /></a></p>

<p>And then via a splitter:</p>

<p><a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot843.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot843.jpg" title="screenshot843" alt="image" /></a>
<a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot844.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot844-300x142.jpg" title="screenshot844" alt="image" /></a></p>

<p>To the V+HD box:</p>

<p><a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot845.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot845-300x107.jpg" title="screenshot845" alt="image" /></a>
<a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot846.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot846-300x181.jpg" title="screenshot846" alt="image" /></a>
<a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot847.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot847-300x194.jpg" title="screenshot847" alt="image" /></a></p>

<p>Note that the &#8220;online&#8221; LED is lit up, suggesting the system might be
online? And the modem:</p>

<p><a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot848.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot848-300x195.jpg" title="screenshot848" alt="image" /></a></p>

<p>(it&#8217;s not clear here because my flash drowned it out, but the &#8220;enet&#8221;,
&#8220;sync&#8221; and &#8220;ready&#8221; indicators are all green, and the &#8220;recv&#8221; and &#8220;send&#8221;
indicators flash when I load the website below.)</p>

<p><a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot849.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot849-300x254.jpg" title="screenshot849" alt="image" /></a></p>

<p>(It&#8217;s an ambit 256 modem)</p>

<p><a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot852.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot852-300x143.jpg" title="screenshot852" alt="image" /></a></p>

<p>(Note that <strong>cable</strong> plugs in the back.) Here&#8217;s what happens when I
connect my laptop to the modem:</p>

<ol>
<li>I am given an IP address of
213.107.40.198 (I have proof this is an NTL/Virgin Media IP address at
the bottom of the post)</li>
</ol>


<!-- style="color:green;font-weight:bold;" -->


<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>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>jem@jem-laptop:~$ ifconfig
</span><span class='line'>eth0      Link encap:Ethernet  HWaddr 00:22:15:8f:a5:50  
</span><span class='line'>          inet addr:&lt;strong&gt;213.107.40.198&lt;/strong&gt;  Bcast:213.107.40.255  Mask:255.255.255.0
</span><span class='line'>          inet6 addr: fe80::222:15ff:fe8f:a550/64 Scope:Link
</span><span class='line'>          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
</span><span class='line'>          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
</span><span class='line'>          TX packets:43 errors:0 dropped:0 overruns:0 carrier:2
</span><span class='line'>          collisions:0 txqueuelen:1000 
</span><span class='line'>          RX bytes:2606 (2.6 KB)  TX bytes:9440 (9.4 KB)
</span><span class='line'>          Interrupt:28 
</span><span class='line'>lo        Link encap:Local Loopback  
</span><span class='line'>          inet addr:127.0.0.1  Mask:255.0.0.0
</span><span class='line'>          inet6 addr: ::1/128 Scope:Host
</span><span class='line'>          UP LOOPBACK RUNNING  MTU:16436  Metric:1
</span><span class='line'>          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
</span><span class='line'>          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
</span><span class='line'>          collisions:0 txqueuelen:0 
</span><span class='line'>          RX bytes:800 (800.0 B)  TX bytes:800 (800.0 B)
</span></code></pre></td></tr></table></div></figure>


<ol>
<li>I am taken to the following screen, asking me to activate broadband
 <a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot853.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot853-1024x192.jpg" title="screenshot853" alt="image" /></a></li>
<li>When I click the link, it asks me to choose between Virgin Media and
ntl/telewest
 <a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot854.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot854-1024x249.jpg" title="screenshot854" alt="image" /></a></li>
<li><p>I choose virginmedia and the page asks me to phone you:
 <a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot855.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot855.jpg" title="screenshot855" alt="image" /></a></p>

<p> Here&#8217;s what I see from the V+HD box:</p>

<p> <a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot856.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot856-300x216.jpg" title="screenshot856" alt="image" /></a>
 <a href="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot857.jpg"><img src="http://www.benjiegillam.com/wp-content/uploads/2010/10/screenshot857-300x128.jpg" title="screenshot857" alt="image" /></a></p></li>
</ol>


<p>Here&#8217;s a
<a href="http://www.youtube.com/watch?v=CQ38AfIRWns">video of the V+HD box in action</a>, showing the
terrestrial channels delivered over cable, but the premium channels
cannot be viewed because I don&#8217;t have a subscription: The radio channels
also work for me through the V+HD box as I guess they are unprotected.</p>

<iframe width="560" height="315" src="http://www.youtube.com/embed/CQ38AfIRWns" frameborder="0" allowfullscreen></iframe>


<p><strong>In conclusion: please believe me now that I do in fact already have
fibre optic at this house and let me sign up!</strong></p>

<hr />

<p>Proof that the IP address I was given is a Virgin Media (NTL) address:</p>

<pre><code>$ whois 213.107.40.198
% This is the RIPE Database query service.
% The objects are in RPSL format.
%
% The RIPE Database is subject to Terms and Conditions.
% See http://www.ripe.net/db/support/db-terms-conditions.pdf

% Note: This output has been filtered.
%       To receive output for a database update, use the "-B" flag.

% Information related to '213.107.40.0 - 213.107.45.255'

inetnum:        213.107.40.0 - 213.107.45.255
netname:        NTL-BIA-LONDON-BROMLEY
descr:          NTL Infrastructure - Southampton
country:        GB
admin-c:        NNMC1-RIPE
tech-c:         NNMC1-RIPE
status:         ASSIGNED PA
mnt-by:         AS5089-MNT
source:         RIPE # Filtered

role:           NTLI Network Management Centre
address:        NTL Internet
address:        Crawley Court
address:        Winchester
address:        Hampshire
address:        SO21 2QA
phone:          +44 1633710142
admin-c:        MH22007-RIPE
admin-c:        NR731-RIPE
admin-c:        CW1083-RIPE
tech-c:         MH22007-RIPE
tech-c:         CW1083-RIPE
nic-hdl:        NNMC1-RIPE
mnt-by:         AS5089-MNT
e-mail:         pim@virginmedia.co.uk
source:         RIPE # Filtered

% Information related to '213.104.0.0/14AS5089'

route:        213.104.0.0/14
descr:        NTL-UK-IP-BLOCK-4
origin:       AS5089
mnt-by:       AS5089-MNT
source:       RIPE # Filtered

% Information related to '213.107.0.0/16AS5089'

route:        213.107.0.0/16
descr:        NTL-UK-IP-BLOCK
origin:       AS5089
mnt-by:       AS5089-MNT
source:       RIPE # Filtered

% Information related to '213.107.0.0/17AS5089'

route:        213.107.0.0/17
descr:        NTL-UK-IP-BLOCK
origin:       AS5089
mnt-by:       AS5089-MNT
source:       RIPE # Filtered
</code></pre>
]]></content>
  </entry>
  
</feed>

