<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lito White on Medium]]></title>
        <description><![CDATA[Stories by Lito White on Medium]]></description>
        <link>https://medium.com/@lito_white?source=rss-affb5fd490d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RI0mHuHuvDJX0QjlCnj7_w.jpeg</url>
            <title>Stories by Lito White on Medium</title>
            <link>https://medium.com/@lito_white?source=rss-affb5fd490d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 05:03:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lito_white/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Applied Abstraction and Encapsulation when working with APIs on Rails]]></title>
            <link>https://medium.com/@lito_white/applied-abstraction-and-encapsulation-when-working-with-apis-on-rails-dd5053c2898b?source=rss-affb5fd490d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd5053c2898b</guid>
            <category><![CDATA[abstraction]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[turing-school]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Lito White]]></dc:creator>
            <pubDate>Mon, 24 Aug 2020 02:55:32 GMT</pubDate>
            <atom:updated>2020-08-24T02:55:32.561Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Beginner’s Overview</h4><p>It’s one thing to read about the theory behind basic programming principles like Abstraction and Encapsulation; it’s quite another to understand how to implement these lessons on a practical level. This article will present a very high level summary of how to organize your code in Rails when interacting with APIs.</p><p>Here’s how to do it.</p><h4>First Attempt</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*RVEP-6fyIL1G4KCtbl-bfA.png" /><figcaption>What NOT to do</figcaption></figure><p>Even if you’re just starting off on Rails, you know this is bad. The controller is doing way too much. Skinny controller? Nope. The controller is sending an array of hashes straight from the API to the view. This data dump breaks <strong>encapsulation </strong>— the data isn’t contained in any way.</p><p>The view has to manipulate an array of hashes. The view shouldn’t be smart enough to do this — it’s whole job is to simply show you something. By doing too much, the view is breaking <strong>abstraction</strong>. This business logic belongs somewhere else.</p><p>We are going to need to restructure our code. Here are the goals:</p><ul><li>The controller shouldn’t interact with the API. That belongs in a different <strong>Service</strong> class.</li><li>The view shouldn’t deal with data from the API. Instead, the view should receive a specific collection of objects that it can easily handle. These objects are created in a different <strong>Model</strong> class.</li><li>To keep the controller as skinny as possible, the controlller will outsource the coordination of the API and the model data. We will have a dedicated <strong>PORO</strong> class that will call on both the <em>Service</em> class to get the desired data from the API and the <em>Model</em> class to turn it into objects we can easily use.</li></ul><h4>Refactored Code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*WEbqT2SH8dA2L-jZ10lW-Q.png" /><figcaption>Way better</figcaption></figure><p>Notice how everything has a single responsibility?</p><ul><li>The <strong>view</strong> shows you the object attributes. It doesn’t have to think too hard to do this.</li><li>The <strong>controller</strong> is acting like a true PM. Why do something yourself when you can delegate!</li><li>The <strong>SearchResult PORO</strong> coordinates the effort of retrieving the API data and transforming it into usable objects.</li><li>The <strong>API Service</strong> interacts with the API directly.</li><li>The <strong>Object Model</strong> creates objects out of the incoming API data.</li></ul><p>As a student, it seems counterintuitive that adding more and more classes is actually better. More classes means more complexity, right? Turns out, the answer is no! Having each class do one thing and one thing only makes for more robust, flexible code. Nothing is too smart for its own good, and the code can be easily modified without creating unexpected ripple effects through the whole project.</p><p>Note: the material presented here is based off a lecture presented at <a href="https://medium.com/u/119e2e296a36">Turing School</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd5053c2898b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>