Week 3 Round Up: Twitter Fitter—the tweet generator!12 Jan 2015
So there's still a lot more work I want to do on my week 3 app Twitter Fitter. But the general idea is there--it's a Markov generator. You type in a twitter handle, and it spits back a generated tweet (or tweet-like string) that is in the style of that user. (Try @DATABASE_HULK, @StephenAtHome, @BarackObama, or @kimkardashian) Part of the app/week project is that I want to learn something new each time. So what did I learn this week?
Well, this was my first app where I had to connect to another service (in this case it was the Twitter API through the Twitter gem) and also the first app where I wrote my own POROs (plain old Ruby objects) inside of Rails. In fact, this was barely a Rails app. It doesn't even have a model. It's mostly just a single page that connects to Twitter and then runs the MarkovChain object that I wrote for it. I also obviously learned a lot about Markov generators and how they work. (I knew about them generally but never thought about how to write one before)
I was able to write tests for my MarkovChain object. I'm pretty proud of this. But I haven't written tests for the actual controller code yet (which is currently very messy).
As always there are still many things I want to do to improve it:
- Needs major styling. Looks really ugly right now. I want it to look good!
- I want you to be able to retweet the generated tweets. Maybe it will retweet it from a custom account, maybe @TwitterFitter
- I want to make a gem out of the MarkovChain object. There are already similar gems out there (and my algorithm isn't all that unique), but I think it would be a learning experience to create my own gem.
- Tweak my generator code. Currently it's pretty simple, and the results are either pretty funny / nonsensical or else they're pretty boring (like almost word for word of an existing tweet rather than a mix-match of different tweets)
- writing the actual Markov generator, and making it perform okay as well as be open to modification in the future if I wanted to change the way it works later. For example, I thought it would be cool if I could pass it a lambda so that it picks the next word based on running that lambda code. The default is just random, but maybe someone would want to tweak it themselves without having to go into my code.
- figuring out how to call private / public methods within my class, and whether or not to even use private methods. Here's a blog post I wrote about this topic.
- tweaking the generator. When I use a prefix length of 2, I get back text that's very similar to the original text, so much so that it's kind of boring. If I set it to 1, I get back things that are sometimes very nonsensical. I decided on 1. But it would be nice to come up with a different algorithm so that it's between totally nonsensical and completely boring.
- figuring out how to include my ruby object in the lib directory and have rails auto include it. I was not naming my file correctly, so it didn't include it when autoloading! Took me some extra googling to figure out why.
- writing test code. I'm not sure I did it right, but testing code that's supposed to be random was also kind of tricky. I had to seed the generator with values so that it would be predictable/testable.
- using the Twitter gem. I had to figure out how to give it my API keys and secrets without exposing it through github. I wrote a blog post about this here.
I've mentioned taking the incredibly helpful edX.org SaaS class (Software as a Service) before. Well, part 2 of the class has just started, so instead of continuing with my own apps, I will be doing the SaaS homework apps every week for the next few weeks. It's only a few weeks, and there's not a homework for every week, so it won't be taking over this project completely, but its presence will definitely be felt. I will still be blogging about my discoveries, though! Hope you'll keep reading.