<?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>Floorplanner Tech Blog &#187; XML</title>
	<atom:link href="http://techblog.floorplanner.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Our latest geek adventures!</description>
	<lastBuildDate>Tue, 16 Mar 2010 18:45:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>RESTful uploading of files using XML</title>
		<link>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/</link>
		<comments>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 11:34:31 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=940</guid>
		<description><![CDATA[After searching for too long to find any documentation on this topic, this is just as a reminder for myself. Hopefully, it&#8217;ll solve somebody else&#8217;s problem as well.
To send a file to a Rails application, using a RESTful XML API call, use the following XML snippet:

&#60;field type=&#34;file&#34; name=&#34;filename.ext&#34; content-type=&#34;mime/type&#34;&#62;
  base64-encoded file contents
&#60;/field&#62;

The file contents [...]]]></description>
			<content:encoded><![CDATA[<p>After searching for too long to find any documentation on this topic, this is just as a reminder for myself. Hopefully, it&#8217;ll solve somebody else&#8217;s problem as well.</p>
<p>To send a file to a Rails application, using a RESTful XML API call, use the following XML snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;filename.ext&quot;</span> <span style="color: #000066;">content-type</span>=<span style="color: #ff0000;">&quot;mime/type&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  base64-encoded file contents
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The file contents should be base64 encoded, and may be encapsulated in a CDATA block:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;greeting</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;greeting.txt&quot;</span> <span style="color: #000066;">content-type</span>=<span style="color: #ff0000;">&quot;text/plain&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #339933;">&lt;![CDATA[SGVsbG8gd29ybGQh]]&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/greeting<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You can test this by opening a console and feed the XML to <code>Hash.from_xml</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family: Monaco,monospace;">&gt;&gt; greeting = &lt;&lt;-EOXML
    &lt;greeting type=&quot;file&quot; name=&quot;greeting.txt&quot; content-type=&quot;text/plain&quot;&gt;
      &lt;![CDATA[SGVsbG8gd29ybGQh]]&gt;
    &lt;/greeting&gt;
  EOXML
&nbsp;
&gt;&gt; data = Hash.from_xml(greeting)
=&gt; {&quot;greeting&quot;=&gt;#&lt;StringIO:0x10617d9d0&gt;}
&gt;&gt; data['greeting'].content_type
=&gt; &quot;text/plain&quot;
&gt;&gt; data['greeting'].original_filename
=&gt; &quot;greeting.txt&quot;
&gt;&gt; data['greeting'].read
=&gt; &quot;Hello world!&quot;</pre></div></div>

<p>Now you can handle files using your XML API just like you would use file uploads in your HTML forms.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faster RESTful XML processing in Rails</title>
		<link>http://techblog.floorplanner.com/2010/01/03/faster-restful-xml-processing-in-rails/</link>
		<comments>http://techblog.floorplanner.com/2010/01/03/faster-restful-xml-processing-in-rails/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 19:29:09 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[libxml]]></category>
		<category><![CDATA[nokogiri]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[rexml]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=815</guid>
		<description><![CDATA[The Floorplanner API uses XML-formatted requests and responses, so our servers process a lot of XML. In Rails, most XML parsing is done using the Hash.from_xml method. This method allows for different backends, but the current backends are either slow or buggy. I decided to fix this situation myself.
I fixed bugs in the current libxml [...]]]></description>
			<content:encoded><![CDATA[<p>The Floorplanner API uses XML-formatted requests and responses, so our servers process a lot of XML. In Rails, most XML parsing is done using the <code>Hash.from_xml</code> method. This method allows for different backends, but the current backends are either slow or buggy. I decided to fix this situation myself.</p>
<p>I fixed bugs in <a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3641">the current libxml and nokogiri backends</a>, and I added some <a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3636">new SAX-based backends</a> for additional performance. <a href="http://github.com/rails/rails/commit/12f6fd0f2687f083bc23ad63fdc82c7e65cb8984">My</a> <a href="http://github.com/rails/rails/commit/37c51594b9610469173f3deee1ffdda4beb3e397">patches</a> <a href="http://github.com/rails/rails/commit/96a2b3905ce14df8f25b1646d3b110505bf8820b">are</a> <a href="http://github.com/rails/rails/commit/d7f9b9fd244228d3503d7d37ac2f07365d54df3c">already</a> <a href="http://github.com/rails/rails/commit/34b03cebf9c9f2ce2a53511a4b2160485e270f12">accepted</a> by the Rails team, so everybody will enjoy fast and bug-free XML parsing in Rails 2.3.6 and eventually in Rails 3!</p>
<h3> Performance </h3>
<p>I have benchmarked the new backends using an 1.8 MB XML file. The REXML, LibXML and Nokogiri backends currently ship with Rails, but are horribly slow or are buggy. The ++ variants are my improved versions of these backends, and the SAX variants are completely written from scratch using a SAX-based parser. </p>
<pre>
                  user     system      total        real
REXML        17.170000   0.060000  17.230000 ( 17.297263)
LibXML        2.100000   0.100000   2.200000 (  2.217380)
LibXML++      0.530000   0.000000   0.530000 (  0.531034)
LibXMLSAX     0.630000   0.010000   0.640000 (  0.632472)
Nokogiri      5.280000   0.020000   5.300000 (  5.322575)
Nokogiri++    1.840000   0.020000   1.860000 (  1.872055)
NokogiriSAX   0.770000   0.000000   0.770000 (  0.778777)
</pre>
<p>As you can see, LibXML++ is the fastest backend, but NokogiriSAX comes close if you want to stick to Nokogiri. </p>
<h3> No patience? </h3>
<p>Don&#8217;t want to wait for the next Rails version for this speed up? You don&#8217;t have to: just put <a href="http://github.com/rails/rails/tree/2-3-stable/activesupport/lib/active_support/xml_mini/">the appropriate backend file</a> in the <code>/lib/active_support/xml_mini/</code> folder of your Rails project, and set your backend accordingly in your environment:</p>
<pre>ActiveSupport::XmlMini.backend = 'NokogiriSAX'</pre>
<p>Happy coding in 2010!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/03/faster-restful-xml-processing-in-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating a REST API for a Flash application</title>
		<link>http://techblog.floorplanner.com/2009/02/25/creating-a-rest-api-for-a-flash-application/</link>
		<comments>http://techblog.floorplanner.com/2009/02/25/creating-a-rest-api-for-a-flash-application/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 16:49:47 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Flash+ActionScript]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[caveats]]></category>
		<category><![CDATA[DELETE]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[PUT]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=486</guid>
		<description><![CDATA[We have been working hard to implement an XML-based REST-style API for Floorplanner and some of our partners are using it already to access their users and plans. We now have started to use this API ourselves.
The Floorplanner Flash application communicates with our servers to load and save projects and designs. The backend for this [...]]]></description>
			<content:encoded><![CDATA[<p>We have been working hard to implement an XML-based REST-style API for Floorplanner and some of our partners are using it already to access their users and plans. We now have started to use this API ourselves.</p>
<p>The Floorplanner Flash application communicates with our servers to load and save projects and designs. The backend for this functionality used to be written in PHP. Eliminating this PHP application simplifies our server setup, eases development and reduces our maintenance burden. To rewrite this functionality in Rails, we decided to eat our own dogfood and use the REST API to load and save designs. Why reinvent the wheel?</p>
<p>However, while implementing the changes it the Flash application, we found that it did not work out of the box, because of some limitations in ActionScript. Note that we still use ActionScript 2, so some of these issues may not be relevant for ActionScript 3. In this post, we will detail what these issues are and how we overcame them.</p>
<h3> Using a separate format </h3>
<p>Our REST API uses the XML format supplied by Rails. Because we had to make some changes to make it work from ActionScript, we decided to add a distinct format that we could implement differently without altering the behavior of the default XML API. Adding a new format, called &#8220;flash&#8221; can be done in Rails by adding a MIME alias to your environment:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#6666ff; font-weight:bold;">Mime::Type</span>.<span style="color:#9900CC;">register_alias</span> <span style="color:#996600;">&quot;application/xml&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:flash</span></pre></div></div>

<p>Now we can send different responses based on this format:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># default API behavior</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">flash</span> <span style="color:#006600; font-weight:bold;">&#123;</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># do something different</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3> HTTP status codes </h3>
<p>REST APIs use HTTP status codes to return whether a call succeeded, and if not, why. Flash however uses the browser to perform HTTP requests. The browser only returns something to Flash if the request was successful. So, if an error code is used if a request failed together with some error messages, these error messages will not be delivered to Flash and cannot be displayed to the user. We resolved this by always sending the HTTP OK status.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">errors</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">422</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">flash</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> XML.<span style="color:#9900CC;">failure</span><span style="color:#006600; font-weight:bold;">&#40;</span>@project.<span style="color:#9900CC;">errors</span>, <span style="color:#006666;">422</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Our <code>XML.failure</code> method will return something like:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;failure</span> <span style="color: #000066;">status</span>=<span style="color: #ff0000;">&quot;422&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>A project should have a name!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/error<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/failure<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that other HTTP success statuses than 200 work in Safari and Firefox, but not in Internet Explorer. So, never return a 201 (<code>:created</code>), because Internet Explorer will not send the result to Flash!</p>
<h3> PUT and DELETE requests </h3>
<p>REST-style APIs use HTTP PUT requests to alter objects and DELETE requests to destroy objects. Most browsers do not support these request type. It is not supported by ActionScript either, because Flash uses the browser to send the request. </p>
<p>To overcome this problem, these types of requests can be simulated in Rails by sending a <code>_method</code> parameter along with a POST request. Unfortunately, this does not work when calling the REST API. The POST request body cannot be used to send additional variables, because it is used for the XML payload.</p>
<p>We solved this issue by creating additional routes for POST requests to the <code>update</code> and <code>destroy</code> actions of our resource controllers. Our <code>routes.rb</code> file now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">  map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:projects</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:post</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:post</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>These routes route to exactly the same methods as the default REST actions (<code>ProjectsController#update</code> and <code>ProjectsController#destroy</code>), so no additional code is needed. The following calls are now equivalent:</p>
<pre>
PUT    /projects/123.xml
POST   /projects/123/update.xml
POST   /projects/123/update.flash

DELETE /projects/123.xml
POST   /projects/123/destroy.xml
POST   /projects/123/destroy.flash
</pre>
<h3>The result </h3>
<p>It requires some stretching of the pure REST principles, but doing so is worth it: we can now reuse the code we use for our API to handle Flash application calls and we can eliminate the PHP backend.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/02/25/creating-a-rest-api-for-a-flash-application/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
