Brandon's Coding Journal


Based On Probably Something Similar To A True Story

Sinatra Project - Writers Block

So, it’s finally time for another project, this time working with Sinatra & Activerecord. Mostly focusing on databases, tables, and relationships the basis for my project came to me damn near instantly. No wasting time scouring the web for inspiration this time! Seeing as I have a fairly significant history creating and developing stories independently, (never cared for outside validation/influence/help, despite being confident enough to have multiple stories published if me and my partner gave a damn too) I knew instantly that I wanted an app to help build and keep track of there stories, characters, and events as they develop them. I decided to name this app Writers Block…I have absolutely no idea if that name is already copyrighted for something similar already, but damn did I think it was cool!

**Day 1:** Pretty basic stuff here, snagged the corneal gem to help me get my skeleton file structure and proceeded to build my tables & models. I set it up with 4 different models, although for the purpose of this project I only need to get my user & story classes functional. I did end up spending more time a the conceptualizing than I had originally thought, so I didn't get a lot done overall. Also I started my project on break week, so I took the extra time as a means to set smaller daily goals to avoid fatigue.

**Day 2:** Designed a couple of simplistic views to test against while I set up my controllers. Built the application (really just added a couple of helpers and enabled sessions) and the user controller. At this point in the project I had so many issues that simultaneously messed eachother up I decided to turn off sessions for the time being. This led to a number of issues come **Day 4** but I couldn't make real progress and something had to give. The important thing was I got my signup and login pages sorted out and could proceed to my story controller the next day.

**Day 3:** Story controller time, the heart of the CRUD part of the project. Once again, as I initially based it off my 'Fwitter' lab, I would run into several issues throughout this project. But for today, building my '/stories' landing page after logging in and creating a new story (title, genre, synopsis) was my goal. I wouldn't say I had any real technical issues, it's just my app was still in its fledgling phases and I had to build and tweak several minimally functioning routes/views/methods to make sure I was on the right track, the prelude to my big issues really, and by this time I could tell that the massive amounts of complex logic that 'Fwitter' had required would be a burden later and wouldn't be a logical long term set up for what I was trying to accomplish. But the functionality was slowly coming together.

**Day 4:** Official start of project week, should be done in 2 more days right? Nope. This is where the issues really started, as I went from a macro pov getting everything running to a micro pov in getting everythng presentable. Let's start with those damnable sessions. Disabling sessions didn't stop me from viewing the stories page, it instead just loaded the view for '/stories' on my '/login' page, causing me to have to resubmit every time i refreshed as if I was logging in. After playing with erb/redirect commands for 2 hours I finally discovered the issue was I couldnt progress much further without enabling sessions. We had a cohort wide issue where we had to enable session **at the top** of our ```configure``` method in our application folder, which was a really weird thing and seemingly the primary reason we had issues getting our sessions to set appropriately. Also I had a few rogue ```find``` methods that needed to be changed to ```find_by``` or ```find_by_id``` to help differntiate each User from one another. 

**Day 5:**  Ok so now that the sessions were working again, today was edit day! Once again, my 'Fwitter' based editing feature was overrun with logic that I had to cut down on to make it run more straightforward. Even moreso, I had to develop a 'post' route for my stories/:id which wasnt neccessary in the previous project, but Sinatra wouldnt let me advance wuthout it. Basically I had to learn to rewrite my neccessary methods in a totally differnet manner to get my program to function. And by the end of the day I had a funtioning edit feature with a dozen or so cluttered stories that I had nio way to delete.

