- Read Tutorial
- Watch Guide Video
In this section of the course, we're going to talk about how we can use our lib directory.
One trend that I've noticed both in myself and also with the developers I know. Is that the more advanced that we become in working with Rails the more we tend to work inside of our lib directory. The reason for that is because the lib which is short for library is a great place to put isolated code components that can sit and live outside of the rest of our application. The lib is usually where I will start building modules that I want to eventually turn into their own ruby gems. As you saw from earlier on in this course the lib is also a great file to put overrides for when we want to have custom scaffolds.
In this section what I want to talk about is how we can use the lib to create our own custom modules. And I think we're going to create a fun one in this course. We're going to create a parser that is going to take some live Tech News. So it's going to take tech news from a different site. It's going to read in the API it's going to parse the data and then we're going to use that module to pass it into the rest of the Rails application so that it can be rendered on the site. This is a very common pattern that I follow when I'm communicating with outside API's. And if you want to build rails applications properly it's very important to isolate this type of behavior so it doesn't get too tightly coupled to the rest of the application.
By tightly coupled what I mean is. Say that I were to do something horrible, like put this API call inside of the view of our application. If I ever want to call that tech news say from a widget or a sidebar item or the footer in some other part of the application I would have to copy and paste the code from the page that I had it and paste it over into the second spot. That would be a very bad way of building the application.
Imagine that you have to do that five times then every time you made a change you'd have to go through and make the change all five times and that is a recipe for development disaster. Instead what I do is I focus all of my effort in one section and I try to make it as isolated as humanly possible so that it can be called by any other part of the app that needs it. And then when I need to make a change I can make a change just in that one location instead of having to make it in every other spot. So that is what we're going to cover in this section.