<?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>Forem: august-jk</title>
    <description>The latest articles on Forem by august-jk (@augustjk).</description>
    <link>https://forem.com/augustjk</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%2F879226%2F8d11e213-1a8e-4732-bc2b-1d7372acaf92.png</url>
      <title>Forem: august-jk</title>
      <link>https://forem.com/augustjk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/augustjk"/>
    <language>en</language>
    <item>
      <title>Showing Relational Tables in Your JSON Response</title>
      <dc:creator>august-jk</dc:creator>
      <pubDate>Tue, 03 Jan 2023 21:25:36 +0000</pubDate>
      <link>https://forem.com/augustjk/showing-relational-tables-in-your-json-response-3d1f</link>
      <guid>https://forem.com/augustjk/showing-relational-tables-in-your-json-response-3d1f</guid>
      <description>&lt;p&gt;Consider this scenario:&lt;/p&gt;

&lt;p&gt;You're a ruby developer and you've just set up a few models with a one-to-many relationship, for the purpose of this blog we'll say the models are Author and Book.&lt;/p&gt;

&lt;p&gt;An Author &lt;code&gt;has_many :books&lt;/code&gt; and a Book &lt;code&gt;belongs_to :author&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is common, as in programming we tend to rely on real-world models for our code. A pet can belong to an owner and an owner can have many pets, and a game can have many reviews, but a review can only belong to that one game.&lt;/p&gt;

&lt;p&gt;Let's also say, you've developed a React frontend where you need to show these relationships, and your backend is using Ruby, ActiveRecord, and Sinatra. &lt;/p&gt;

&lt;p&gt;Well, it sounds like you need to use "include:" in your Sinatra ApplicationController!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's get started!
&lt;/h2&gt;

&lt;p&gt;Locate your Application Controller; this is the part of our Ruby backend that will handle what endpoints we will fetch from inside our React frontend.&lt;/p&gt;

&lt;p&gt;Using our Author and Book models, here is what this class might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; Sinatra::Base

  get '/authors' do
    Author.all.to_json
  end

end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Making sure that we inherit methods from Sinatra::Base will allow us to use various options inside our Controller file.&lt;/p&gt;

&lt;p&gt;Here's an example of what JSON data we might receive if we visit &lt;a href="http://localhost:9292/authors" rel="noopener noreferrer"&gt;http://localhost:9292/authors&lt;/a&gt; (once we have started the server, that is):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "id": 1,
    "name": "J. R. R. Tolkien",
    "image_url": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQO-WAOvTtPuzb5mS0h2GIZvwSO-HLofOY7VZRZBfcVW8wEJQUa"
  }
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is nice, but what if we want to see all of the books this author has written?&lt;br&gt;
We would then need to create a new route inside our Application Controller, see a list of all the books in our database, then compare the author_id of each book to our desired author's id...&lt;br&gt;
I don't know about you, but that sounds like a lot of extra and unnecessary work!&lt;/p&gt;

&lt;p&gt;There's one little line of code we can add to our author's route that will make things a little easier, can you spot the difference?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; Sinatra::Base

  get '/authors' do
    Author.all.to_json(include: :books)
  end

end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hey! This new route says &lt;code&gt;(include: :books)&lt;/code&gt;, what's that about?&lt;/p&gt;

&lt;p&gt;Well, that little line of code allows the books associated with the authors to be included in the JSON data without a whole lot of extra work!&lt;/p&gt;

