<?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>Technical Ramblings &#187; Ruby</title>
	<atom:link href="http://jonswope.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonswope.com</link>
	<description>More information than you cared to know about whatever I find cool</description>
	<lastBuildDate>Fri, 07 Jan 2011 04:58:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Rails 3 Engines/Plugins and Static Assets</title>
		<link>http://jonswope.com/2010/07/25/rails-3-engines-plugins-and-static-assets/</link>
		<comments>http://jonswope.com/2010/07/25/rails-3-engines-plugins-and-static-assets/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 21:58:12 +0000</pubDate>
		<dc:creator>Jon Swope</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[rails 3]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jonswope.com/?p=68</guid>
		<description><![CDATA[Engine-ish One of the big changes in Rails 3 is the move to &#8220;everything is an engine&#8221;.  You end up reading a lot about this in relation to plugins, mentioning how this architecture makes it so you can easily embed entire rails applications into others since they&#8217;re all just derivatives of Railties.  While this holds [...]]]></description>
			<content:encoded><![CDATA[<h3>Engine-ish</h3>
<p>One of the big changes in Rails 3 is the move to &#8220;everything is an engine&#8221;.  You end up reading a lot about this in relation to plugins, mentioning how this architecture makes it so you can easily embed entire rails applications into others since they&#8217;re all just derivatives of Railties.  While this holds true for very simple cases, you&#8217;ll quickly find simply creating a rails app, and sticking it in vendor/plugins doesn&#8217;t work the way you&#8217;d expect from reading about it.  You really do need to create your plugin in a certain way to actually make it work.  <a href="http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/" target="_blank">Over here</a> is a good writeup on creating a Rails Engine based plugin installed as a gem.</p>
<h3>Serving Static Assets</h3>
<p>Now, serving static assets from your plugin is a bit on the unintuitive side, and likely for a good reason.  <span style="color: #ff0000;">You generally do not want to do this for any kind of production system.</span> However, like most rules, there are times when breaking them makes pragmatic sense.  In this use case we have an internal tool that we want to be able to easily extend.  The maximum number of concurrent users will likely be in low teens, and serving static assets through the rails app is a non-issue.  Extending the app includes providing views, controllers, models, routes, and images.  The first three are extremely straightforward and intuitive, routes use a slightly different enclosing syntax in an Engine, but are otherwise identical.  Images, however are a bit tricky.</p>
<p>There are two ways to create an Engine based plugin, and they are a bit incompatible with each other.  You can either install your plugin as a gem, or as a plugin in the app.  In both cases, you are going to use the ActionDispatch::Static middleware to serve your content.  I am also assuming that you are placing your content in the &#8220;public&#8221; directory in the root of your plugin&#8217;s file structure.</p>
<h4>Gem Based Plugin</h4>
<p>A gem based plugin is initialized via the definition of itself in your plugin&#8217;s lib directory.  You can see an example of this below.  In order to set it up correctly you should put your middleware line in an initializer in your declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> MyEngine
  <span style="color:#9966CC; font-weight:bold;">class</span> Engine <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Rails::Engine</span>
    initializer <span style="color:#996600;">&quot;static assets&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>app<span style="color:#006600; font-weight:bold;">|</span>
      app.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">use</span> ::<span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, <span style="color:#996600;">&quot;#{root}/public&quot;</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>

<h4>In App Plugin</h4>
<p>Unfortunately, if the above file exists in a plugin installed to vendor/plugins your app will fail to load.  This is because rails automatically assumes all plugins are Engines, and initializes them as instances of the Plugin class which inherits from Engine.  It will autoload everything in lib, see the declaration, and then fail out with the incredible error message &#8220;[Your Plugin] is a Railtie/Engine and cannot be installed as plugin&#8221;.  Which is somewhat misleading since a plugin <em>is</em> an Engine, just not explicitly.</p>
<p>To get around this issue, you just have to move where you do your initialization and setup. Inside your plugin&#8217;s init.rb (which should be in the root of your plugin&#8217;s file structure) is where you can do this kind of stuff.  Rails boots your Plugin, and then loads this file with the variable &#8220;config&#8221; set for you to do your work.  So it&#8217;s simply:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">use</span> ::<span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, <span style="color:#996600;">&quot;#{root}/public&quot;</span></pre></div></div>

<h4>Load Order</h4>
<p>I strongly recommend using middleware.use as it will place your middleware after the parent app, and therefore will cause your plugins assets to be of lower priority than everything else in the stack.  This means that files in the parent app&#8217;s public directory will load instead of the plugin&#8217;s in the case of a conflicting name.  You still have options though.  To give your plugin&#8217;s static files priority over everything except the parent app&#8217;s static files, use:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">middleware.<span style="color:#9900CC;">insert_after</span> ::<span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, ::<span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, <span style="color:#996600;">&quot;#{root}/public&quot;</span></pre></div></div>

<p>To give your plugin top priority in static asset serving use:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">middleware.<span style="color:#9900CC;">insert_before</span> ::<span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, ::<span style="color:#6666ff; font-weight:bold;">ActionDispatch::Static</span>, <span style="color:#996600;">&quot;#{root}/public&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://jonswope.com/2010/07/25/rails-3-engines-plugins-and-static-assets/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Cucumber, Service Based Applications, Middleware, and You</title>
		<link>http://jonswope.com/2010/04/10/cucumber-service-based-applications-middleware-and-you/</link>
		<comments>http://jonswope.com/2010/04/10/cucumber-service-based-applications-middleware-and-you/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 06:28:12 +0000</pubDate>
		<dc:creator>Jon Swope</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[Sinatra]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://jonswope.com/?p=58</guid>
		<description><![CDATA[Over at Primedia, we&#8217;ve been trying to get a little bit better at our BDD, and have resolved to really use Cucumber wherever possible. This is an easy thing to say for most web apps, however ours have a number of critical parts that are external to the application itself. This means that full integration [...]]]></description>
			<content:encoded><![CDATA[<p>Over at <a href="http://primedia.com">Primedia</a>, we&#8217;ve been trying to get a little bit better at our BDD, and have resolved to really use Cucumber wherever possible.  This is an easy thing to say for most web apps, however ours have a number of critical parts that are external to the application itself.  This means that full integration testing of the applications that use these services as well as testing the services themselves can be rather tricky.  In order for us to get started there were two major problems that we had to tackle.</p>
<p>The first was how to test the service based apps outside of any consuming application.  The specific one in question did not present any HTML pages at all, and only returned Javascript that was executed.  A few ideas were floated around like dev/test only controllers or basic one-off apps that were specifically for testing the service.  Eventually we cam across a solution that worked and seemed much cleaner.  We combined the one-off app idea with rack middleware to create an application within our app that was only loaded in certain environments.  We used Sinatra to make a simple application that would respond with test pages on routes prefixed with &#8216;/test&#8217;, and then added it with config.middleware.use.  Combining this with Celerity and Capybara gave us a very clean testing solution for an application that only served Javascript.</p>
<p>There is one gotcha to this, and it only involves using Sinatra as middleware in Rails.  Rails attempts to determine if the middleware you gave it is a proc, and in doing so prevents you from being able to use any middleware that can respond to Rack requests as a singleton (like Sinatra).  Our quick fix was to monkey patch Sinatra to remove self.call, but a more permanent solution is fixing the way Rails 2.3 does its middleware.  I hope to submit some patches for that soon.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Sinatra
  <span style="color:#9966CC; font-weight:bold;">class</span> Base
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
      remove_method <span style="color:#ff3333; font-weight:bold;">:call</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></td></tr></table></div>

<p>Our second hurdle was determining how to test our consuming apps using Cucumber.  The main problem we had is that we use reverse proxying to make the services appear to be coming from the same domains as the rest of the apps.  This was easily taken care of in manual tests, where we could simulate our load balancers using Apache or nginx, however with automated tests, this becomes a problem.  The automated test suite doesn&#8217;t run behind a proxying layer or a web server, it just hits the app directly.  So, tackling that problem, I am releasing a gem I developed called <a href="http://github.com/jaswope/rack-reverse-proxy">rack-reverse-proxy</a>.  What this gem does is allow you to put reverse proxy rules directly into your middleware stack so they are independent of your web server.  This is probably not a good idea for production systems, load balancers and web servers are much better at this sort of thing, and due to the way rack works I have to wait until the entire request is received before forwarding it.  However it&#8217;s great for development and testing, allowing you to test your app directly without the need of an intermediate web server layer.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rack/reverse_proxy'</span>
&nbsp;
  use <span style="color:#6666ff; font-weight:bold;">Rack::ReverseProxy</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#008000; font-style:italic;"># Forward the path /test* to http://example.com/test*</span>
    reverse_proxy <span style="color:#996600;">'/test'</span>, <span style="color:#996600;">'http://example.com/'</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Forward the path /foo/* to http://example.com/bar/*</span>
    reverse_proxy <span style="color:#006600; font-weight:bold;">/</span>^\<span style="color:#006600; font-weight:bold;">/</span>foo<span style="color:#006600; font-weight:bold;">&#40;</span>\<span style="color:#006600; font-weight:bold;">/</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:#996600;">'http://example.com/bar$1'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonswope.com/2010/04/10/cucumber-service-based-applications-middleware-and-you/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Updating Multiple Git Repos</title>
		<link>http://jonswope.com/2010/01/29/updating-multiple-git-repos/</link>
		<comments>http://jonswope.com/2010/01/29/updating-multiple-git-repos/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 13:58:55 +0000</pubDate>
		<dc:creator>Jon Swope</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://jonswope.com/?p=53</guid>
		<description><![CDATA[Adam Lowe has a great writeup on setting up Vim for Ruby/Rails work.  It&#8217;s a pretty great solution, using pathogen.vim and the git repos for the bundles, but he didn&#8217;t mention how to update your bundles in an easy manner&#8230; $ find . -name ".git"  -type d -prune -execdir git pull \; This is a [...]]]></description>
			<content:encoded><![CDATA[<p>Adam Lowe has a <a href="http://www.adamlowe.me/2009/12/vim-destroys-all-other-rails-editors.html">great writeup</a> on setting up Vim for Ruby/Rails work.  It&#8217;s a pretty great solution, using pathogen.vim and the git repos for the bundles, but he didn&#8217;t mention how to update your bundles in an easy manner&#8230;</p>
<pre>$ find . -name ".git"  -type d -prune -execdir git pull \;</pre>
<p>This is a little command line snippet that will search for git repositories in all of the subdirectories of wherever you run it and update them.  Really handy in this case, because you can update all of your bundles at once, but it will work in any case where you have cloned a bunch of repos and want them to all stay up to date.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonswope.com/2010/01/29/updating-multiple-git-repos/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Passenger can&#8217;t find Rails</title>
		<link>http://jonswope.com/2010/01/24/passenger-cant-find-rails/</link>
		<comments>http://jonswope.com/2010/01/24/passenger-cant-find-rails/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 06:21:59 +0000</pubDate>
		<dc:creator>Jon Swope</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Phusion Passenger]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jonswope.com/?p=42</guid>
		<description><![CDATA[If you are running in to an issue where you are trying to get a Rails app up and running, but are receiving the error &#8220;Missing the Rails 2.x.x gem&#8230;&#8221; when you attempt to access it despite Rails being installed and available to your system, do not panic.  Rails is just giving you an extremely [...]]]></description>
			<content:encoded><![CDATA[<p>If you are running in to an issue where you are trying to get a Rails app up and running, but are receiving the error &#8220;Missing the Rails 2.x.x gem&#8230;&#8221; when you attempt to access it despite Rails being installed and available to your system, do not panic.  Rails is just giving you an extremely unhelpful error message.</p>
<p>Rails catches *any* problem loading the Rails gem, and then responds with that error.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> <span style="color:#9966CC; font-weight:bold;">rescue</span> Gem::<span style="color:#CC00FF; font-weight:bold;">LoadError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> load_error
      <span style="color:#ff6633; font-weight:bold;">$stderr</span>.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#40;</span>Missing the Rails <span style="color:#008000; font-style:italic;">#{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)</span>
      <span style="color:#CC0066; font-weight:bold;">exit</span> <span style="color:#006666;">1</span></pre></div></div>

<p>If you inspect load_error from the line above, you should see the actual error message.  In my case it was that ActiveSupport required Rack v1.0.0, but Passenger had already instantiated Rack v1.1.0.  This led to a failure when loading the Rails gem, which was of course incorrectly reported.  My quick solution was to uninstall the new version of rack, and force the install of the older version.</p>
<p>As a side note: I have the world&#8217;s worst code highlighter.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonswope.com/2010/01/24/passenger-cant-find-rails/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Getting Ruby Going on App Engine</title>
		<link>http://jonswope.com/2010/01/14/getting-ruby-going-on-app-engine/</link>
		<comments>http://jonswope.com/2010/01/14/getting-ruby-going-on-app-engine/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 15:34:43 +0000</pubDate>
		<dc:creator>Jon Swope</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[JRuby]]></category>

		<guid isPermaLink="false">http://jonswope.com/?p=33</guid>
		<description><![CDATA[I gave a talk last night at the Atlanta Ruby Users Group meetup about using JRuby on Google App Engine.  It was more of an overview about what App Engine is, and how to get started with a Sinatra app.  The Github repository for the application is located here, and you can see the application [...]]]></description>
			<content:encoded><![CDATA[<p>I gave a talk last night at the <a href="http://www.atlruby.org/" target="_blank">Atlanta Ruby Users Group</a> meetup about using JRuby on <a href="http://appengine.google.com/">Google App Engine</a>.  It was more of an overview about what App Engine is, and how to get started with a <a href="http://www.sinatrarb.com/">Sinatra</a> app.  The Github repository for the application is located <a href="http://github.com/jaswope/atlrug-appengine-example">here</a>, and you can see the application in action <a href="http://jaswope-sandbox.appspot.com/">here</a>.</p>
<p>The application demonstrates a few app engine concepts.  In <a href="http://github.com/jaswope/atlrug-appengine-example/blob/master/models.rb">models.rb</a> you&#8217;ll see a sharded counter implementation using the direct Datastore API, and a traditional model using Datamapper as your ORM (ODM?) to Datastore.  The counter also demonstrates usage of the Memcache API.  Inside <a href="http://github.com/jaswope/atlrug-appengine-example/blob/master/app.rb">app.rb</a> you can see some simple usage of the Users API, including how to check to see if a user is logged in, present a user with a login screen, and interacting with the object.  Lastly, you will find an example of how to determine your app&#8217;s name and version in <a href="http://github.com/jaswope/atlrug-appengine-example/blob/master/config.ru">config.ru</a>.</p>
<p>Following the presentation I received a number of requests for my slides, so they are included below.  Slide 13 illustrates pushing your own app to App Engine.  As I mentioned in the talk, I strongly suggest setting aside one of your app ids for testing new ideas, since you only get 10 ids total.</p>
<p><iframe src="http://docs.google.com/present/embed?id=dcdz6b48_1gs7xcznq" frameborder="0" width="410" height="342"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://jonswope.com/2010/01/14/getting-ruby-going-on-app-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

