My Phase 1 FlatIron Project
Tags: beginners, ruby
Published: 2021-02-26 17:33:57

Where do I even start? This is my fourth week at FlatIron School and we were tasked with creating a CLI project from scratch using an API. It completely intimidated me at first, I had no idea where to even start. My cohort leads gave the advice that even if you don't know where to start, just start somewhere. I know that statement sounds vague, but hey, I listened to that and figured the first thing I would need was the API.

I searched and searched the internet for hours for what I thought a good API would look like. This step took me way longer than I would have liked and I must admit, I got a little bit hung up over it. At last though, I found an API that I liked and would be perfect for this kind of project. It's an API that lists dog breeds and then returns information on each bread. Perfect, it's right up my alley since I'm a dog lover of course.

With this API I wanted to be able to create a project where the user gets to scroll through a list of breeds and then picks one that they would like to know more about. Simple enough right? Well..... let me just say I struggled! I felt like it was the first time on my own where there was no lab tests, just me and all the information I learned in the previous three weeks. 

With that being said it was time to actually start building the project. I would be lying if I said I was able to just get right in there and start typing out code. It took watching videos upon videos to understand the basics that go into building a CLI. Thankfully, my cohort leads are amazing and also provided us with more than enough information to get my project up and going.

After hours of watching YouTube videos it was time to jump in. There were now things that I had to tackle in order to get a final product:
1. What files to create
2. How to push all of my work to GitHub
3. How to get my API data to work in my code

## Knowing What Files to Create
Knowing where to start and what files to even create was half the battle. From the videos I watched, I sort of knew what needed to be created, but I didn't fully understand why/how they interacted with one another. I started with what I knew, I knew I needed a file for the API. This was an obvious to me, because well yeah if we're going to be planning on building this project with an API we need a file that has code to actually pull the data out of it.

I also created a CLI file. I knew I had to create this because, again, the videos I watched, but I wasn't sure exactly what it did. After researching I came to terms that the CLI file is where I am going to be putting my code that pulls the information and then displays it to the user in an easily understandable manner. 

I knew I had to create a file that somehow connected all of my files together. For this I created an environment file. What this environment file does is, it requires all of my other files into it so that way when I code in, let's say the API file, it was also register in the CLI file for example. If there was no environment folder my code would not know how to interact with one another, therefore making my program not feasible. 

## How to Push Work to GitHub
My next battle was learning how to save and push all of my code that I was working on to GitHub. The way GitHub works is actually pretty cool, because say if unexpectedly my computer crashed in the middle of building out my project, you would think all of my progress would be lost right? Well, think again, with GitHub it allows you to push your code to your account while you are working on it. This way you can avoid the disaster of losing everything that you were working on. 

How do you do this though? That's what I had to find out. I'm pretty lucky because one of my cohort leads did a whole bonus lecture all about git. After rewatching that lecture and Googling a couple of things, I found that it was actually quite easy. There are a few basic commands that you can enter in your terminal as you are working on your project, or whatever it may be that you are coding. ```
git add .

git commit -m "<descriptive message>"

git push
```
These three commands came very much in handy. What "git add ." does is that it saves all the new changes that you made to your code and prepares it for the next command that you will need to use. The next command is "git commit -m "<descriptive message>"", this allows you write a message about the new/improvised code that you just created. Make sure to make this message as descriptive as you can, because say in the future you want to come back to your project and build upon it more, you'll be able to fully understand each step of code that you previously created. Lastly, "git push" actually pushes all those changes up to GitHub allowing you to now see all you code that you've created, up until this point, in your repository. 

## How to get my Code to Work
The heading says it all, how was I even supposed to get my API data to properly interact with my code? Well this is definitely what I struggled the most with. It's not like I wasn't able to get everything to work, but there were definitely times where I thought that I was never going to end up with a fully functioning project.

How was I supposed to get all the code from my different files setup the right work for it to work? Well I had to take a step back and really examine what I was doing and what my code said. I tried to make sense of everything in my head and think if this does this, then that should do this. With that being said, I was still running into the problem with initializing my DogBreed class. What I needed the initialize method to do here was take the information from the API that I was using and make it work in a way where it can nicely display it to the user.

Initially I was trying to set up the initialize method with an attr_accessor:
```
attr_accessor :name, :weight, :height, :bred_for, :life_span, :breed_group, :temperament
```  
Coding my method in this way would mean that my initialize method would end up looking a little something like this:
```
@@all = []
attr_accessor :name, :weight, :height, :bred_for, :life_span, :breed_group, :temperament

def initialize
@name = name
@weight = weight
@height = height
@bred_for = bred_for
@life_span = life_span
@breed_group = breed_group
@temperament = temperament
end
@@all << self
end
```

I mean it would get the job done, but then I would run into hurdles of other problems I want to implement this information into my CLI class. One more watch of the instructor led videos and the lightbulb clicked in my head; metaprogramming!

Metaprogramming, is essentially code that writes other code. It's a pretty cool concept and it helped me a lot on this project. With metaprogramming it can pull key/value pairs from my data and make it work more effectively in my code. Key/value pairs is a simply concept, for example, a key would be "name" and the value of it would be "John". So using this logic of metaprogramming I could pull those specific keys or pieces of information from my API and display the correct value corresponding with each dog breed.

With that being said my initialize method would now look like:
```
@@all = []
 def initialize(breed_hash)
        breed_hash.each do |key, value|
            self.class.attr_accessor(key)
            self.send("#{key}=", value)
        end
        @@all << self
    end
```
I didn't have to explicitly code out every piece of information that I wanted from the API in my initialize method. It's a lot more concise and gets my code to function exactly how I want it to.

So, basically this week was quite an experience. It took long days and a lot of coffee to end up with a final result. I'm proud of myself for what I was able to do. I went from not knowing a single thing about coding to three weeks later creating a whole CLI application on my own. I know building the project is only half the battle, but I hope my technical review goes just as well and feels just as rewarding as creating this project.

You can check out my project repo if you would like at:
(https://github.com/jessicaajosephh/dog-breeds)