**Day 6-7:** Ok, pressure's on, project week halfway through, but all I felt I needed was a delete feature to finish! Well, turns out my delete issue was tied into a deeper issue that would basically take me the remaining time to correct. After on;y being able to delete params and not remove from database, a few pry's showed me that there was A LOT of inconsistency with me ```.create``` method. I had changed it several times and my early stories werent saving anything, then stories weren't saving user_id, then I was good on everything, but modified it again somewhere and lost my user_id again (perhaps when I changed to my laptop and lost some progress for not pushing to github before switching machines). Needless to say how pain in the ass it was to try and save, edit delete with ```nil``` as the user_id, but the bigger problem was in my views. Because no user was assigned a user_id, I couldn't edit or delete several stories, and they were all available to view by any user, bust also disrupted my ability to set logical barriers to prevent user's from being able to delete/edit other user's projects (since at this point permissions haven;t been a subject for us). There were no fancy fixes here, the big issue was learning how to apply my ```current_user``` helper method and meticulously going through a dozen or so pages of code, making sure my tables were correct, relationships, grammatical/syntax errors etc. Then after that it was a simple matter of adding extremely minor logic to my views to block user's from viewing stories that didn't match the ```current_user.id``` with the ```@story.user_id```. But at 2am in the morning, after going thorugh all that I can proudly say it was all worth it. Then I remembered I had to write this damnable blog and recount all my work to the best of my ability.

My CLI Project Journey

So, after month’s of labs in the free course, pre-work, and the first 3 weeks of the full time course I’m finally here on my first independent project. For this project I decided to keep it as simple as possible. Rather than get some grand idea of building my own fantasy football website in a week with my fairly limited experience, I instead chose to focus on scraping some data from one of my favorite forms of fantasy football: Madden Ultimate Team!! And if you are a fellow partaker in any variation - whether console or the Madden Mobile app - you undoubtedly know muthead.com is the go to source for any information and data you could possibly want, so that was the obvious place to scrape my data from.

Now, I knew the joke would be on me all week because project week this time around just so happened to start on non other than April Fool’s Day. An ominous and telling sign indeed as the first day of project week I managed to accomplish absolutely nothing on my project. Instead I spent the whole day setting up and learning how to use my local environment until the last 10 minutes, when for the sake of sanity I wrote the basic outline of a Scraper class…before spending the next 30 minutes ‘learning’ that pushing my work to Github is now a fun 3 step process that Learn IDE would do for me in one simple command. But no worries because I would make it all up the next day right? Riigghhht.

Day 2 of the saga was wildly unproductive in real time, as I had all day to work on my project, and succumbed to several delays and changes in project ideas and scope (ok I DID have a grand idea to build my own fantasy football website). I had difficulties scraping data from the first two websites i tried for real football stats for 3 major reasons: 1. General incompetence 2. Site protections and 3. The sheer amount of data available made it harder for a noob scraper like me to parse through everything simply. I scrapped several project ideas, and subsequently several pages of code each time, for the sake of trying to simplify what I was looking to accomplish because I was working on a time limit. By the end of the day, I basically had enough code to scrape the team name ‘Tampa Bay Buccaneers’ from the site….a major accomplishment! But ultimately not so productive. And that’s when I turned my attention to muthead.com, which has been much more noob friendly with each page containig more finite amounts of data and data being slightly less dynamic than the first sites I tried to scrape from. Last thing I did at the end of the night was come up with a new, simpler project and planned it all out from start to finish: I was going to scrape the prices page and bring the user data based on who have been the biggest gainers and losers in the madden auction house. Brilliant! Having a more clear and simple project in mind I got a decent amount of sleep for the busy day ahead.*

I speak of day 2 negativley, but in hindsight day 2 was easily the most vital day of this project as it pushed me in so many different directions and helped to expand my knowledge by pushing me deeper into things I didn’t know. It has proven to be a neccessary evil.

Day 3: With my new project stil clear in my mind (not to mention the auction house being one of the best parts of MUT) I set out with renewed vigor! I build a functioning yet basic scraper model on the https://www.muthead.com/prices/xbox-one page and what do I get? 503 temporarily unavailable. Ugh. Would I have to change my project again? Turns out, yes, but luckily it was only an error specific to that page on the site, and the scraper worked on the players page just fine. So on the third day I had my project - choose a card to see it’s attributes! So let’s get working! I decided my Scraper class would parse the data on the basics of a card: Name, Position, Ovr (short for overall), and Price. Stats would also be as important, but having 30+ stats in a basic application with this time limit seemed a bit unwise so I didnt include them. With a little bit of digging and research however I did manage to figure out a way to link to the specific card’s profile page where the stats are viewable for users - although I havent yet implemented this yet as of this writing and am still thinking I may be better served expanding it later on rather than for project presentation.

 ```
 def self.scrape_muthead
  cards = []