For a couple of days I have been working on a basic site that would allow someone to enter a twitter handle then would pull some data using the twitter REST API , do some “stuff” to that data and show you a pretty graph. When I say “stuff”, for instance I mean something like figure out the average tweets per day for the user. This is all information that is there, we just need a way to sort it to our liking.
My last post began with some issues I had getting my environment variables setup, well not really issues but more of a general service announcement. Anyway, what I am building is a basic sinatra app with basically three routes and some helper methods for making the calculations.
Note: I keep adding new things I want to graph or show visually, thus my gemfile is growing and I am starting to pull from multiple API’s. I’m really just building this to mess around, but it’s teaching me a ton which is great!
I am using a few gems to make this happen. see my gemfile below:
Sinatra, that’s a given. The twitter gem which gives so great methods for pulling data from the API. Chartkick is what I am using to display most of this data, you can do a lot of customization which is pretty awesome. Geocoder, this is for another route where you can input a search term, location and radius and a list of tweets will be returned. This feature is on the back burner while I work on the first part (figuring a users avg tweets per week day). That brings me to group date.
Now, I can get back to really the whole point of this post…I had an issue with groupdate. Could be operator error, I acatuly opened my first github issue regarding it here. Let me walk you through the problem.
Note: I truncated the tweet returns above because there are a couple hundred..
So, we have a variable “timeline” that is an array of tweets, each tweet contains information about that particular tweet. The CLIENT global variable is used in my app, I just cut and paste it (contains my api keys and other identifying info so i can use the api). The #user_timeline method is provided by the twitter gem which takes a twitter handle as the first argument along with some options.
Great, so now we have an array of tweets. For my chartkick gem to display data, especially when not using a database it wants that data in a hash or an array of arrays. I created the variable days_hash calling #group_by_day_of_week method provided by the groupdate gem. This returns a hash with keys corresponding to the days of the week (0 = sunday, 1 = mon…etc).
Next I call #merge on the days_hash, merging with itself and passing in the block to return counts of tweets for the different days. So, do you see the problem with the return value?
I guess I have never tweeted on a Sunday…Now I know I am not a huge twitterer (word?) but I am pretty sure I have tweeted on a sunday…Lets go back to the timeline array and see..
So indeed there is something going on here. I tried playing around with timezone etc, thinking that may be it, but eliminated that. Thus, I opened the issue on github. Since I was still waiting on a response and wanted to keep going on the project, I figured I could just make my own method to do it for me…This is what I came up with, I actually like it better because I can use the weekday abbreviation vs [0..6].
I should be able to refactor this, but for now it gets me working again. What I don’t like about it how for the key I have [tweet.created_at.strftime(“%a”)] three times.
Here is the result..
And finally, it in action rendered in view…
Obviously, this isn’t styled yet at all…I even show the return value from the tweet_day method just to make sure the chart corresponds.
That’s about it, next part I want to briefly discuss the twittercounter API and how I used HTTParty to work with requests.