<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ariel Fogel</title>
    <description>The latest articles on DEV Community by Ariel Fogel (@afogel).</description>
    <link>https://dev.to/afogel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1008804%2Fe39ffa10-fcae-4e9b-af38-b305180d6cdc.jpeg</url>
      <title>DEV Community: Ariel Fogel</title>
      <link>https://dev.to/afogel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/afogel"/>
    <language>en</language>
    <item>
      <title>RESTful routes are still quite powerful -- or how to RESTfully download</title>
      <dc:creator>Ariel Fogel</dc:creator>
      <pubDate>Sun, 15 Jan 2023 21:15:24 +0000</pubDate>
      <link>https://dev.to/afogel/restful-routes-are-still-quite-powerful-1873</link>
      <guid>https://dev.to/afogel/restful-routes-are-still-quite-powerful-1873</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Even though it may seem obvious, RESTful API design can still be an incredibly versatile and flexible architecture. It merits remembering that controllers conforming to this design are capable of responding with multiple types of representations for a given resource request, allowing us to maintain parsimonious mental models for organizing business logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I RESTfully download?
&lt;/h3&gt;

&lt;p&gt;I was recently chatting with my friend Christian about Ruby on Rails and how I've been enjoying its maturity and stability. I mentioned that one aspect I've been particularly enjoying is using REST as a guiding priniciple to write my APIs.&lt;/p&gt;

&lt;p&gt;Christian mentioned to me that it seems like a lot of JS frameworks are moving away from writing RESTful APIs, replacing them with other options like graphQL or gRPC. One explanation for reaching for these alternatives may be that some requests, like &lt;code&gt;sign_in&lt;/code&gt;, don't cleanly map to a given CRUD action for a resource. I hesitantly agreed with Christian, though seeing a &lt;code&gt;sign_in&lt;/code&gt; action modeled as a new &lt;code&gt;Session&lt;/code&gt; conceptually has made sense to me ever since I first encountered that pattern.&lt;/p&gt;

&lt;p&gt;Still, with Christian's words echoing through my head, I sat down today to write a bit of code and was &lt;em&gt;almost&lt;/em&gt; stumped while trying to design a RESTful Rails endpoint for downloading a resource. &lt;/p&gt;

&lt;p&gt;In vanilla Rails, I've gotten used to thinking about our controllers being responsible for only serving either the resource's HTML (i.e., a partial) or a JSON, or if you've hotwired your app, perhaps a Turbo Stream. So...what happens if my controllers are already set up to serve partials but I also want to download a resource as, perhaps, a YAML file?&lt;/p&gt;

&lt;h3&gt;
  
  
  MIME-types to the rescue
&lt;/h3&gt;

&lt;p&gt;As I mentioned above, Rails controllers let us respond to different MIME-types like &lt;code&gt;html&lt;/code&gt;, &lt;code&gt;json&lt;/code&gt;, or &lt;code&gt;turbo_stream&lt;/code&gt;s. I realized that the act of downloading a course is just like requesting that resource, only this time in a different format than one of the existing MIME-types that I'm used to (e.g. &lt;code&gt;html&lt;/code&gt; or &lt;code&gt;json&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;What this means is that rather than needing to define a custom action and route, I should be able to send a GET request to an existing controller with a different &lt;code&gt;content-type&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since I want to download a YAML file, I'll need to set the &lt;code&gt;content-type&lt;/code&gt; to &lt;code&gt;text/yaml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To get this to work, I'll need to do a few things --&lt;br&gt;
First, the MIME-type needs to be registered with Rails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/initializers/mime_types.rb&lt;/span&gt;

&lt;span class="no"&gt;Mime&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt; &lt;span class="s2"&gt;"text/yaml"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:yaml&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my case, I was requesting a &lt;code&gt;Course&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, in my &lt;code&gt;courses/show.html.erb&lt;/code&gt; page, I could simply add a new &lt;code&gt;link_to&lt;/code&gt; url helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/views/courses/show.html.erb&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= link_to t("download"), course_path(@course, format: :yaml) %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, in my controller, I'm able to leverage the Rails &lt;code&gt;ActionController#send_data&lt;/code&gt;&lt;sup id="fnref1"&gt;1&lt;/sup&gt; method to efficiently stream this request from my &lt;code&gt;CourseController#show&lt;/code&gt; action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/controllers/courses_controller.rb&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CoursesController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="n"&gt;before_action&lt;/span&gt; &lt;span class="ss"&gt;:set_course&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;only: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:edit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:update&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:destroy&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
    &lt;span class="n"&gt;respond_to&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;html&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;yaml&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;send_data&lt;/span&gt; &lt;span class="vi"&gt;@course&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_yaml_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"text/yaml"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;disposition: &lt;/span&gt;&lt;span class="s2"&gt;"attachment"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  So what?
&lt;/h3&gt;

&lt;p&gt;I know this blog post isn't an earth-shattering revelation. Frankly, this approach feels quite obvious to me in retrospect. I imagine that graphQL, gRPC, and other approaches may still have significant benefits that perhaps I'm not fully appreciating. That said, remembering the purpose behind RESTful API design and trying to return to first principles gave me a renewed appreciation for how powerful an abstraction it remains to this day. &lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;I chose to dynamically generate the yaml file by defining an instance method on my &lt;code&gt;Course&lt;/code&gt; model. However, if the resource you are trying to represent already exists as a file, you can use the &lt;a href="https://api.rubyonrails.org/classes/ActionController/DataStreaming.html" rel="noopener noreferrer"&gt;&lt;code&gt;ActionController#send_file&lt;/code&gt; method&lt;/a&gt; to send it instead. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
