Tweet Tweet Tweet!!

Having wrapped up the Minesweeper game,  the next challenge came down the lines.  It was a multipart challenge that would have me learning several new things.  First was to create a Ruby on Rails application to which we would be using for the next project.  Having tinkered with Ruby,  I already had an older version on my machine so it was really just a matter of pulling the Git, and running the bundle to grab all the latest and greatest things needed from the Gem file.  This of course never goes %100 according to plan and took a bit a troubleshooting to require particular versions that would work correctly.  Once I got all the needed items and requirements added to the Gem file,  got the server up and running and was ready to start work.

With the server up and running,  my blank page at Localhost needed something.  That is when I got my mission,  to act like I’m working with Stanford and create a page where their tweets would be displayed.  After getting a lesson on the Twitter API and getting it hooked up to a test dataset,  it was time to begin work.

First up was to create the framework for which the tweets would get pulled into.  This was as easy as creating some Divs in which I could then post the results to.  From there,  using what I knew about the data,  I was able to start pulling it in and making it display on the page.  It was very nice to look at,  so I decided to invest a bit of time and effort cleaning it up with some CSS.

Now I had a list of Stanford Tweets, which was exciting,  but not very useful.  I was directed to List.js which allows for some great sorting functionality.  It took some time tweaking with it to figure out just how to get it working,  but once done, it was great at sorting the list as well as providing a dynamic search as well.

Things were going great,  when the next challenge came in:  Make some reports; such as which are the top posts being Re-Tweeted,  or which Locations get tweeted from the most.  The presented a new battle,  as I now needed to figure out how to manipulate the data in a different way.  I learned sorting of Arrays which wasn’t too bad but this data was an Array in an Object.  The difference being that Arrays are easy because you can sort by indexes,  but with Objects the data is put in as names and it is not as simple as saying “alphabetized this”.  After a session with the trainers,  they taught me how to dig into the Objects, and effectively pull the items into an Array so then I could do the sorting.  From there I needed to create some loops that would go through the data and get it sorted by what I needed,  as well as tallying up the counts (for how many Tweets came from a location).  Finally everything was looking great and I thought I was just about done.

Really,  is a product ever done?  It was time for a new lesson.  With my JavaScript code it was becoming rather large and unsightly.  This was largely due to the long functions need to put the data on to the HTML page.  So it was time to learn a new way of doing this,  which is with Templates.  With the templates in Ruby,  you build out the page (or section of a page) just like you would regular HTML.  The difference is that you can add JavaScript directly in by wrapping it in “<% %>”.  From this I was able to build each of those reports into their own little page,  and greatly clean up the JavaScript file as a lot of it was on the template.  The added bonus is that I can easily fix or change things with the layout or look,  without tinkering with the delicate functions I had created.  So I set out and converted each of the sections into their own templates.

Weeeeew!  That was nice chunk of work,  but are we finished?  Nope, it was time to learn something new. (Learning programming has no end because there are always great new things to figure out)  The lesson this time was about framework,  specifically putting things into an MVC framework.  MVC stands for Model-View-Controller and was such a shift that my mind hurt for a few days after learning (still hurting in fact as I’m still learning it!).  From my understanding at this point,  the goal of an MVC is really a form of organization.  It allows you to separate your data (Model),  your display (View),  and your page links (Controller) into different parts that can then be accessed easier.  Using a framework always adds a lot of functionality to things like data manipulation such as the sorting we were doing before.

We decided to use Backbone.js and after adding the needed entries into my Gem file and re-doing my bundle,  I was ready to begin.  One of the first things I had to wrap my brain around with GET and SET.  Normally (at least my normal at this point),  I would be creating variable of different results to manipulate.  Doing so was only creating a copy of the data,  so the real data never changed.  With GET and SET,  you are actually touching the data so you have to be a bit more careful in what you do.  The next challenging aspect to learn was separating the different elements and how to work with them.  Such as creating your Model and your Collection from the data and get it to initialize,  and then working with your View for the displaying it.  The real confusion came from wondering what actually should go in these vs outside.  Such as the sorting function,  do I do within the Collection,  within the View,  or outside of both?  With programming there isn’t a fully right or wrong answer,  it usually is situational.  Because I was having different views,  sorted different ways,  I decided to put each sort into each of the relevant views.  This allowed me to easily do it in the render area of the view which seemed to work good.  In our next session however,  they walked me through being able to do this in its own function within the Collection,  so that it can easily called by any view that may need it.

Okay,  so finally I went through and got everything converted over to the MVC and then did a great deal of clean-up on the code.  For today’s challenge I’m working on a Collatz Conjecture.  So far it looks like I got it,  but will update again after my meeting today.

Leave a Reply