&lt;p&gt;Here's what our JSON data will look like now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "id": 1,
    "name": "J. R. R. Tolkien",
    "image_url": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQO-WAOvTtPuzb5mS0h2GIZvwSO-HLofOY7VZRZBfcVW8wEJQUa",
    "books": [...
    ]
  }
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can further customize the data you receive by changing up the syntax a bit (depending on how you set up your classes and what you'd like to return), but there you have it! &lt;br&gt;
A great way to receive all of the data you need for your frontend without having to rely on using a GET request to fetch data more than once.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How to create a RESTful API with JSON Server</title>
      <dc:creator>august-jk</dc:creator>
      <pubDate>Sat, 01 Oct 2022 00:08:29 +0000</pubDate>
      <link>https://forem.com/augustjk/how-to-create-a-restful-api-with-json-server-m98</link>
      <guid>https://forem.com/augustjk/how-to-create-a-restful-api-with-json-server-m98</guid>
      <description>&lt;h2&gt;
  
  
  What is an API?
&lt;/h2&gt;

&lt;p&gt;An API, or Application Programming Interface, is a set of functions and procedures allowing the creation of applications that access the features or data of an operating system, application, or other service.&lt;br&gt;
In other words, an API is an interface that allows applications to communicate with each other and exchange data.&lt;br&gt;
Developers will use APIs to send and retrieve data from servers to use on their applications.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a RESTful API?
&lt;/h2&gt;

&lt;p&gt;REST stands for Representational State Transfer and is a type of API that conforms to the constraints of REST architectural style.&lt;br&gt;
In order for an API to be RESTful, it must conform to specific criteria such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A client-server architecture made up of clients, servers, and resources, with requests managed through HTTP&lt;/li&gt;
&lt;li&gt;Stateless client-server communication, meaning no client information is stored between get requests and each request is separate and unconnected&lt;/li&gt;
&lt;li&gt;Cacheable data that streamlines client-server interactions&lt;/li&gt;
&lt;li&gt;A uniform interface between components so that information is transferred in a standard form&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RESTful APIs are faster and more lightweight than other, older types of APIs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why set up a RESTful API using a JSON Server?
&lt;/h2&gt;

&lt;p&gt;Sometimes front-end developers will need to simulate a back-end REST service to deliver data in JSON format to their front-end application in order to make sure everything is working properly.&lt;/p&gt;

&lt;p&gt;Of course, you could set up a full back-end server, but that would take some time to complete. &lt;br&gt;
Creating a RESTful API using a JSON Server is simpler and faster.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Continue reading to learn how to set up a RESTful API using JSON Server!&lt;/p&gt;
&lt;h3&gt;
  
  
  Installing JSON Server
&lt;/h3&gt;

&lt;p&gt;JSON Server can be installed as an NPM package.&lt;/p&gt;

&lt;p&gt;Type into your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install -g json-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding the -g we are installing it globally on your system.&lt;/p&gt;

&lt;h3&gt;
  
  
  JSON File
&lt;/h3&gt;

&lt;p&gt;Now we'll create a new file called db.json&lt;br&gt;
This file will hold all of your data!&lt;/p&gt;

&lt;p&gt;Here is an example of how this file should look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "cats": [
    {
      "id": 1,
      "name": "Fluffy",
      "color": "white",
      "favorite_toy": "fuzzy ball"
    },
    {
      "id": 2,
      "name": "Oreo",
      "color": "black and white",
      "favorite_toy": "toy mouse"
    },
    {
      "id": 3,
      "name": "Luna",
      "color": "brown and grey tabby",
      "favorite_toy": "laser pointer"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see we have a JSON file has an object labelled "cats". Inside the object is an array of 3 different cat objects each containing their name, color, and favorite toy.&lt;br&gt;
This is how the data inside of a JSON Server looks.&lt;/p&gt;
&lt;h3&gt;
  
  
  Running the Server
&lt;/h3&gt;

&lt;p&gt;Type into your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ json-server --watch db.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your server is running and you can begin to make any FETCH requests to &lt;a href="http://localhost:3000/cats"&gt;http://localhost:3000/cats&lt;/a&gt;&lt;br&gt;
If you make POST, PUT, PATCH or DELETE requests, changes will be automatically saved to db.json.&lt;br&gt;
A POST, PUT or PATCH request should include a             &lt;code&gt;Content-Type: application/json&lt;/code&gt; header to use the JSON in the request body. Otherwise it will result in no changes being made to the data.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Keeping your code DRY</title>
      <dc:creator>august-jk</dc:creator>
      <pubDate>Wed, 22 Jun 2022 02:24:40 +0000</pubDate>
      <link>https://forem.com/augustjk/keeping-your-code-dry-f49</link>
      <guid>https://forem.com/augustjk/keeping-your-code-dry-f49</guid>
      <description>&lt;h2&gt;
  
  
  Hello World!
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Welcome to my first ever blog post!
&lt;/h3&gt;

&lt;p&gt;I thought I would start with something that took me some time to get used to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keeping your code &lt;strong&gt;DRY&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DRY stands for Don't Repeat Yourself, which I have a hard time with in my day-to-day interactions, so you can imagine how writing DRY code could be quite foreign to me!&lt;/p&gt;

&lt;p&gt;How do you write DRY code?&lt;/p&gt;

&lt;p&gt;Well, let's first look at some WET (Write Everything Twice) code.&lt;/p&gt;

&lt;p&gt;Let's say you're iterating over an array:&lt;br&gt;
 &lt;code&gt;let array = [1, 3, 6, 9]&lt;/code&gt;&lt;br&gt;
We want to increase each number in the array by 1, so our result would be:&lt;br&gt;
&lt;code&gt;[2, 4, 7, 10]&lt;/code&gt;&lt;br&gt;
Here's what that would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function increaseByOne(array) {
  for(let i = 0; i &amp;lt; array.length; i ++) {
    array[i] += 1
    }
  return array;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks like a nice snippet of code, right?&lt;/p&gt;

&lt;p&gt;And it definitely gives us the output we're looking for!&lt;/p&gt;

&lt;p&gt;Well, if you &lt;em&gt;really&lt;/em&gt; look at it, a few things could be improved!&lt;br&gt;
First of all, we can only use this to add 1 to each number in the array.&lt;br&gt;
What if we wanted to subtract 1? Or multiply by 2?&lt;br&gt;
We would have to write an entirely new function and it would definitely be WET:&lt;/p&gt;

&lt;p&gt;Let's take a look at how we would write a function to multiply by 2 in the same way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiplyByTwo(array) {
  for(let i = 0; i &amp;lt; array.length; i ++) {
    array[i] *= 2
    }
  return array;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's almost identical and yet we're still repeating ourselves!&lt;/p&gt;

&lt;p&gt;How can we make this more straightforward and easier to read as well as making the code itself a little more diverse?&lt;/p&gt;

&lt;p&gt;One of my favorite built in JavaScript methods is .map()&lt;/p&gt;

&lt;p&gt;.map() tells your code to iterate over an array, then it takes a callback function to do whatever it is you need to do with that array! &lt;/p&gt;

&lt;p&gt;And the best part is, it's non-destructive so we can avoid any bugs.&lt;/p&gt;

&lt;p&gt;Let's rewrite that first function and use .map() instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function increaseByOne(x) {
return x + 1;
}
array.map(plusOne)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wow! It's that easy?&lt;/p&gt;

&lt;p&gt;It definitely is, and doesn't that look a whole lot cleaner too?&lt;/p&gt;

&lt;p&gt;Now let's replicate the second function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiplyByTwo(x) {
return x * 2;
}
array.map(multiplyByTwo)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That looks great!&lt;/p&gt;

&lt;p&gt;.map() has to be one of my favorite built in JavaScript methods, and now you know why!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
