<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rigel Group</title>
	<atom:link href="http://rigelgroupllc.com/wp/feed" rel="self" type="application/rss+xml" />
	<link>http://rigelgroupllc.com/wp</link>
	<description>Software Engineering</description>
	<lastBuildDate>Wed, 18 Apr 2012 15:41:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Batmanjs mixin for cycling CSS class names</title>
		<link>http://rigelgroupllc.com/wp/blog/a-batmanjs-mixin-for-cycling-css-class-names</link>
		<comments>http://rigelgroupllc.com/wp/blog/a-batmanjs-mixin-for-cycling-css-class-names#comments</comments>
		<pubDate>Wed, 18 Apr 2012 04:45:44 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[batmanjs]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=228</guid>
		<description><![CDATA[Recently we needed to cycle through some CSS class names when rendering a Batmanjs template. Something like alternating 'odd' and 'even' classes on table rows, for example. This turned out to be harder than I had first thought it would be. I tried several approaches, but this was the one that I finally got to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently we needed to cycle through some CSS class names when rendering a <a href="http://batmanjs.org" title="Batmanjs" target="_blank">Batmanjs</a> template. Something like alternating 'odd' and 'even' classes on table rows, for example. This turned out to be harder than I had first thought it would be. I tried several approaches, but this was the one that I finally got to work.</p>
<p>First, create a Cycler class. (Using a Batman object for this is probably overkill, but it does give me one more opportunity to type 'Batman'.)</p>

<div class="wp_syntax"><div class="code"><pre class="coffeescript" style="font-family:monospace;">MyApp.Cycler extends Batman.Object
  isObservable: false
&nbsp;
  constructor: (@values) -&gt;
    @values = @values.split(',') if typeof @values is 'string'
    @idx = -1
&nbsp;
  reset: -&gt;
    @idx = -1
&nbsp;
  next: -&gt;
    @idx = (@idx + 1) % @values.length
    @values[@idx]</pre></div></div>

<p>Now we create the mixin...</p>

<div class="wp_syntax"><div class="code"><pre class="coffeescript" style="font-family:monospace;"># Use a global property to hold the cycler, which holds the state
Batman.mixins.cycler =
  initialize: -&gt;
    $node = $(@)
    cyclerName = &quot;cycler-#{$node.data('cycler-name')}&quot;
    MyApp.getOrSet(cyclerName, =&gt; new MyApp.Cycler($node.data('cycler-values')))
    $node.addClass(MyApp.get(cyclerName).next())</pre></div></div>

<p>Now, to use it in your template, you can do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
  &lt;li data-foreach-post=&quot;posts&quot; data-mixin=&quot;cycler&quot; data-cycler-values=&quot;left,right&quot; data-cycler-name=&quot;all-posts&quot;&gt;
    &lt;a data-route=&quot;post&quot; data-bind=&quot;post.title&quot;&gt;&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;</pre></div></div>

<p>Which would render to something like this (leaving out the batman tags for clarity)</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
  &lt;li class=&quot;left&quot;&gt;
    &lt;a href=&quot;/posts/1&quot;&gt;Post 1&lt;/a&gt;
  &lt;/li&gt;
  &lt;li class=&quot;right&quot;&gt;
    &lt;a href=&quot;/posts/2&quot;&gt;Post 2&lt;/a&gt;
  &lt;/li&gt;
  &lt;li class=&quot;left&quot;&gt;
    &lt;a href=&quot;/posts/3&quot;&gt;Post 3&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;</pre></div></div>

<p>So, that's it. As you can see we have to create some global properties, and name them, so that we can have multiple cyclers in the app without conflict. This does bother me a bit, but there are plenty of other dragons to slay, so it will have to do for the time being.</p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/a-batmanjs-mixin-for-cycling-css-class-names/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Batmanjs binding for executing &lt;script&gt; blocks in views.</title>
		<link>http://rigelgroupllc.com/wp/blog/batmanjs-binding-for-executing-script-blocks-in-views</link>
		<comments>http://rigelgroupllc.com/wp/blog/batmanjs-binding-for-executing-script-blocks-in-views#comments</comments>
		<pubDate>Mon, 02 Apr 2012 18:02:14 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[batmanjs]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=213</guid>
		<description><![CDATA[You may have noticed that if you add a &#60;script&#62; block to your Batmanjs views, it never gets executed. This binding will look for any &#60;script&#62; nodes that are immediate children and executes them.]]></description>
			<content:encoded><![CDATA[<p>You may have noticed that if you add a &lt;script&gt; block to your <a href="http://batmanjs.org" target="_blank">Batmanjs</a> views, it never gets executed. This binding will look for any &lt;script&gt; nodes that are immediate children and executes them.</p>
<p><script src="https://gist.github.com/2017532.js?file=Execscripts.coffee"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/batmanjs-binding-for-executing-script-blocks-in-views/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connecting to POW via SSL</title>
		<link>http://rigelgroupllc.com/wp/blog/connecting-to-pow-via-ssl</link>
		<comments>http://rigelgroupllc.com/wp/blog/connecting-to-pow-via-ssl#comments</comments>
		<pubDate>Wed, 14 Mar 2012 03:24:23 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[OSX]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=195</guid>
		<description><![CDATA[If you are running your Rails apps under Pow (and really, why wouldn't you?), and you need to connect via SSL, you may have googled around and found instructions on how to set up Nginx as an SSL proxy. While this is probably your best bet for heavy-duty usage, if you want something simpler and [...]]]></description>
			<content:encoded><![CDATA[<p>If you are running your Rails apps under <a href="http://pow.cx/" title="Pow">Pow</a> (and really, why wouldn't you?), and you need to connect via SSL, you may have googled around and found instructions on how to set up Nginx as an SSL proxy. While this is probably your best bet for heavy-duty usage, if you want something simpler and lighter-weight for occasional testing, check out stunnel. It will proxy SSL connections just like Nginx. </p>
<p>The magic incantations (for OSX at least) go something like this:</p>
<p><code>brew install stunnel</code></p>
<p>Now, the default .pem file that stunnel created for me didn't work, so let's create our own:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl req <span style="color: #660033;">-new</span> <span style="color: #660033;">-x509</span> <span style="color: #660033;">-days</span> <span style="color: #000000;">9999</span> <span style="color: #660033;">-nodes</span> <span style="color: #660033;">-out</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>stunnel<span style="color: #000000; font-weight: bold;">/</span>stunnel.pem <span style="color: #660033;">-keyout</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>stunnel<span style="color: #000000; font-weight: bold;">/</span>stunnel.pem</pre></div></div>

<p>Verify the .pem file with this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl x509 <span style="color: #660033;">-subject</span> <span style="color: #660033;">-dates</span> <span style="color: #660033;">-fingerprint</span> <span style="color: #660033;">-in</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>stunnel<span style="color: #000000; font-weight: bold;">/</span>stunnel.pem</pre></div></div>

<p>If everything checks out, lets now create a config file for stunnel that will proxy https connections from port 443 on localhost to http connections to port 80 on localhost.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>stunnel<span style="color: #000000; font-weight: bold;">/</span>stunnel.cnf <span style="color: #cc0000; font-style: italic;">&lt;&lt;ENDOFFILE
pid        = /tmp/stunnel.pid
setuid     = nobody
setgid     = nobody
foreground = yes
client     = no
[https]
cert = /usr/local/etc/stunnel/stunnel.pem
accept = 443
connect =  80
ENDOFFILE</span></pre></div></div>

<p>Now all that is left to do is fire it up:</p>
<p><code>sudo stunnel</code></p>
<p>Go <a href="https://localhost">here</a> and you should see your pow server.</p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/connecting-to-pow-via-ssl/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simulate Poor Network Conditions on OSX</title>
		<link>http://rigelgroupllc.com/wp/blog/simulate-poor-network-conditions-on-osx</link>
		<comments>http://rigelgroupllc.com/wp/blog/simulate-poor-network-conditions-on-osx#comments</comments>
		<pubDate>Tue, 13 Mar 2012 19:35:11 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[OSX]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=189</guid>
		<description><![CDATA[If you have OSX (Lion) and XCode installed, spotlight for Network Link Conditioner.prefPane, and install it. You will get this nifty preferences pane which lets you simulate various network speeds and latencies, which comes in really handy for testing your web app in less-then-optimal conditions.]]></description>
			<content:encoded><![CDATA[<p>If you have OSX (Lion) and XCode installed, spotlight for <code>Network Link Conditioner.prefPane</code>, and install it. You will get this nifty preferences pane which lets you simulate various network speeds and latencies, which comes in really handy for testing your web app in less-then-optimal conditions.</p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/simulate-poor-network-conditions-on-osx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Batman&#8217;s Secret Cache</title>
		<link>http://rigelgroupllc.com/wp/blog/batmans-secret-cache</link>
		<comments>http://rigelgroupllc.com/wp/blog/batmans-secret-cache#comments</comments>
		<pubDate>Thu, 02 Feb 2012 05:10:39 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[batmanjs]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=146</guid>
		<description><![CDATA[The real Batman has caches of weapons stashed across Gotham City, but the Batman.js javascript framework could use a cache of it's own, a Views cache. I love the fact that Batman views are all stored in separate files (a la Rails) when doing development, it makes organizing a large project very easy. But when [...]]]></description>
			<content:encoded><![CDATA[<p>The real Batman has caches of weapons stashed across Gotham City, but the <a href="http://batmanjs.org">Batman.js</a> javascript framework could use a cache of it's own, a Views cache. I love the fact that Batman views are all stored in separate files (a la Rails) when doing development, it makes organizing a large project very easy. But when it comes time to deploy to a production system, stock Batmanjs will happily request each view as it is first requested via AJAX, but all those individual views should really be bundled up and served in one file, for obvious performance reasons. </p>
<p>To accomplish this, <a href="http://ryanfunduk.com/">Ryan Funduk</a> and I  recently came up with this approach. First we will leverage the Rails asset pipeline to create a json file that contains all of our app's views. </p>
<p><code>/app/assets/javascripts/all_views.json.erb</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>=
  prefix = <span style="color:#996600;">&quot;#{Rails.root}/app/assets/javascripts/views&quot;</span>
  paths = <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">glob</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{prefix}/**/*&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">file</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>f =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#006600; font-weight:bold;">&#40;</span>html<span style="color:#006600; font-weight:bold;">|</span>erb<span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  paths.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>all_views, f<span style="color:#006600; font-weight:bold;">|</span>
    viewname = f.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#008000; font-style:italic;">#{prefix}/, '' ).sub( /\..*$/i, '' )</span>
    view = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span>
    view = <span style="color:#CC00FF; font-weight:bold;">ERB</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>view<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">result</span> <span style="color:#9966CC; font-weight:bold;">if</span> f =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">erb</span>$<span style="color:#006600; font-weight:bold;">/</span>i
    all_views<span style="color:#006600; font-weight:bold;">&#91;</span>viewname<span style="color:#006600; font-weight:bold;">&#93;</span> = view
    all_views
  <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">to_json</span>
<span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Now, if you go to http://localhost:3000/assets/all_views.json you should see all your views in one large json object. Note that your Batman views will be processed through ERB, so if you need the server to do some processing, go right ahead.  (Just remember that the ERB code will be run only once, when the views are compiled, not each time they are requested.)</p>
<p>Next, tell the Rails asset pipeline to precompile this file, like so:</p>
<p><code>/config/environments/production.rb</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">assets</span>.<span style="color:#9900CC;">precompile</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>all_views.<span style="color:#9900CC;">json</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Next, we write a Batman helper that will request the all_views.json file, and create Batman views for each of the individual views. That looks like this:</p>
<p><code>/app/assets/javascripts/helpers/views_preloader.js.coffee.erb</code></p>

<div class="wp_syntax"><div class="code"><pre class="coffeescript" style="font-family:monospace;">MyApp.preloadViews = () -&gt;
  new Batman.Request
    url: '&lt;%= asset_path(&quot;all_views.json&quot;) %&gt;'
    type: 'json'
    error: (response)  -&gt; throw new Error(&quot;Could not load views&quot;)
    success: (all_views) =&gt;
      for view of all_views
        Batman.View.store.set(view, all_views[view])</pre></div></div>

<p>Note that we run this file through ERB first, so that we get the proper path (including the digest, if applicable) to the all_views file.  The last line pre-populates the view store with each of the views. (You will need to be on the master Batmanjs branch, as the View store is a new addition since the 0.8 release).</p>
<p>The last thing we need to do is kick off the preloader, and a good place to do that is like so:</p>
<p>/app/assets/javascripts/application.js.coffee</p>

<div class="wp_syntax"><div class="code"><pre class="coffeescript" style="font-family:monospace;">window.MyApp = class MyApp extends Batman.App
&nbsp;
  @on 'run', -&gt;
    MyApp.preloadViews()</pre></div></div>

<p>So, putting it all together, when your Rails app is deployed and the assets are precompiled, the asset pipeline will write out an all_views-1234.json file to the public/assets directory.  On the client side of things, when your Batman app starts, it calls the preloader, which loads the all_views-1234.json file and creates Batman views for them all in one fell swoop.  The really cool thing about this setup is that the Rails asset pipeline takes care of caching the views for you. Winning!</p>
<p>- John Lynch<br />
<a href="https://twitter.com/#!/johnrlynch">@johnrlynch</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/batmans-secret-cache/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Torquebox really does go to 11!</title>
		<link>http://rigelgroupllc.com/wp/blog/torquebox-really-does-go-to-11</link>
		<comments>http://rigelgroupllc.com/wp/blog/torquebox-really-does-go-to-11#comments</comments>
		<pubDate>Thu, 22 Sep 2011 19:41:21 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=135</guid>
		<description><![CDATA[After working with Torquebox for the last year, I couldn't imagine writing a Rails app without it. I came for the deployment story, but I stayed because of all the fantastic tools you get for free. From diagnostic tools on the JVM, to message queueing, background tasks, the awesome Infinispan cache, etc. Do yourself a [...]]]></description>
			<content:encoded><![CDATA[<p>After working with <a href="http://torquebox.org/">Torquebox</a> for the last year, I couldn't imagine writing a Rails app without it. I came for the deployment story, but I stayed because of all the fantastic tools you get for free. From diagnostic tools on the JVM, to message queueing, background tasks, the awesome Infinispan cache, etc.  Do yourself a favor and check it out.</p>
<p>But, when doing development, I like to make sure the app will run outside of Torquebox as well, with a simple 'rails s', so here are a few tricks to make that happen.</p>
<p>When setting Torquebox-specific features, like for example setting the Rails cache store to Infinispan, do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">cache_store</span> = <span style="color:#ff3333; font-weight:bold;">:torque_box_store</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>TorqueBox<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>That way, when you run outside of Torquebox, Rails will use the default file cache and all will be well.</p>
<p>Another great Torquebox feature is the ability to mark any method as backgroundable, and the JVM will transparently spin off that method in a new thread. You accomplish that with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Battlestar
  <span style="color:#9966CC; font-weight:bold;">include</span> Backgroundable
  always_background <span style="color:#ff3333; font-weight:bold;">:fire</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> fire
    ...
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This of course will cause problems when running without Torquebox, so if you add this shim into your initializers, the method will just run in the foreground like normal.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>Torquebox<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">module</span> Backgroundable
    extend <span style="color:#6666ff; font-weight:bold;">ActiveSupport::Concern</span>
    <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
      <span style="color:#9966CC; font-weight:bold;">def</span> always_background<span style="color:#006600; font-weight:bold;">&#40;</span>method<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Another technique I use, especially for accessing the Torquebox message queues, is make sure you use your own class as a central point of control, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> QueueManager
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">TorqueBox::Injectors</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>TorqueBox<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> publish_weapon_event<span style="color:#006600; font-weight:bold;">&#40;</span>event<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@weapon_queue</span> <span style="color:#006600; font-weight:bold;">||</span>= inject<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/queues/myapp/weapon_events&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>TorqueBox<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@weapon_queue</span>
      <span style="color:#0066ff; font-weight:bold;">@weapon_queue</span>.<span style="color:#9900CC;">publish</span><span style="color:#006600; font-weight:bold;">&#40;</span>event<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      processor = WeaponEventProcessor.<span style="color:#9900CC;">new</span>
      processor.<span style="color:#9900CC;">on_message</span><span style="color:#006600; font-weight:bold;">&#40;</span>event<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So, when running inside Torquebox, the message gets sent to the queue, which would then at some point in the future execute the event processor,  but when running under Webrick, it calls the event processor directly. </p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/torquebox-really-does-go-to-11/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Render Rails3 Views Outside of your Controllers</title>
		<link>http://rigelgroupllc.com/wp/blog/render-rails3-views-outside-of-your-controllers</link>
		<comments>http://rigelgroupllc.com/wp/blog/render-rails3-views-outside-of-your-controllers#comments</comments>
		<pubDate>Thu, 22 Sep 2011 19:13:46 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=127</guid>
		<description><![CDATA[If you ever had the need to break out of the MVC box and render a view template outside of your controller, the old-style Rails2 version went something like this: &#160; body = ActionView::Base.new&#40;Rails::Configuration.new.view_path&#41;.render&#40;:file =&#62; &#34;/orders/receipt.html.erb&#34;,:layout =&#62; false,:locals=&#62;&#123;:order=&#62;order&#125;&#41; In Rails3, the same thing can be accomplished with this incantation: av = ActionView::Base.new&#40;&#41; av.view_paths = ActionController::Base.view_paths [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever had the need to break out of the MVC box and render a view template outside of your controller, the old-style Rails2 version went something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
body = <span style="color:#6666ff; font-weight:bold;">ActionView::Base</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rails::Configuration</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">view_path</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">render</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/orders/receipt.html.erb&quot;</span>,:layout <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>,:locals<span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:order<span style="color:#006600; font-weight:bold;">=&gt;</span>order<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In Rails3, the same thing can be accomplished with this incantation:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">av = <span style="color:#6666ff; font-weight:bold;">ActionView::Base</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
av.<span style="color:#9900CC;">view_paths</span> = <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">view_paths</span>
av.<span style="color:#9900CC;">extend</span> ApplicationHelper <span style="color:#008000; font-style:italic;">#or any other helpers your template may need</span>
body = av.<span style="color:#9900CC;">render</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:template</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;orders/receipt.html.erb&quot;</span>,:locals <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:order <span style="color:#006600; font-weight:bold;">=&gt;</span> order<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/render-rails3-views-outside-of-your-controllers/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transparently Cache Network Calls in Titanium</title>
		<link>http://rigelgroupllc.com/wp/blog/transparently-cache-network-calls-in-titanium</link>
		<comments>http://rigelgroupllc.com/wp/blog/transparently-cache-network-calls-in-titanium#comments</comments>
		<pubDate>Tue, 07 Sep 2010 23:17:51 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=113</guid>
		<description><![CDATA[Typically, when you are building a mobile app with Titanium, you are grabbing data from the web via RSS/Atom/etc or various APIs, and displaying them to the user. And of course, when developing for mobile clients, you can never take the network for granted. So a common pattern we use is to always cache the [...]]]></description>
			<content:encoded><![CDATA[<p>Typically, when you are building a mobile app with <a href="http://www.appcelerator.com/">Titanium</a>, you are grabbing data from the web via RSS/Atom/etc or various APIs, and displaying them to the user.  And of course, when developing for mobile clients, you can never take the network for granted. So a common pattern we use is to always cache the results of network requests locally, so in the event of a network glitch or even "airplane mode", we still have some (stale) data to show to the user.  We have packaged this functionality up into a <a href="http://jashkenas.github.com/coffee-script/">CoffeeScript </a>snippet <a href="http://gist.github.com/560183/">here</a>. </p>
<p>Basically, it is a wrapper around the standard <a href="http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Network.HTTPClient-object.html">Titanium.Network.HTTPClient</a> API, except that we cache the results of each request in a local SQLite database table, and use that for subsequent requests until the cached record expires. The cache key is a hash of the full URL (plus any data parameters for POSTs).</p>
<p>For an example (written in CoffeeScript), we can create a new instance of the class to connect to the CNN RSS news feeds:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  Ti.<span style="color:#9966CC; font-weight:bold;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;HTTPClientWithCache.js&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  cnn = new root.<span style="color:#9900CC;">HTTPClientWithCache</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>
    baseURL: <span style="color:#996600;">&quot;http://rss.cnn.com/rss&quot;</span>,
    retryCount: <span style="color:#006666;">2</span>,
    cacheSeconds: <span style="color:#006666;">60</span>,
    onload: <span style="color:#006600; font-weight:bold;">&#40;</span>response<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
      Ti.<span style="color:#9900CC;">API</span>.<span style="color:#9900CC;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Response Data: #{response.responseText}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      Ti.<span style="color:#9900CC;">API</span>.<span style="color:#9900CC;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Is this cached data?: #{response.cached}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      Ti.<span style="color:#9900CC;">API</span>.<span style="color:#9900CC;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Cached at: #{response.cached_at}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Each time we want to make the request, we say:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  cnn.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>url: <span style="color:#996600;">&quot;/cnn_topstories.rss&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The response will either come from the cache, if it has not expired, or from cnn.com. Because we set the retryCount property to 2, if the first attempt to fetch the data over the network fails (meaning any HTTP status code > 400) it will automatically try again a second time. If that fails, then it will respond with the most recent version available in the cache, or null if nothing exists in the cache. The response object also has a cached property of true for anything served out of the cache, and a cached_at property of the timestamp the record was cached.</p>
<p>You can use the same object to make calls to other URLs as well, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  cnn.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>url: <span style="color:#996600;">&quot;/cnn_us.rss&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>If you need to pass parameters (like for pagination) you can say:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  cnn.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>url: <span style="color:#996600;">&quot;/cnn_us.rss?page=1&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>To POST data to a URL, do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  cnn.<span style="color:#9900CC;">post</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>url: <span style="color:#996600;">&quot;/story/19912/edit&quot;</span>, data: <span style="color:#006600; font-weight:bold;">&#123;</span>param1: <span style="color:#996600;">&quot;value1&quot;</span>, param2: <span style="color:#996600;">&quot;value2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>To manually prune the cache, you can call the prune_cache method, and anything older than seconds will be<br />
deleted from the cache. For example, to remove anything older than 1 day (86,400 seconds) you would say this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  cnn.<span style="color:#9900CC;">prune_cache</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">86400</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>To completely clear the cache of every single entry, you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  cnn.<span style="color:#9900CC;">prune_cache</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The code is on GitHub as a gist <a href="http://gist.github.com/560183/">here</a>. For pointers on how to build Titanium apps using the incredible CoffeeScript instead of Javascript, see our previous blog post <a href="http://rigelgroupllc.com/wp/blog/building-iphone-apps-with-titanium-and-coffeescript">here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/transparently-cache-network-calls-in-titanium/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building iPhone Apps using Titanium and CoffeeScript</title>
		<link>http://rigelgroupllc.com/wp/blog/building-iphone-apps-with-titanium-and-coffeescript</link>
		<comments>http://rigelgroupllc.com/wp/blog/building-iphone-apps-with-titanium-and-coffeescript#comments</comments>
		<pubDate>Tue, 31 Aug 2010 21:26:26 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=93</guid>
		<description><![CDATA[We have been using Titanium to build mobile apps for some time now. It is truly amazing how much more productive we are as opposed to when we use POOC (Plain Old Obj-C). Granted, Titanium is not a great fit for some applications, but for your standard network and data-heavy line of business applications, it [...]]]></description>
			<content:encoded><![CDATA[<p><script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script></p>
<p>We have been using <a href="http://www.appcelerator.com">Titanium</a> to build mobile apps for some time now. It is truly amazing how much more productive we are as opposed to when we use POOC (Plain Old Obj-C).  Granted, Titanium is not a great fit for some applications, but for your standard network and data-heavy line of business applications, it rocks. </p>
<p>But, Titanium does not give you a lot of guidance on <i>how</i> you should write your app. They provide all the pieces you need, but folks are still figuring out the best way to structure things. So, we thought we would show you our particular technique for putting together a Titanium mobile app.</p>
<p>The first thing we did, was ditch Javascript.  To be honest, something about it just makes our eyes bleed. Fortunately, <a href="http://github.com/jashkenas">jashkenas</a> has come to our rescue with a Ruby-like language that compiles down to Javascript, called <a href="http://github.com/jashkenas/coffee-script">CoffeeScript</a>.  CoffeeScript makes Javascript a joy to use, and because it "compiles" to plain ole' Javascript, you can use it anywhere, including Titanium. (Basically, you run the command "coffee -w -c *.coffee" in whatever directory your files are in, and it continually watches for any changes and immediately compiles the *.coffee into a *.js file.) So to be clear, you are only ever referencing the .js files in your Titanium project, Titanium doesn't know anything about CoffeeScript or how to parse/use it.</p>
<p>One interesting thing CoffeeScript does, is compile each CoffeeScript file into its own namespace inside an anonymous Javascript function. This is great, because it isolates each file and prevents you from polluting the global namespace. For example, this CoffeeScript code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">show_flag = <span style="color:#0000FF; font-weight:bold;">true</span>
alert <span style="color:#996600;">&quot;Hello World!&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> show_flag</pre></div></div>

<p>gets translated into this Javascript code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> show_flag<span style="color: #339933;">;</span>
  show_flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>show_flag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Another thing CoffeeScript enables, is an easier-on-the-eyes way to creates "classes", for example (from the CoffeeScript docs):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Animal
  constructor: <span style="color:#006600; font-weight:bold;">&#40;</span>@name<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
&nbsp;
  move: <span style="color:#006600; font-weight:bold;">&#40;</span>meters<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
    alert <span style="color:#0066ff; font-weight:bold;">@name</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot; moved &quot;</span> <span style="color:#006600; font-weight:bold;">+</span> meters <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;m.&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Snake extends Animal
  move: <span style="color:#006600; font-weight:bold;">-&gt;</span>
    alert <span style="color:#996600;">&quot;Slithering...&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span> <span style="color:#006666;">5</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Horse extends Animal
  move: <span style="color:#006600; font-weight:bold;">-&gt;</span>
    alert <span style="color:#996600;">&quot;Galloping...&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span> <span style="color:#006666;">45</span>
&nbsp;
sam = new Snake <span style="color:#996600;">&quot;Sammy the Python&quot;</span>
tom = new Horse <span style="color:#996600;">&quot;Tommy the Palomino&quot;</span>
&nbsp;
sam.<span style="color:#9900CC;">move</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
tom.<span style="color:#9900CC;">move</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>(If you really want to see what that gets translated to, put on some shades and click <a href="javascript:jQuery('#bloody-eyeballs').toggle()">here</a>.)</p>
<div id="bloody-eyeballs" style="display:none">

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Animal<span style="color: #339933;">,</span> Horse<span style="color: #339933;">,</span> Snake<span style="color: #339933;">,</span> sam<span style="color: #339933;">,</span> tom<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> __extends <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>child<span style="color: #339933;">,</span> parent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> ctor <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    ctor.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> parent.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span>
    child.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ctor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    child.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">=</span> child<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> parent.<span style="color: #660066;">extended</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span> parent.<span style="color: #660066;">extended</span><span style="color: #009900;">&#40;</span>child<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    child.__super__ <span style="color: #339933;">=</span> parent.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Animal <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>_a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> _a<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Animal.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">move</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>meters<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; moved &quot;</span> <span style="color: #339933;">+</span> meters <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;m.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Snake <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Animal.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
__extends<span style="color: #009900;">&#40;</span>Snake<span style="color: #339933;">,</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Snake.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">move</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Slithering...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Snake.__super__.<span style="color: #660066;">move</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Horse <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Animal.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
__extends<span style="color: #009900;">&#40;</span>Horse<span style="color: #339933;">,</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Horse.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">move</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Galloping...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Horse.__super__.<span style="color: #660066;">move</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">45</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
sam <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Snake<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Sammy the Python&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
tom <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Horse<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tommy the Palomino&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sam.<span style="color: #660066;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
tom.<span style="color: #660066;">move</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<p>So, when using CoffeeScript in Titanium apps, we can use these two features to help us structure things in a way that keeps each window isolated in its own namespace, yet allows us easy access to global objects as well. To start, we set up our app.js file, which Titanium runs on app startup. We keep this file as a plain Javascript file, and define a variable called 'root', to which we will attach anything that we want access to globally. We also include each window's CoffeeScript file (well, technically we include the Javascript file that the CoffeeScript file was translated into). Finally, we include a 'main' file, which sets up the main tab group and kicks everything off.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//app.js</span>
<span style="color: #003366; font-weight: bold;">var</span> root <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Ti.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'js/generic_window.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Ti.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'js/main.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the generic_window.coffee file, we create a class definition for a generic window, and actually create a Titanium window in the constructor. We then attach the class definition to the root object so we can get at it from anywhere.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#generic_window.coffee</span>
<span style="color:#9966CC; font-weight:bold;">class</span> GenericWindow
  constructor: <span style="color:#006600; font-weight:bold;">&#40;</span>theTitle, theText<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
    <span style="color:#0066ff; font-weight:bold;">@win</span> = Ti.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">createWindow</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>title:theTitle,backgroundColor:<span style="color:#996600;">'#fff'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    label = Titanium.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">createLabel</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>
      color: <span style="color:#996600;">'#999'</span>,
      text: theText,
      font: <span style="color:#006600; font-weight:bold;">&#123;</span>
        fontSize: <span style="color:#006666;">20</span>,
        fontFamily: <span style="color:#996600;">'Helvetica Neue'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>,
      textAlign: <span style="color:#996600;">'center'</span>,
      width: <span style="color:#996600;">'auto'</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@win</span>.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>label<span style="color:#006600; font-weight:bold;">&#41;</span>;
&nbsp;
root.<span style="color:#9900CC;">GenericWindow</span> = GenericWindow</pre></div></div>

<p>Now the main file sets up the tabgroup, and actually instantiates some windows from the above generic window class...</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#main.coffee</span>
tabGroup = Titanium.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">createTabGroup</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>	barColor:<span style="color:#996600;">'#336699'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
Titanium.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">setBackgroundColor</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'#000'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Attach the window instance to root so we can get to it from anywhere </span>
root.<span style="color:#9900CC;">Win1</span> = new root.<span style="color:#9900CC;">GenericWindow</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Win1'</span>,<span style="color:#996600;">'I am Window 1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
tab1 = Titanium.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">createTab</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>
  icon:<span style="color:#996600;">'KS_nav_views.png'</span>,
  title:<span style="color:#996600;">'Win1'</span>,
  window: root.<span style="color:#9900CC;">Win1</span>.<span style="color:#9900CC;">win</span>
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
tabGroup.<span style="color:#9900CC;">addTab</span><span style="color:#006600; font-weight:bold;">&#40;</span>tab1<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Attach the window instance to root so we can get to it from anywhere </span>
root.<span style="color:#9900CC;">Win2</span> = new root.<span style="color:#9900CC;">GenericWindow</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Win2'</span>,<span style="color:#996600;">'I am Window 2'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
tab2 = Titanium.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">createTab</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>
  icon:<span style="color:#996600;">'KS_nav_views.png'</span>,
  title:<span style="color:#996600;">'Win2'</span>,
  window: root.<span style="color:#9900CC;">Win2</span>.<span style="color:#9900CC;">win</span>
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
tabGroup.<span style="color:#9900CC;">addTab</span><span style="color:#006600; font-weight:bold;">&#40;</span>tab2<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
tabGroup.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>transition:Titanium.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">iPhone</span>.<span style="color:#9900CC;">AnimationStyle</span>.<span style="color:#9900CC;">FLIP_FROM_LEFT</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Now, let's add a custom property to our generic window class, so add this line to the constructor of generic_window.coffee:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    <span style="color:#0066ff; font-weight:bold;">@custom1</span> = <span style="color:#996600;">&quot;Default Value&quot;</span></pre></div></div>

<p>This creates a property and gives it a default value. To access it, you could say "root.Win1.custom1". Now let's add an event handler that displays an alert box containing the value of @custom1:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#generic_window.coffee</span>
<span style="color:#9966CC; font-weight:bold;">class</span> GenericWindow
  constructor: <span style="color:#006600; font-weight:bold;">&#40;</span>theTitle, theText<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
    <span style="color:#008000; font-style:italic;"># Save off the context of this object to a local var 'self'</span>
    <span style="color:#0000FF; font-weight:bold;">self</span> = this
    <span style="color:#0066ff; font-weight:bold;">@custom1</span> = <span style="color:#996600;">&quot;Default Value&quot;</span>
    <span style="color:#0066ff; font-weight:bold;">@win</span> = Ti.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">createWindow</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>title:theTitle,backgroundColor:<span style="color:#996600;">'#fff'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    label = Titanium.<span style="color:#9900CC;">UI</span>.<span style="color:#9900CC;">createLabel</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>
      color: <span style="color:#996600;">'#999'</span>,
      text: theText,
      font: <span style="color:#006600; font-weight:bold;">&#123;</span>
        fontSize: <span style="color:#006666;">20</span>,
        fontFamily: <span style="color:#996600;">'Helvetica Neue'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>,
      textAlign: <span style="color:#996600;">'center'</span>,
      width: <span style="color:#996600;">'auto'</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@win</span>.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>label<span style="color:#006600; font-weight:bold;">&#41;</span>;
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@win</span>.<span style="color:#9900CC;">addEventListener</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'click'</span>, <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
      alert<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">custom1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
root.<span style="color:#9900CC;">GenericWindow</span> = GenericWindow</pre></div></div>

<p>If you would like to see how it all hangs together, clone the GitHub repo <a href="http://github.com/johnthethird/titanium-coffeescript-sample">here</a> and try it out. The bottom line is that before CoffeeScript, our Titanium apps tended to resemble spaghetti, but now we are able to encapsulate functionality and just generally make things look much cleaner. Sure, you don't <i>need</i> CoffeeScript to do this, but it sure makes it a lot more fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/building-iphone-apps-with-titanium-and-coffeescript/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Tee with Sinatra</title>
		<link>http://rigelgroupllc.com/wp/blog/tee-with-sinatra</link>
		<comments>http://rigelgroupllc.com/wp/blog/tee-with-sinatra#comments</comments>
		<pubDate>Mon, 29 Mar 2010 04:50:53 +0000</pubDate>
		<dc:creator>JLynch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[elasticsearch]]></category>
		<category><![CDATA[riak]]></category>

		<guid isPermaLink="false">http://rigelgroupllc.com/wp/?p=82</guid>
		<description><![CDATA[OK, so I wanted to take all of the JSON data that we were stuffing into our Riak cluster, and send a copy of it to our ElasticSearch cluster as well, so that we could, you know, actually find the data later. We could have done this by modifying one of the Riak client libraries, [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so I wanted to take all of the JSON data that we were stuffing into our <a href="http://riak.basho.com">Riak</a> cluster, and send a copy of it to our <a href="http://www.elasticsearch.com/">ElasticSearch</a> cluster as well, so that we could, you know, actually <em>find</em> the data later.  We could have done this by modifying one of the Riak client libraries, but then any data that got uploaded through a different client would be missed. So as an experiment, we turned to our new favorite tool, Sinatra, and hacked up a Rack proxy app, that will intercept the incoming HTTP requests, send them on to Riak and also send a copy to the ElasticSearch cluster. We used <a href="http://github.com/pauldix/typhoeus">Typhoeus</a> as the HTTP client to do this, so that we could concurrently execute the 2 requests in the interests of speed.</p>
<p>Here is the proof-of-concept:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'typhoeus'</span>
&nbsp;
OPTIONS = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:riak_host</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;localhost&quot;</span>
OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:riak_port</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;8098&quot;</span>
OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:es_host</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;localhost&quot;</span>
OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:es_port</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;9200&quot;</span>
OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:riak_timeout</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">5000</span> <span style="color:#008000; font-style:italic;"># milliseconds</span>
OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:es_timeout</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">5000</span> <span style="color:#008000; font-style:italic;"># milliseconds</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Rack::Proxy</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span> = app
    <span style="color:#0066ff; font-weight:bold;">@hydra</span> = <span style="color:#6666ff; font-weight:bold;">Typhoeus::Hydra</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    req = <span style="color:#6666ff; font-weight:bold;">Rack::Request</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># We need to use it twice, so read in the stream. This is an obvious problem with large bodies, so beware.</span>
    req_body = req.<span style="color:#9900CC;">body</span>.<span style="color:#9900CC;">read</span> <span style="color:#9966CC; font-weight:bold;">if</span> req.<span style="color:#9900CC;">body</span> 
&nbsp;
    riak_url = <span style="color:#996600;">&quot;http://#{OPTIONS[:riak_host]}:#{OPTIONS[:riak_port]}#{req.fullpath}&quot;</span>
&nbsp;
    opts = <span style="color:#006600; font-weight:bold;">&#123;</span>:timeout <span style="color:#006600; font-weight:bold;">=&gt;</span> OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:riak_timeout</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    opts.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> req.<span style="color:#9900CC;">request_method</span>.<span style="color:#9900CC;">downcase</span>.<span style="color:#9900CC;">to_sym</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    opts.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:headers</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;Content-type&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> req.<span style="color:#9900CC;">content_type</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> req.<span style="color:#9900CC;">content_type</span>
    opts.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> req_body<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> req_body <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> req_body.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
&nbsp;
    riak_req = <span style="color:#6666ff; font-weight:bold;">Typhoeus::Request</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>riak_url, opts<span style="color:#006600; font-weight:bold;">&#41;</span>
    riak_response = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    riak_req.<span style="color:#9900CC;">on_complete</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>response<span style="color:#006600; font-weight:bold;">|</span>
      riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:code</span><span style="color:#006600; font-weight:bold;">&#93;</span> = response.<span style="color:#9900CC;">code</span>
      riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:headers</span><span style="color:#006600; font-weight:bold;">&#93;</span> = response.<span style="color:#9900CC;">headers_hash</span>
      riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:body</span><span style="color:#006600; font-weight:bold;">&#93;</span> = response.<span style="color:#9900CC;">body</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0066ff; font-weight:bold;">@hydra</span>.<span style="color:#9900CC;">queue</span> riak_req
&nbsp;
    <span style="color:#008000; font-style:italic;"># If we are putting or posting JSON, send a copy to the ElasticSearch index named &quot;riak&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>req.<span style="color:#9900CC;">put</span>? <span style="color:#006600; font-weight:bold;">||</span> req.<span style="color:#9900CC;">post</span>?<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> req.<span style="color:#9900CC;">content_type</span> == <span style="color:#996600;">&quot;application/json&quot;</span> 
      req.<span style="color:#9900CC;">path</span> =~ <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>^<span style="color:#006600; font-weight:bold;">/</span>riak<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
      bucket, key = $<span style="color:#006666;">1</span>, $<span style="color:#006666;">2</span>
      es_url = <span style="color:#996600;">&quot;http://#{OPTIONS[:es_host]}:#{OPTIONS[:es_port]}/riak/#{bucket}/#{key}&quot;</span>
      opts = <span style="color:#006600; font-weight:bold;">&#123;</span>:timeout <span style="color:#006600; font-weight:bold;">=&gt;</span> OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:es_timeout</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
      opts.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> req.<span style="color:#9900CC;">request_method</span>.<span style="color:#9900CC;">downcase</span>.<span style="color:#9900CC;">to_sym</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      opts.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> req_body<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> req_body  <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> req_body.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
      es_req = <span style="color:#6666ff; font-weight:bold;">Typhoeus::Request</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>es_url, opts<span style="color:#006600; font-weight:bold;">&#41;</span>
      es_response = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
      es_req.<span style="color:#9900CC;">on_complete</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>response<span style="color:#006600; font-weight:bold;">|</span>
        es_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:code</span><span style="color:#006600; font-weight:bold;">&#93;</span> = response.<span style="color:#9900CC;">code</span>
        es_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:headers</span><span style="color:#006600; font-weight:bold;">&#93;</span> = response.<span style="color:#9900CC;">headers_hash</span>
        es_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:body</span><span style="color:#006600; font-weight:bold;">&#93;</span> = response.<span style="color:#9900CC;">body</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#0066ff; font-weight:bold;">@hydra</span>.<span style="color:#9900CC;">queue</span> es_req
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Concurrently executes both HTTP requests, blocks until they both finish</span>
    <span style="color:#0066ff; font-weight:bold;">@hydra</span>.<span style="color:#9900CC;">run</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#If we wrote to ES add a custom header</span>
    riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:headers</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;X-ElasticSearch-ResCode&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> es_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:code</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> es_response <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> es_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:code</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#Typhoeus can add nil headers, lets get rid of them</span>
    riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:headers</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>k,v<span style="color:#006600; font-weight:bold;">|</span> v == <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#125;</span> 
&nbsp;
    <span style="color:#008000; font-style:italic;"># Return original Riak response to client</span>
    <span style="color:#006600; font-weight:bold;">&#91;</span>riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:code</span><span style="color:#006600; font-weight:bold;">&#93;</span>, riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:headers</span><span style="color:#006600; font-weight:bold;">&#93;</span>, riak_response<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:body</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
use <span style="color:#6666ff; font-weight:bold;">Rack::Proxy</span></pre></div></div>

<p>(Gist <a href="http://gist.github.com/347418">here</a>)</p>
<p>Execute the script, and it will listen on port 4567, so point your Riak client of choice there and start PUTing data, which will be seamlessly replicated into the ElasticSearch cluster.  If we were really going to use this in anger, there is a lot of work yet to be done, but as a skeleton of how to use Sinatra (Rack, really) to quickly whip up custom proxys, and <code>tee</code> HTTP requests, I thought it might be useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://rigelgroupllc.com/wp/blog/tee-with-sinatra/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

