In my last blog post, I talked a little bit about my background in psychology. I worked on a project that provided access to free trade school education for parents, and free early education to children in poverty. My job was to research and analyze data for this program to see if it would be beneficial to implement in other cities. I often found that if people are given the chance to create better circumstances for themselves, they will succeed in doing so. As I am writing this, we are almost one year into the covid-19 pandemic. Unfortunately our economy has declined, we have lost loved ones, as well as jobs, and homes. Basically, there are a lot of charitable organizations that would be grateful for your help right now, and a lot of people looking for their chance to get out of an unfortunate situation. For those reasons, I decided to make my first project a CLI app that helps you find local charities.
CLI stands for command line interface (or input), and it is the predecessor for more accessible interfaces like the GUI (graphical user interface). I used an API to access a database of IRS registered non-profits. I created three classes, API, Charity, and CLI. The CLI class is what the user sees. Once the user is welcomed and prompted to enter their zip code, their input is sent to the API class where it is inserted into the “getting_zipcode” method. It is specifically inserted into the API’s url which is assessed using the rest-client gem. That data is then parsed with the JSON gem, and put into an empty array. I also had to keep error handling in mind, so I added an if statement to return nil if an invalid zip code was entered.
One of the most difficult pieces of this project for me was attempting to get the data I needed out of a nested hash that was inside an array. After trying many different methods, I decided on parsing the database starting at the “data” array within the API. With that array, I utilized the “.each” method to create a new instance of the Charity class with each element of that array. Each element of that array was a hash enclosed with details of each charity, so I further iterated on that in the initialize method in the Charity class.
Instead of taking each attr accessor value and setting it equal to itself to make an instance of the Charity class, I used mass assignment to take the hash within the data array and iterated over each key/value pair in the hash. The name of the key in the hash becomes the setter method, which equals its’ associated value. The “.send” method is an abstract way of calling any method on an object. At the end, I added an if statement with “.respond_to?” to make sure that only the keys I wanted to include in my app were included. These steps allowed me to access the data within my hash with the least amount of code possible.
Overall, coding my CLI app was a challenging and rewarding experience. I can’t wait to start my next project!