

<?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>Cody A. Ray</title>
	<atom:link href="http://codyaray.com/feed" rel="self" type="application/rss+xml" />
	<link>http://codyaray.com</link>
	<description>Design. Technology. Sustainability. Education. Entrepreneurship.</description>
	<lastBuildDate>Sat, 26 May 2012 07:50:12 +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>Mocking static java.util.Logger with EasyMock&#8217;s PowerMock extension</title>
		<link>http://codyaray.com/2012/05/mocking-static-java-util-logger-with-easymocks-powermock-extension</link>
		<comments>http://codyaray.com/2012/05/mocking-static-java-util-logger-with-easymocks-powermock-extension#comments</comments>
		<pubDate>Fri, 25 May 2012 07:18:44 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1349</guid>
		<description><![CDATA[We have a class that needs to limit the number of logged messages to avoid DDOSes. But this particular piece of software is still using JULI (yes, I know, not my decision though). Since JULI doesn&#8217;t support NDC or MDC, we needed to get creative to test this logging limit. You probably create a static [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>We have a class that needs to limit the number of logged messages to avoid DDOSes. But this particular piece of software is still using JULI (yes, I know, not my decision though). Since JULI doesn&#8217;t support NDC or MDC, we needed to get creative to test this logging limit.</p>
<p>You probably create a static logger for your class, something like</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoggingClass <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger logger <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>LoggingClass.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>or the equivalent with SLF4J&#8217;s <code>LoggerFactory</code>.</p>
<p>PowerMock can help you mock this logger. Yes, its that powerful. First, <span id="more-1349"></span>add it as a Maven dependency (shocker). Your <code>pom.xml</code> might look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.powermock<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>powermock-module-junit4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.4.8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.powermock<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>powermock-api-easymock<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.4.8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.easymock<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>easymock<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.8.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now, in order to mock this Logger, we must suppress static initialization for the <code>LoggingClass</code> and then set the value of this static field using the PowerMock API.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.powermock.core.classloader.annotations.PrepareForTest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.powermock.core.classloader.annotations.SuppressStaticInitializationFor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.powermock.modules.junit4.PowerMockRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.powermock.reflect.Whitebox</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>PowerMockRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@SuppressStaticInitializationFor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.example.LoggingClass&quot;</span><span style="color: #009900;">&#41;</span>
@PrepareForTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>LoggingClass.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoggingClassTest <span style="color: #000000; font-weight: bold;">extends</span> EasyMockSupport <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Logger logger<span style="color: #339933;">;</span>
&nbsp;
    @Before <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        logger <span style="color: #339933;">=</span> createMock<span style="color: #009900;">&#40;</span>Logger.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Whitebox.<span style="color: #006633;">setInternalState</span><span style="color: #009900;">&#40;</span>LoggingClass.<span style="color: #000000; font-weight: bold;">class</span>, logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you just use <code>logger</code> like any other mock.</p>
<p>This approach should work for mocking other logging frameworks as well, but YMMV. You&#8217;ll need to make some PowerMock API changes since you&#8217;d be mocking a non-system class (any other 3rd party logging library). This could be as simple as using something like <code>@PrepareForTest(LoggerFactory.class)</code> in place of the one above (for SLF4J&#8217;s <code>LoggerFactory</code>). Or it might be much more difficult. I haven&#8217;t tried it.</p>
<div class="shr-publisher-1349"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2012/05/mocking-static-java-util-logger-with-easymocks-powermock-extension/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Book Review: Forging Truth</title>
		<link>http://codyaray.com/2012/04/book-review-forging-truth</link>
		<comments>http://codyaray.com/2012/04/book-review-forging-truth#comments</comments>
		<pubDate>Wed, 18 Apr 2012 17:31:57 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Commentary]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1333</guid>
		<description><![CDATA[After a number of years away, my love of reading (non-textbooks) has returned. In the last two months, I&#8217;ve finished 6 or 7 books. Last one was probably five years before. Ok, maybe an exaggeration, but you get the picture. One of the books that got me back into fiction was Forging Truth, by Raymond [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><img src="http://www.assoc-amazon.com/e/ir?t=coaraco-20&amp;l=as2&amp;o=1&amp;a=1468096214" alt="" width="1" height="1" border="0" />After a number of years away, my love of reading (non-textbooks) has returned. In the last two months, I&#8217;ve finished 6 or 7 books. Last one was probably five years before. Ok, maybe an exaggeration, but you get the picture.</p>
<p>One of the books that got me back into fiction was <em><a href="http://www.amazon.com/gp/product/1468096214/ref=as_li_ss_tl?ie=UTF8&amp;tag=codyaray-20" rel="nofollow">Forging Truth</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=coaraco-20&amp;l=as2&amp;o=1&amp;a=1468096214" alt="" width="1" height="1" border="0" /></em>, by <a title="Raymond Master's WordPress" href="http://raymondmasters.wordpress.com/">Raymond Masters</a>. (disclaimers below)</p>
<p><a href="http://www.amazon.com/gp/product/1468096214/ref=as_li_ss_il?ie=UTF8&amp;tag=codyaray-20" rel="nofollow"><img class="aligncenter" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=1468096214&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=coaraco-20&amp;ServiceVersion=20070822" alt="" border="0" /></a></p>
<p>Although I expected <em>Forging Truth</em> to be a typical fantasy/sci-fi piece, it ended up being a big mystery as well. It was this aspect that hooked me from the end of Chapter 2. The protagonist (and the reader) go through a journey of discovery, trying to recover his lost memories that are key to solving a crime against humanity. The book twisted and turned til the end, feeding you just enough information and back-story to keep you intrigued but not so much that the next pages were obvious.</p>
<p>Situated in present-day America, the novel was centered around subjects normally reserved for other (usually more medieval) contexts. This juxtaposition made for a very interesting read. Given this modern context, the language was easy and the approach to magic was &#8220;light&#8221; (i.e., unrestricted, not manna-based, etc.). However, the actual content/subject matter was a bit darker in nature, with some moderately graphic scenes. Action-packed til the end, this book left me hanging for more. Can&#8217;t wait to read the second book!</p>
<p><strong>disclaimers</strong>: the links above are Amazon-affiliate links and the author is my uncle. <img src='http://codyaray.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="shr-publisher-1333"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2012/04/book-review-forging-truth/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Early Animation &#8211; The Zoescope</title>
		<link>http://codyaray.com/2012/04/early-animation-the-zoescope</link>
		<comments>http://codyaray.com/2012/04/early-animation-the-zoescope#comments</comments>
		<pubDate>Tue, 10 Apr 2012 02:12:20 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Commentary]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1317</guid>
		<description><![CDATA[Today, Google highlighted Muybridge&#8217;s The Horse in Motion for his 182nd birthday. This early motion picture was created using a device Muybridge created called a zoopraxiscope that paved the way for modern cinematography. I&#8217;d like to follow it with another cool early animation device called the zoetrope that I learned about in my recent trip to California. [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Today, Google highlighted Muybridge&#8217;s <em>The Horse in Motion</em> for his 182nd birthday. This early motion picture was created using a device Muybridge created called a <a href="http://en.wikipedia.org/wiki/Zoopraxiscope">zoopraxiscope</a> that paved the way for modern cinematography. I&#8217;d like to follow it with another cool early animation device called the <a href="http://en.wikipedia.org/wiki/Zoetrope">zoetrope</a> that I learned about in my recent trip to California. The following clip uses the characters familiar from Toy Story, one of Pixar&#8217;s earliest hit animations.</p>
<p><iframe src="http://www.youtube.com/embed/0rVbUIrXhqw?version=3&amp;wmode=transparent" width="560" height="340" title="YouTube video player" style="background-color:#000;display:block;margin-bottom:0;max-width:100%;" frameborder="0" allowfullscreen></iframe><p style="font-size:11px;margin-top:0;"><a href="http://www.youtube.com/watch?v=0rVbUIrXhqw" target="_blank" title="Watch on YouTube">Watch this video on YouTube</a>.</p></p>
<p>The audio narration (in the voice of <em>Woody</em>) explains how the Zoetrope works, using a quickly-flashing strobe light and a rotating display. Pretty sweet, I think.</p>
<div class="shr-publisher-1317"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2012/04/early-animation-the-zoescope/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharebox &#8211; A Dropbox-like Sharing App using Ruby on Rails</title>
		<link>http://codyaray.com/2012/03/sharebox-a-dropbox-like-sharing-app-using-ruby-on-rails</link>
		<comments>http://codyaray.com/2012/03/sharebox-a-dropbox-like-sharing-app-using-ruby-on-rails#comments</comments>
		<pubDate>Tue, 20 Mar 2012 06:02:31 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1306</guid>
		<description><![CDATA[A week ago, I posted a quick fix to a bug in the Sharebox tutorial. The next day, it appears that the site hosting this tutorial expired, and its content could be lost. Never fear! Google&#8217;s cached version is here. To make it easier for you, I&#8217;ve posted the source code developed through the tutorial to [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>A week ago, I posted a <a title="Fixing the “unknown attribute: uploaded_file” error in the ShareBox Tutorial" href="http://codyaray.com/2012/03/fixing-the-unknown-attribute-uploaded_file-error-in-the-sharebox-tutorial">quick fix</a> to a bug in the Sharebox tutorial. The next day, it appears that the site hosting this tutorial expired, and its content could be lost. Never fear! <a title="Sharebox Tutorial via Google Cache" href="http://webcache.googleusercontent.com/search?q=cache:http://www.prowl.in/sharebox.html">Google&#8217;s cached version</a> is here. To make it easier for you, I&#8217;ve posted the source code developed through the tutorial to my Github and you can see Sharebox in action on heroku. Lastly, I&#8217;m also hosting a cached copy of the tutorial for safekeeping, in the event its lost overwritten in Google&#8217;s cache.</p>
<ul>
<li><a title="Sharebox Source Code on Github" href="https://github.com/codyaray/sharebox">Sharebox source code on Github</a></li>
<li><a title="Sharebox Demo" href="http://shareboxapp.heroku.com/" target="_blank">Sharebox demo on Heroku</a></li>
<li><a title="Sharebox tutorial (hosted cached copy)" href="http://codyaray.com/downloads/2012/03/How-to-Build-a-Dropbox-like-Sharing-App-using-Ruby-on-Rails.html">Sharebox tutorial (hosted cached copy)</a></li>
</ul>
<p>Hope this helps!</p>
<div class="shr-publisher-1306"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2012/03/sharebox-a-dropbox-like-sharing-app-using-ruby-on-rails/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fixing the &#8220;unknown attribute: uploaded_file&#8221; error in the ShareBox Tutorial</title>
		<link>http://codyaray.com/2012/03/fixing-the-unknown-attribute-uploaded_file-error-in-the-sharebox-tutorial</link>
		<comments>http://codyaray.com/2012/03/fixing-the-unknown-attribute-uploaded_file-error-in-the-sharebox-tutorial#comments</comments>
		<pubDate>Tue, 13 Mar 2012 03:32:26 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1285</guid>
		<description><![CDATA[While going through the (quite good) ShareBox Tutorial that teaches you to build a Dropbox-like sharing app, I stumbled on a nasty bug. The tutorial wisely uses the paperclip gem for file attachments, but after moving from local hosting to Amazon&#8217;s S3, I started getting an exception in the AssetsController create action.   ActiveRecord::UnknownAttributeError in AssetsController#create [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>While going through the (quite good) <a title="ShareBox Tutorial" href="http://www.prowl.in/sharebox.html" target="_blank">ShareBox Tutorial</a> that teaches you to build a Dropbox-like sharing app, I stumbled on a nasty bug. The tutorial wisely uses the paperclip gem for file attachments, but after moving from local hosting to Amazon&#8217;s S3, I started getting an exception in the AssetsController <code>create</code> action.<br />
<code></code></p>
<pre>  ActiveRecord::UnknownAttributeError in AssetsController#create
  unknown attribute: uploaded_file
  app/controllers/assets_controller:17:in `create'</pre>
<p>At the time, Googling the error didn&#8217;t help (gasp!), so here are all the pieces together to help any wary travelers who may stumble here looking for the same answer. <span id="more-1285"></span>Turns out, the error doesn&#8217;t indicate the real problem at all. Fortunately, however, the fix is simple.</p>
<ol>
<li>In the asset model file, asset.rb, change the path to <code>:s3_credentials</code> from using <code>RAILS_ROOT</code> to <code>Rails.root</code>.</li>
<li>In the Gemfile, replace <code>'aws-s3'</code> with <code>'awk-sdk'</code>. Don&#8217;t forget to <code>bundle install</code></li>
</ol>
<p>That&#8217;s all!</p>
<p>Credits go to</p>
<ul>
<li><a title="Sikachu's user profile on GitHub" href="https://github.com/sikachu" target="_blank">Sikachu</a> for pointing out the Rails_ROOT problem in this <a title="GitHub issue" href="https://github.com/thoughtbot/paperclip/issues/523" target="_blank">issue</a></li>
<li><a href="https://github.com/thoughtbot/paperclip" target="_blank">Paperclip</a>&#8216;s rubydocs for <a title="Paperclip::Storage::S3 on rubydoc.info" href="http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3" target="_blank">S3 Storage</a> for the new dependency tip</li>
</ul>
<div class="shr-publisher-1285"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2012/03/fixing-the-unknown-attribute-uploaded_file-error-in-the-sharebox-tutorial/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PyYAML using easy_install on Mac OS X (Lion)</title>
		<link>http://codyaray.com/2011/12/pyyaml-using-easy_install-on-mac-os-x-lion</link>
		<comments>http://codyaray.com/2011/12/pyyaml-using-easy_install-on-mac-os-x-lion#comments</comments>
		<pubDate>Wed, 07 Dec 2011 01:50:13 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1273</guid>
		<description><![CDATA[Tried to install pyyaml today but got a couple nasty errors. $ sudo easy_install pyyaml Searching for pyyaml Reading http://pypi.python.org/simple/pyyaml/ Reading http://pyyaml.org/wiki/PyYAML Best match: PyYAML 3.10 Downloading http://pyyaml.org/download/pyyaml/PyYAML-3.10.zip Processing PyYAML-3.10.zip Running PyYAML-3.10/setup.py -q bdist_egg --dist-dir /tmp/easy_install-8Co0C6/PyYAML-3.10/egg-dist-tmp-FYdiLy build/temp.macosx-10.7-intel-2.7/check_libyaml.c:2:18: error: yaml.h: No such file or directory build/temp.macosx-10.7-intel-2.7/check_libyaml.c: In function ‘main’: build/temp.macosx-10.7-intel-2.7/check_libyaml.c:5: error: ‘yaml_parser_t’ undeclared &#40;first use in [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Tried to install pyyaml today but got a couple nasty errors.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install  pyyaml
Searching <span style="color: #000000; font-weight: bold;">for</span> pyyaml
Reading http:<span style="color: #000000; font-weight: bold;">//</span>pypi.python.org<span style="color: #000000; font-weight: bold;">/</span>simple<span style="color: #000000; font-weight: bold;">/</span>pyyaml<span style="color: #000000; font-weight: bold;">/</span>
Reading http:<span style="color: #000000; font-weight: bold;">//</span>pyyaml.org<span style="color: #000000; font-weight: bold;">/</span>wiki<span style="color: #000000; font-weight: bold;">/</span>PyYAML
Best match: PyYAML <span style="color: #000000;">3.10</span>
Downloading http:<span style="color: #000000; font-weight: bold;">//</span>pyyaml.org<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>pyyaml<span style="color: #000000; font-weight: bold;">/</span>PyYAML-<span style="color: #000000;">3.10</span>.zip
Processing PyYAML-<span style="color: #000000;">3.10</span>.zip
Running PyYAML-<span style="color: #000000;">3.10</span><span style="color: #000000; font-weight: bold;">/</span>setup.py <span style="color: #660033;">-q</span> bdist_egg <span style="color: #660033;">--dist-dir</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>easy_install-8Co0C6<span style="color: #000000; font-weight: bold;">/</span>PyYAML-<span style="color: #000000;">3.10</span><span style="color: #000000; font-weight: bold;">/</span>egg-dist-tmp-FYdiLy
build<span style="color: #000000; font-weight: bold;">/</span>temp.macosx-<span style="color: #000000;">10.7</span>-intel-<span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>check_libyaml.c:<span style="color: #000000;">2</span>:<span style="color: #000000;">18</span>: error: yaml.h: No such <span style="color: #c20cb9; font-weight: bold;">file</span> or directory
build<span style="color: #000000; font-weight: bold;">/</span>temp.macosx-<span style="color: #000000;">10.7</span>-intel-<span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>check_libyaml.c: In <span style="color: #000000; font-weight: bold;">function</span> ‘main’:
build<span style="color: #000000; font-weight: bold;">/</span>temp.macosx-<span style="color: #000000;">10.7</span>-intel-<span style="color: #000000;">2.7</span><span style="color: #000000; font-weight: bold;">/</span>check_libyaml.c:<span style="color: #000000;">5</span>: error: ‘yaml_parser_t’ undeclared <span style="color: #7a0874; font-weight: bold;">&#40;</span>first use <span style="color: #000000; font-weight: bold;">in</span> this <span style="color: #000000; font-weight: bold;">function</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
...
lipo: can<span style="color: #ff0000;">'t open input file: /var/tmp//ccxDQipT.out (No such file or directory)</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">libyaml is not found or a compiler error: forcing <span style="color: #660033;">--without-libyaml</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">if</span> libyaml is installed correctly, you may need to
 specify the option <span style="color: #660033;">--include-dirs</span> or uncomment and
 modify the parameter include_dirs <span style="color: #000000; font-weight: bold;">in</span> setup.cfg<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Fortunately, after piecing together a couple posts, the fix is easy. Use the following command to install PyYAML.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> python <span style="color: #660033;">-m</span> easy_install pyyaml</pre></div></div>

<p>We must install this way because the problem is a bug in an Python&#8217;s setuptools. Its been fixed in later versions, but for now this did the trick for me.</p>
<h3>Resources</h3>
<ul>
<li>http://pyyaml.org/ticket/186</li>
<li>http://pyyaml.org/ticket/94</li>
<li>http://andreasjacobsen.com/2008/10/10/using-python-setuptools-on-the-mac/</li>
</ul>
<div class="shr-publisher-1273"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2011/12/pyyaml-using-easy_install-on-mac-os-x-lion/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse Start-Up Crash on Mac OS X Lion</title>
		<link>http://codyaray.com/2011/11/eclipse-start-up-crash-on-mac-os-x-lion</link>
		<comments>http://codyaray.com/2011/11/eclipse-start-up-crash-on-mac-os-x-lion#comments</comments>
		<pubDate>Fri, 25 Nov 2011 04:57:53 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1268</guid>
		<description><![CDATA[Today, I was working from my parent&#8217;s home in Arkansas where we don&#8217;t have broadband, so my Mac was offline. I tried to start Eclipse and it just popped a dialog mentioning a log file with no other message. After a bit of searching the errors in the log, Stack Overflow came to the rescue. just [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Today, I was working from my parent&#8217;s home in Arkansas where we don&#8217;t have broadband, so my Mac was offline. I tried to start Eclipse and it just popped a dialog mentioning a log file with no other message.</p>
<p>After a bit of searching the errors in the log, <a title="InetAddress.getLocalHost resolution on OS X when offline" href="http://stackoverflow.com/questions/7892609/inetaddress-getlocalhost-resolution-on-osx-lion-when-offline">Stack Overflow came to the rescue</a>.</p>
<p style="padding-left: 30px;">just add an alias for localhost to the network interface:</p>
<pre style="padding-left: 30px;"><code>sudo ifconfig en0 alias 127.0.0.1</code></pre>
<p>That&#8217;s all you have to do, really.</p>
<div class="shr-publisher-1268"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2011/11/eclipse-start-up-crash-on-mac-os-x-lion/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lost Your Kindle? Email A Last-Ditch Note to the Finder/Thief.</title>
		<link>http://codyaray.com/2011/09/lost-your-kindle-email-a-last-ditch-note-to-the-finderthief</link>
		<comments>http://codyaray.com/2011/09/lost-your-kindle-email-a-last-ditch-note-to-the-finderthief#comments</comments>
		<pubDate>Tue, 20 Sep 2011 04:38:50 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1264</guid>
		<description><![CDATA[About a week ago, I lost my Kindle. This makes me sad. After tracking it on security cameras until it left the film of vision (left in cart at grocery store), I&#8217;ve devised a last ditch effort. Remember that convenience method of emailing documents to your kindle? Well, I&#8217;ve just created a PDF pleading for its [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>About a week ago, I lost my Kindle. This makes me sad. After tracking it on security cameras until it left the film of vision (left in cart at grocery store), I&#8217;ve devised a last ditch effort. Remember that convenience method of emailing documents to your kindle? Well, I&#8217;ve just created a PDF pleading for its return, including my contact info. Will it work? Maybe not, but it can&#8217;t hurt either. You can do this yourself.</p>
<ol>
<li>Write a &#8220;please return my Kindle&#8221; note in MS Word. I used size 20 font, all caps, and short sentences. <span id="more-1264"></span>After my first pleading sentences<br />
PLEASE RETURN MY KINDLE. I MISS IT. ☹<br />
I included my address, phone, and email to maximize the opportunity for contact. (I live in a building with card access and the building manager&#8217;s office by the entry, so I&#8217;m not overly concerned with security here.) I even listed a location next to the grocery store it was left in as a potential drop off location. Lastly, I offered a small finders fee if it helps to incentivize them.</li>
<li>Save as (or print-to) PDF with filename &#8220;PLEASE RETURN MY KINDLE.&#8221; My assumption is that this will be the title as it appears in the Kindle itself.</li>
<li>Email to your name@free.kindle.com with the subject &#8220;convert&#8221;. This will convert it to Kindle format for easier reading, i.e., no need to do zoom/positioning to get the best view of the text. Make it as easy as possible for them to return, if they have any intention of doing so.</li>
</ol>
<p style="text-align: left;">I&#8217;ll let you know if it works. In the meantime, <em><strong>if you try this, let me know the results (if any).</strong></em></p>
<div class="shr-publisher-1264"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2011/09/lost-your-kindle-email-a-last-ditch-note-to-the-finderthief/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Simple REST API Testing Setup using Vows.js, Tobi, and Node.js</title>
		<link>http://codyaray.com/2011/09/simple-rest-api-testing-setup-using-vows-js-tobi-and-node-js</link>
		<comments>http://codyaray.com/2011/09/simple-rest-api-testing-setup-using-vows-js-tobi-and-node-js#comments</comments>
		<pubDate>Sun, 18 Sep 2011 07:48:15 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1236</guid>
		<description><![CDATA[I&#8217;m learning so many new things at BrightTag! Among them, I&#8217;m helping develop a new service in node.js. After piecing together a few articles online about using node for asynchronous testing of REST services, I thought I&#8217;d share the testing setup I&#8217;d settled on, in case it can help you. The setup is written for vowsjs [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I&#8217;m learning so many new things at BrightTag! Among them, I&#8217;m helping develop a new service in node.js. After piecing together a <a title="Vows.js" href="http://vowsjs.org/" target="_blank">few</a> <a title="REST API Testing using Vows, Tobi, and Node" href="http://stackoverflow.com/questions/6918697/rest-api-testing-using-vows-tobi-and-node-js" target="_blank">articles</a> <a title="Testing REST APIs with Learnboost's Tobi + Vows.js" href="http://vilimpoc.org/blog/2011/01/19/testing-rest-api-with-learnboosts-tobi-vows-js/#comments" target="_blank">online</a> about using node for asynchronous testing of REST services, I thought I&#8217;d share the testing setup I&#8217;d settled on, in case it can help you. The setup is written for vowsjs and can easily be tailored to your situation. I&#8217;ve tried to make heavy use of asynchronization, macros, and contextualized requests. In the case below, I was integration-testing an oauth security middleware (simplified here for, well, simplicity).<span id="more-1236"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> vows <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'vows'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    assert <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'assert'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    should <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    tobi <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tobi'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> HOST <span style="color: #339933;">=</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #339933;">,</span>
    PORT <span style="color: #339933;">=</span> <span style="color: #CC0000;">8087</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> suite <span style="color: #339933;">=</span> vows.<span style="color: #660066;">describe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'My API Security'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    browser <span style="color: #339933;">=</span> tobi.<span style="color: #660066;">createBrowser</span><span style="color: #009900;">&#40;</span>PORT<span style="color: #339933;">,</span> HOST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> client <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  get<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> header<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    browser.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> headers<span style="color: #339933;">:</span> header <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  post<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> header<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    browser.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> body<span style="color: #339933;">:</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> headers<span style="color: #339933;">:</span> header <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">//Send a request and check the response status.</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #003366; font-weight: bold;">function</span> respondsWith<span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    topic<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: #006600; font-style: italic;">// Get the current context's name, such as &quot;POST / access_token&quot;</span>
      <span style="color: #006600; font-style: italic;">// and split it at the spaces.</span>
      <span style="color: #003366; font-weight: bold;">var</span> req    <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">context</span>.<span style="color: #000066;">name</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/ +/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// [&quot;POST&quot;, &quot;/&quot;, &quot;access_token&quot;]</span>
          method <span style="color: #339933;">=</span> req<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>          <span style="color: #006600; font-style: italic;">// &quot;post&quot;</span>
          path   <span style="color: #339933;">=</span> req<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>                        <span style="color: #006600; font-style: italic;">// &quot;/&quot;</span>
          token  <span style="color: #339933;">=</span> req<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>                        <span style="color: #006600; font-style: italic;">// &quot;access_token&quot;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> header <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'Authorization'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Bearer '</span> <span style="color: #339933;">+</span> token <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// Perform the contextual client request,</span>
      <span style="color: #006600; font-style: italic;">// with the above method and path.</span>
      client<span style="color: #009900;">&#91;</span>method<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> header<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callback</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: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Create and assign the vows to the context.</span>
&nbsp;
  context<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'should respond with a '</span> <span style="color: #339933;">+</span> <span style="color: #000066;">status</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span>
         <span style="color: #339933;">+</span> http.<span style="color: #660066;">STATUS_CODES</span><span style="color: #009900;">&#91;</span><span style="color: #000066;">status</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> assertStatus<span style="color: #009900;">&#40;</span>res.<span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  errorMessage <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>error <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'out errors'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">' an &quot;'</span><span style="color: #339933;">+</span>error<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; error'</span><span style="color: #339933;">;</span>
  context<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'should respond with'</span> <span style="color: #339933;">+</span> errorMessage<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> assertHeaders<span style="color: #009900;">&#40;</span>res.<span style="color: #660066;">error</span><span style="color: #339933;">,</span> res.<span style="color: #660066;">errorDescription</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> context<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> assertStatus<span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>res<span style="color: #339933;">,</span> $<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    res.<span style="color: #660066;">should</span>.<span style="color: #660066;">have</span>.<span style="color: #000066;">status</span><span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> assertHeaders<span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> errorDescription<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>res<span style="color: #339933;">,</span> $<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error <span style="color: #339933;">||</span> errorDescription<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      res.<span style="color: #660066;">should</span>.<span style="color: #660066;">have</span>.<span style="color: #660066;">header</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'www-authenticate'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      header <span style="color: #339933;">=</span> oauth.<span style="color: #660066;">parseAuthenticationHeader</span><span style="color: #009900;">&#40;</span>res.<span style="color: #660066;">headers</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'www-authenticate'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> error.<span style="color: #660066;">should</span>.<span style="color: #660066;">equal</span><span style="color: #009900;">&#40;</span>header.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>errorDescription<span style="color: #009900;">&#41;</span> errorDescription.<span style="color: #660066;">should</span>.<span style="color: #660066;">equal</span><span style="color: #009900;">&#40;</span>header.<span style="color: #660066;">error_description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      res.<span style="color: #660066;">should</span>.<span style="color: #660066;">not</span>.<span style="color: #660066;">have</span>.<span style="color: #660066;">property</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'www-authenticate'</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;">&#125;</span>
&nbsp;
suite.<span style="color: #660066;">addBatch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'GET /resource/1 authorized'</span><span style="color: #339933;">:</span> respondsWith<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066;">status</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'GET /resource/1 un@vth0riz3d'</span><span style="color: #339933;">:</span> respondsWith<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066;">status</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">401</span><span style="color: #339933;">,</span> error<span style="color: #339933;">:</span> <span style="color: #3366CC;">'invalid_token'</span><span style="color: #339933;">,</span> errorDescription<span style="color: #339933;">:</span> <span style="color: #3366CC;">'The access token is invalid'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'GET /resource/1 expired'</span><span style="color: #339933;">:</span> respondsWith<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066;">status</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">401</span><span style="color: #339933;">,</span> error<span style="color: #339933;">:</span> <span style="color: #3366CC;">'invalid_token'</span><span style="color: #339933;">,</span> errorDescription<span style="color: #339933;">:</span> <span style="color: #3366CC;">'The access token is expired'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'GET /resource/1 insufficient'</span><span style="color: #339933;">:</span> respondsWith<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066;">status</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">403</span><span style="color: #339933;">,</span> error<span style="color: #339933;">:</span> <span style="color: #3366CC;">'insufficient_scope'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
suite.<span style="color: #003366; font-weight: bold;">export</span><span style="color: #009900;">&#40;</span>module<span style="color: #009900;">&#41;</span></pre></div></div>

<p>This pattern is nice because adding new tests is a real piece of cake now that we have the contextualized request macro, and this macro pattern is easy to tailor to your situation. It also demonstrates how to set two contexts, which was left &#8220;to the reader&#8221; on the <a title="Vows Guide" href="http://vowsjs.org/#-macros">vows guide</a>. You can even imagine adding more vows and assertions, such as one that checks the body content, into the macro, as I&#8217;ve done in the real case.</p>
<p><em><strong>Can you refactor or modify this to make it cleaner still? Let me know in the comments.</strong></em></p>
<div class="shr-publisher-1236"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2011/09/simple-rest-api-testing-setup-using-vows-js-tobi-and-node-js/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Delete Lines Matching Regular Expression From Multiple Files</title>
		<link>http://codyaray.com/2011/08/delete-lines-matching-regular-expression-from-multiple-files</link>
		<comments>http://codyaray.com/2011/08/delete-lines-matching-regular-expression-from-multiple-files#comments</comments>
		<pubDate>Thu, 18 Aug 2011 15:27:52 +0000</pubDate>
		<dc:creator>codyaray</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://codyaray.com/?p=1203</guid>
		<description><![CDATA[Today, I had to remove 650 instances of a line matching a certain pattern scattered across 145 different XML files. Not a pleasant task. (If you&#8217;re wondering, I&#8217;m deprecating a field in the XML DTD and wished to remove all current instances). Just to save you all the searching and debugging, here is the final [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Today, I had to remove 650 instances of a line matching a certain pattern scattered across 145 different XML files. Not a pleasant task. (If you&#8217;re wondering, I&#8217;m deprecating a field in the XML DTD and wished to remove all current instances).</p>
<p>Just to save you all the searching and debugging, here is the final form and my notes.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-rl</span> <span style="color: #ff0000;">'^&lt;pattern&gt;$'</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-i</span> .bak <span style="color: #ff0000;">'/^&lt;pattern&gt;$/d'</span></pre></div></div>

<p><span id="more-1203"></span><br />
Note a difference in the regex as used in <tt>grep</tt> and <tt>sed</tt>: in <tt>sed</tt>, the parenthesis are escaped, like <tt>\(.*\)</tt>, as are forward slashes which delimit the regex, like <tt>\/</tt>. However, since you&#8217;re just deleted an entire line, parenthesis probably shouldn&#8217;t be needed.</p>
<p>For bonus points, you can count the number of instances of a  pattern scattered across a number of files using</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-rc</span> <span style="color: #ff0000;">'^&lt;pattern&gt;$'</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{sum += $1} END {print sum}'</span></pre></div></div>

<p>And to finish it off, here&#8217;s a real live example!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-rl</span> <span style="color: #ff0000;">'^.*&lt;length&gt;(.*)&lt;/length&gt;\w*$'</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-i</span> .bak <span style="color: #ff0000;">'/^.*&lt;length&gt;\([0-9]*\)&lt;\/length&gt;\w*$/d'</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-rc</span> <span style="color: #ff0000;">'^.*&lt;length&gt;(.*)&lt;/length&gt;\w*$'</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{sum += $1} END {print sum}'</span></pre></div></div>

<div class="shr-publisher-1203"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://codyaray.com/2011/08/delete-lines-matching-regular-expression-from-multiple-files/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.900 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-30 23:38:02 -->

