roovo:web » tagged with: tech

OAuth conversion for unattended twitter bots

I have a couple of ruby scripts which scrape information off websites to post to their own twitter accounts: felixstowe port and virgin movies. These were both set up quite a while ago using Twitter4r and basic authentication and so were doomed to stop working come the end of the month if I didn’t update them.

As today is the start of the basic-auth turn off process I though I’d better convert them over to OAth. I already had a fair understanding of how OAth worked, but I found it pretty tricky to find the information I needed on how to get these unattended scripts set up to work using it.

The first thing I did was to switch over to John Nunemaker’s Twitter gem as I couldn’t work out if Twitter4r supported OAuth or not and Johns obviously did and has a really helpful page on how to set it up – here’s a copy of the snippet you need.

oauth = Twitter::OAuth.new('consumer token', 'consumer secret')
oauth.authorize_from_access('access token', 'access secret')

client = Twitter::Base.new(oauth)
client.friends_timeline.each  { |tweet| puts tweet.inspect }
client.user_timeline.each     { |tweet| puts tweet.inspect }
client.replies.each           { |tweet| puts tweet.inspect }
client.update('Heeeyyyyoooo from Twitter Gem!')

Then all you need is the consumer and access keys and secrets.

To generate these you need to add an application to your twitter account (go to http://dev.twitter.com/apps after logging in to your account). Fill out the info to register your application (i.e. your script). The important items are the Application Name as this will appear as the source of the tweets on your twitter stream, and the Application Type which you need to set to Client (as you can’t use callback urls in an unattended script).

Once your application details have been saved you can get the details you need: the consumer key (or token) and secret are on the application details page and the access token and secret are available from the My Access Token link (oauth_token and oauth_token_secret).

Fill them in the code above and you should be good to go.


tags: tech, ruby, twitter, oauth | comments: view

The End of Dependency Hell?

Saw an interesting commit message in the merb repo on github today.

The part that caught my attention was:

Adds support for Merb::Config[:ignore_system_gems], which if set to true will not fallback to system gems. This enables you to be sure your merb app has all required gems bundled, and not accidently using system gems.

This sounds like it has the potential to keep all those nasty already loaded errors at bay. Can’t wait to see if it’s true.


tags: tech, ruby, merb | comments: view

DataMapper - Form Friendly Integers

I like friendly things and DataMapper is one of them. I also like friendly forms and am a bit picky about how they should behave. Here’s part of a form for inputting a value into an Integer property on a DataMapper model.

Notice that some dumb-ass has entered something stupid in the text box. Now when they submit the form they end up seeing:

The error is great, but I’m not big on the disappearing trick that the text has done. Internally DataMapper is casting the input to be an Integer which results in the text vanishing never to be seen again. If the value input was a decimal then if there were no other errors on the form it would submit OK without complaining that the number wasn’t an integer and save the integer part of the number to the database – WTF!

The solution

There’s a very helpful docs page which explains how to write your own DataMapper types. So I did and it was pretty straightforward. The result is on my github account:

dm-form_friendly_integer

You can use this in your models and your form fields will behave a bit more sensibly (well I think so anyway).


tags: tech, ruby, datamapper | comments: view