<?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>chris moos&#039;s blog &#187; ruby</title>
	<atom:link href="http://chrismoos.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrismoos.com</link>
	<description>coding my way through life</description>
	<lastBuildDate>Sun, 18 Sep 2011 19:48:04 +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>AsyncRecord: Non-blocking database access for&#160;Ruby</title>
		<link>http://chrismoos.com/2010/06/21/asyncrecord-non-blocking-database-access-for-ruby/</link>
		<comments>http://chrismoos.com/2010/06/21/asyncrecord-non-blocking-database-access-for-ruby/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 04:17:18 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[eventmachine]]></category>
		<category><![CDATA[fastr]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=809</guid>
		<description><![CDATA[Two weeks ago I developed my first event-driven web framework for Ruby, Fastr. It helped me understand why running a web framework in an event loop is so natural. As I continued to tackle more features in Fastr, it was time to tackle persistence &#8212; notably, database access. AsyncRecord is/will be an ORM, similar to [...]]]></description>
			<content:encoded><![CDATA[<p>Two weeks ago I developed my first event-driven web framework for Ruby, <a href="http://chrismoos.com/2010/06/08/fastr-ruby-web-framework/">Fastr</a>. It helped me understand why running a web framework in an event loop is so <em>natural</em>. </p>
<p>As I continued to tackle more features in Fastr, it was time to tackle persistence &#8212; notably, database access. </p>
<p>AsyncRecord is/will be an ORM, similar to ActiveRecord &#8212; with one major difference &#8212; it doesn&#8217;t block. AsyncRecord currently uses <a href="http://github.com/tmm1/em-mysql">em-mysql</a> to access a MySQL database.</p>
<h2>How it usually works</h2>
<p>In most ORMs, when you attempt to access the database, everything in that thread will block until a response is received. This means that you waste time &#8212; just waiting. The CPU may be idle, but you cannot handle any more requests. (Typically you start multiple instances of your application to get past this, unfortunately each instance requires more resources on your server)</p>
<h2>How AsyncRecord works</h2>
<p>When you access something in the database with AsyncRecord, the request is sent to the database server, but control returns to the application immediately after the packet(s) are sent. When the server responds, which could be 20ms or 200ms later, a callback that you specify is invoked. </p>
<p>One important thing about accessing a database asynchronously, especially in web frameworks, is the ability to defer a response. Fastr has built-in support for deferred responses, a-la EventMachine/Thin.</p>
<p> A deferred response is when you tell the web server that you will send data to the client some time in the future, and the server is free to handle more requests until you are ready to respond.</p>
<h2>Benchmarking</h2>
<p>As I was implementing AsyncRecord, I knew it would be faster &#8212; but I wasn&#8217;t sure by how much. I setup a very simple Rails 2.3.5 application, as well as a Fastr application (from the latest source).</p>
<p>My goal was to make an application that has a single page, which shows 5 usernames from the database.</p>
<p><b>Rails controller</b>:</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> MainController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> index<br />
&nbsp; &nbsp; users = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; User.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; users <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> user.<span style="color:#9900CC;">username</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; headers<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'Content-Type'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'text/plain'</span><br />
&nbsp; &nbsp; render<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> users.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p><b>Fastr controller:</b></p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> MainController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Fastr::Controller</span> &nbsp;<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> index<br />
&nbsp; &nbsp; defer_response<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">200</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> <span style="color:#996600;">&quot;text/plain&quot;</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>response<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; User.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">5</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>users<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; users.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">send_data</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{user.username}<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">succeed</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<h2>The Numbers</h2>
<h3>Fastr</h3>
<p>Average Latency: 123ms<br />
Requests per second: 385 r/s</p>
<p><img style="width: 600px;" src="http://cdn.chrismoos.com/wp-content/uploads/2010/06/fastr.png"/></p>
<h3>Rails</h3>
<p>Average Latency: 2040ms<br />
Requests per second: 42 r/s</p>
<p><img style="width: 600px;" src="http://cdn.chrismoos.com/wp-content/uploads/2010/06/rails.png"/></p>
<p>The tests were performed using JMeter. 100 concurrent requests (10 requests per connection).</p>
<p>I also ran some tests using apache bench, here are the results:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ab <span style="color: #660033;">-c</span> <span style="color: #000000;">100</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1000</span> http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">5000</span><span style="color: #000000; font-weight: bold;">/</span></div></td></tr></tbody></table></div>
<h3>Fastr</h3>
<p>Average Latency: 90ms<br />
Requests per second: 1100 r/s</p>
<h3>Rails</h3>
<p>Average Latency: 2235ms<br />
Requests per second: 44 r/s</p>
<h2>Conclusion</h2>
<p>I am extremely happy with what AsyncRecord can do &#8212; and I hope to make it even better. I will be moving it out of Fastr and into its own project soon.</p>
<p>Fastr GitHub: <a href="http://github.com/chrismoos/fastr">http://github.com/chrismoos/fastr</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2010/06/21/asyncrecord-non-blocking-database-access-for-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fastr &#8211; A Web Framework for&#160;Ruby</title>
		<link>http://chrismoos.com/2010/06/08/fastr-ruby-web-framework/</link>
		<comments>http://chrismoos.com/2010/06/08/fastr-ruby-web-framework/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 04:12:06 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[web framework]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=787</guid>
		<description><![CDATA[Foreword Every month I go on a binge and learn something new. The most recent binge resulted in a new web framework, called fastr. I&#8217;ve always used Rails when I needed to create a web application in Ruby, but despite how great it is to code in, the performance and concurrency is not up to [...]]]></description>
			<content:encoded><![CDATA[<h2>Foreword</h2>
<p>Every month I go on a binge and learn something new. The most recent binge resulted in a new web framework, called <a href="http://github.com/chrismoos/fastr">fastr</a>.</p>
<p>I&#8217;ve always used Rails when I needed to create a web application in Ruby, but despite how great it is to code in, the performance and concurrency is not up to par. </p>
<p>Don&#8217;t get me wrong &#8212; Rails can scale, but it is not inherently very good when it comes to an individual instance&#8217;s performance.  While this is hopefully changing in Rails 3, I wanted to see what kind of concurrency and performance I could get out of a simple <a href="http://rubyeventmachine.com/">EventMachine</a> web application. Thus, fastr was born.</p>
<h2>Fastr&#8217;s Architecture</h2>
<p>Fastr is nothing special. It is an extremely simple MVC style web framework, similar in configuration to Rails.</p>
<p>What makes it different though, is how it handles requests and responses.</p>
<p>The core of fastr runs on an EventMachine reactor, which is basically just a big event loop. EventMachine is very capable of maintaining thousands of connections in one instance. This is great when you want an application that supports Comet and chunked responses. There is very minimal overhead in having thousands of connections that have occasional data flowing through.</p>
<p>EventMachine and Fastr also support the ability to defer a response until a long running task finishes executing. This is nice because it won&#8217;t block the rest of the application. The only catch is you are limited to EventMachine&#8217;s thread pool size, which I believe defaults to 20. You can always up this number though, if you want.</p>
<h2>Routing</h2>
<p>The routing is very similar to Rails, an example routes.rb file looks like this:</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;router.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>route<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; route.<span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#996600;">'/:controller/:action'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;">#route.for '/home/:action', :action =&gt; '[A-Za-z]+'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;">#route.for '/test', :to =&gt; 'home#index'</span><br />
&nbsp;<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<h2>Controllers</h2>
<p>Controllers are simple, with just a list of actions that are called based on the route. Actions should return a valid Rack response. There are convenience methods in the controller that help in creating Rack responses.</p>
<p>The nice thing about the controllers in Fastr is that you can defer responses with ease. Here is an example of a deferred response:</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> long_running_task<br />
&nbsp; &nbsp; defer_response<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">200</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> <span style="color:#996600;">&quot;text/plain&quot;</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>response<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;in our deferred response...now we can do cool stuff!&quot;</span><br />
&nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">send_data</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;hey<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; long_task = <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color:#9900CC;">debug</span> <span style="color:#996600;">&quot;Sleeping for 5 seconds...but this won't block other requests&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">sleep</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color:#9900CC;">debug</span> <span style="color:#996600;">&quot;Finished sleeping, returning response to client.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;">&quot;finished&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; callback = <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>result<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; log.<span style="color:#9900CC;">debug</span> <span style="color:#996600;">&quot;Callback result: #{result}&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">send_data</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{result}<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">succeed</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; response.<span style="color:#9900CC;">task</span><span style="color:#006600; font-weight:bold;">&#40;</span>long_task, callback<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>The alternative render methods also exist, where you can render a HAML template or just text:</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">render<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:text</span>, <span style="color:#996600;">&quot;Hello, world!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
render<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:haml</span>, <span style="color:#ff3333; font-weight:bold;">:template</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;index&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># this searches for index.haml in your app/views/ folder</span></div></td></tr></tbody></table></div>
<h2>Future</h2>
<p>For now, fastr is just a toy framework for me, that I hope to share with anyone interested in learning about building web frameworks.</p>
<p>I intend to start writing some tests as well as making fastr more robust. I don&#8217;t mean to displace any other Ruby web framework, but as I learn with fastr I hope to contribute what I learn back to projects like Rails and Sinatra.</p>
<p>Also, I&#8217;d like to get em-mysql or equivalent integrated so that you can really enjoy a event-driven web framework &#8212; event with database access.</p>
<p>Check out the GitHub page for more examples on getting fastr up and running.</p>
<p>GitHub: <a href="http://github.com/chrismoos/fastr">http://github.com/chrismoos/fastr</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2010/06/08/fastr-ruby-web-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MoosTrax Ruby&#160;Library</title>
		<link>http://chrismoos.com/2009/02/23/moostrax-ruby-library/</link>
		<comments>http://chrismoos.com/2009/02/23/moostrax-ruby-library/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 21:28:35 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[moostrax]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=246</guid>
		<description><![CDATA[You can now access MoosTrax with Ruby. Install the gem 12$ wget http://www.moostrax.com/static/MoosTrax-0.1.gem $ sudo gem install MoosTrax-0.1.gem Try out this demo script make sure to fill your API_KEY in. 12345678910111213141516171819202122232425262728293031323334require 'rubygems' require 'moostrax' require 'time' def get_local_date&#40;date_str&#41; &#160; &#160; Time.parse&#40;date_str + ' UTC'&#41;.getlocal.asctime end mt = MoosTrax.new&#40;'API_KEY'&#41; begin &#160; &#160; devices = mt.devices &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>You can now access MoosTrax with Ruby.</p>
<h3>Install the gem</h3>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.moostrax.com<span style="color: #000000; font-weight: bold;">/</span>static<span style="color: #000000; font-weight: bold;">/</span>MoosTrax-<span style="color: #000000;">0.1</span>.gem<br />
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> MoosTrax-<span style="color: #000000;">0.1</span>.gem</div></td></tr></tbody></table></div>
<h3>Try out this demo script</h3>
<p>make sure to fill your API_KEY in.</p>
<div class="codecolorer-container ruby blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'moostrax'</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'time'</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">def</span> get_local_date<span style="color:#006600; font-weight:bold;">&#40;</span>date_str<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>date_str <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">' UTC'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">getlocal</span>.<span style="color:#9900CC;">asctime</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
mt = MoosTrax.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'API_KEY'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">begin</span><br />
&nbsp; &nbsp; devices = mt.<span style="color:#9900CC;">devices</span><br />
&nbsp; &nbsp; devices.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>device<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; info = mt.<span style="color:#9900CC;">device_info</span><span style="color:#006600; font-weight:bold;">&#40;</span>device<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Device ID: #{info['device_id']}<span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Device Name: #{info['device_name']}<span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Last Contact: #{get_local_date(info['last_contact'])}<span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Date Added: #{get_local_date(info['date_added'])}<span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; location = mt.<span style="color:#9900CC;">location</span><span style="color:#006600; font-weight:bold;">&#40;</span>device<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Most Recent Location: #{location['latitude']}, #{location['longitude']} #{get_local_date(location['date'])}<span style="color:#000099;">\r</span><span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; history = mt.<span style="color:#9900CC;">history</span><span style="color:#006600; font-weight:bold;">&#40;</span>device, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">86400</span>, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Number of location updates in past 24 hours: #{history.length}<span style="color:#000099;">\r</span><span style="color:#000099;">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;First Location in History: #{history[0]['latitude']}, #{history[0]['longitude']}<span style="color:#000099;">\r</span><span style="color:#000099;">\n</span>&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> history.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span>=<span style="color:#006666;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'------------------------------------------------------'</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">MoosTrax::APIError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e<br />
&nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Unable to access MoosTrax: #{e.message}&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></td></tr></tbody></table></div>
<p>For more information, check out the <a href="http://trac.moostrax.com/trac/wiki/Developer_lib_Ruby">Developer API Wiki</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2009/02/23/moostrax-ruby-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

