Archive for the ‘Ruby’ Category

Migrating Ruby Twitter apps to OAuth

Saturday, August 14th, 2010

Migrating Twitter API apps from basic authentication to OAuth is up there with hoovering the bathroom and taking out the recycling in the “irritating chores to put off for another day” stakes. However, Twitter’s announcement that they’re dropping basic authentication support at the end of this month has made this task altogether more urgent, like an unexpected visit from the mother-in-law.

If you’re using the Ruby Twitter gem by John Nunemaker, you’ll be happy to learn that it supports OAuth. You’ll be less happy to see that the example code on the homepage merrily omits the difficult part:

# NOT SHOWN: granting access to twitter on website
# and using request token to generate access token

If your Twitter app is of the simple ‘bot’ variety, with a single dedicated account where everything happens, and no need for end users to authenticate against it, then most of the example code and documentation floating around the internet is overkill, sending you down the path of sessions and callback URLs, when all you really want to know is: what do I put in the ‘register an application’ form? How do I get hold of these tokens? And what do I do with this PIN code it’s just given me?

I eventually found a blog post at BeefyApps which cleared things up immensely, and so I’ve now reshuffled their code snippet into a mini command line utility which will spit out those all-important tokens and tell you how to use them.

twitter_oauth_setup.rb on gist.github.com

DivIDEo – Spectrum streaming video

Monday, May 3rd, 2010

Six years after my first tentative attempts at streaming video from the DivIDE interface were presented at Notcon 2004, I’ve finally come up with a system that I’m happy with. It boasts 25fps playback with audio somewhere above the ‘nails in a vacuum cleaner’ quality of previous attempts (through the use of delta compression on the video data and variable bitrate audio to use up whatever processor time is left), a one-shot conversion utility that handles all the video decoding, rendering and re-packing, and a player routine that more or less respects the ATA spec (so won’t fall apart as soon as someone else tries it on a different CompactFlash card. Hopefully). Here’s how I presented it at the Outline demo party:

The full description, and a whole bunch of downloads, is on the DivIDEo project website.

Midibeep

Thursday, October 29th, 2009

Last week I posted possibly the most tedious Basic type-in listing ever to World Of Spectrum:

5 BEEP 0.212765, 20; 10 BEEP 0.106383,19...
(continues for approx 1500 more lines)

Anyone typing it in in its entirety would be rewarded with this:

Download midibeep_minute_waltz.mp3

Not bad for an evening’s work. Mind you, I did take an ever so teeny shortcut, by writing a Ruby program to convert a MIDI file to BEEP format. (Any .mid file will do, although ones with a single instrument will survive the rather primitive selective-note-butchering process better. Oh, and anything much longer than this one will exceed the 48K Spectrum memory…) And now you can try it out too:

Update 2010-05-26: Karl McNeil has adapted Midibeep into a variant called Mid2ASM, which outputs an assembler listing rather than Basic – this enables the data to be packed much more efficiently, paving the way for altogether longer pieces of music. Download Mid2ASM (453K, Windows EXE included)

Update 2010-06-02: Another update from Karl, featuring a Windows GUI, more space-saving tweaks, and embedding the output in a Basic REM statement. Download Mid2ASM v2 (3.4Mb)

Update 2011-04-09: Karl McNeil has released the last version of Mid2ASM for a while, version 3.2 – featuring primitive importing from .sid .psg and .wav, and the choice of Basic or assembly output. The source archive also contains the command-line version, midibeep2.

Sleeper – mapping European night trains

Wednesday, June 10th, 2009

It’s now been 10 months and 10 demo parties since I last saw the inside of an airport (with plenty more to come over the next couple of months… parties that is, not airports), and for any eco-conscious European traveller like me, knowing which sleeper trains to catch is the key to happy travels. So, it’s a bit of a shame that there’s no single website you can go to to find out the best sleeper train to get to European Destination X. Sure, Seat 61 is a fantastic resource for finding out how to get to your country of choice, but you can never be sure whether you’d get better results by heading just across the border, or tweaking your journey times slightly…

So, in a classic case of building a website to scratch a personal itch, and not wanting to let niggly licencing issues get in the way of a cool idea… Sleeper is my new website aimed at searching and mapping the European sleeper train network in its entirety. It’s been put together with Ruby, Rails (gosh, that’s rather apt isn’t it…), Geokit, Google Maps, Hpricot and my own freshly open-sourced Bahn library for snarfing data from Deutsche Bahn‘s website, and hopefully it can give you a fuller picture than ever before of what actually exists in the wonderful world of sleeper trains. Right now it stops short of providing one overall definitive map of the network (it would probably crash your browser if I tried plotting it on Google Maps), but that’s on the todo list.

Deploying a Rails app without a database using Capistrano

Thursday, March 5th, 2009

So you’ve got a simple-but-devastatingly-clever Rails app that doesn’t use a database, you’ve dutifully added the line to config/environment.rb to disable the ActiveRecord framework, and now you’re ready to unleash it to the world, using Capistrano for deployment. But when you run ‘cap deploy:cold’, it merrily tries to run database migrations and fails with “uninitialized constant ActiveRecord”. How do you make Capistrano behave?

A simple question, with a simple answer, but one which is curiously absent from the internet. Googlejuice ahoy!

Stick this at the bottom of config/deploy.rb:

namespace :deploy do
	desc "Override deploy:cold to NOT run migrations - there's no database"
	task :cold do
		update
		start
	end
end

FreshBEEP

Sunday, January 4th, 2009

Kicking off 2009 in style with a website-in-a-day, FreshBEEP is my answer to a pressing problem on the Spectrum scene: people are releasing new games all the time, but they’re being overlooked due to a lack of a prominent central place to announce them. However, it turns out that on the World of Spectrum What’s New page, nestled amongst the hive of other site activity that gets mentioned there, Martijn has been dutifully labelling new releases under a “New software for 2008″ banner. One swift bit of mashup work later (does it count as a mashup if it’s only coming from one site?) and these are now being pulled out into a friendly blog-like format, with an all-important RSS feed. It’s not particularly clever, but it scratches an itch…

The Ninja Milkman Conspiracy (and Maze)

Sunday, October 5th, 2008

I’m a bit behind in my blogging, so I’ve got a bit of a “what I did this summer” catchup to do. First up is The Ninja Milkman Conspiracy, a cheap and cheerful oldskool scrolly Speccy demo made for this August’s International Vodka Party, featuring the classic circle interference effect, some creative use of dithering, and a dancing robot. What more could you ask for? The title, incidentally, was just something random and irrelevant to save it from being called ‘IVP 2008 demo’ (which is just as well, because there were already 2 other demos in the competition called that) but is actually a reference to the milkman at one of our offices who is able to deliver the milk and disappear without making a sound.

…All of which is ordinary enough, but the exciting thing (if you’re the sort of person to get excited about build scripts) is, um, the build script. I’ve been happily using makefiles for ages, but this time I finally flipped at the amount of redundant boilerplate you need to shove in there for a typical Spectrum project, even a small one like this – having to remember command line syntax, having to explicitly set up dependencies even though they’re all clearly marked as ‘include’ lines in the assembler file – so I came up with Maze, a Spectrum-oriented replacement for Make written in Ruby. Inevitably, being a scratch-my-own-itch sort of program, it’s a bit more hard-coded (and tuned towards my own way of working) than I’d like, but I reckon it’s enough of an improvement over bog-standard makefiles that it could conceivably be useful to other people. And if it is, maybe I’ll be encouraged to rewrite it in a more open-ended way some time…

Some rather opinionated plugins

Saturday, August 30th, 2008

Update: The belongs_to patch has now made it into core Rails. Thanks to Jon for persevering with it where I threw in the towel, and to Koz and Pratik for giving it their attention. I’m still not entirely convinced that going through the conventional channels (as opposed to “whinging on a blog”) would have yielded the crucial feedback to get the patch in a state where it could be accepted, but all’s well that ends well…


A shark, being jumped, yesterday.Take this as the rantings of a pissed-off curmudgeon who’s had his second core patch rejected if you will, but it’s my sad duty to report that Rails has jumped the shark. It happened when it stopped being a framework and started being a collection of opinions instead.

You see, in my book a framework is something that’s meant to make programmers work more effectively – not to lecture them about what they’re doing wrong. Lecturing can be a good thing, as long as it ultimately results in better code – I’ll happily admit that following the Rails learning curve has taught me an awful lot about good programming practices. More and more though, I’m finding it being used to justify just plain broken features: If it doesn’t work, it’s your fault for not doing it the right way. Take this bit of profoundly broken behaviour:

let’s say we have a Company model, which belongs_to :city

>> torchbox = Company.find_by_name('Torchbox')
=> #<Company id: 1, name: "Torchbox", city_id: 1>
>> torchbox.city
=> #<City id: 1, name: "London">

Aha, we’ve got the city wrong. Let’s reassign a bundle of attributes then, like we would if we were doing this through a web form:

>> torchbox.attributes = {:name => 'Torchbox', :city_id => 2}
=> {:name => 'Torchbox', :city_id => 2}

Right, so now we should retrieve the correct one.

>> torchbox.city
=> #<City id: 1, name: "London">

Er, oops.
(more…)

Comet Chaos

Monday, July 7th, 2008

Thanks to Oleg for reminding me at OpenTech that I still hadn’t written this up yet…

This started out as an experiment in Comet techniques (which allow you to actively push data out from web servers without the client having to initiate the request) which quickly ballooned in ambitiousness – I didn’t set out to write Just Another Chat Application after all. The end result is a realtime multiplayer Javascript conversion of the Spectrum wargame Chaos… or a reasonable chunk of it, at least.

Play Comet Chaos now

If you’re interested in the workings behind it, check out this video from my Oxford Geek Nights presentation to hear about how Comet is like a small child on a car journey, find out how close web developers can get to world domination, and watch a live demonstration going pear-shaped.

(more…)

tracker2ay

Saturday, May 10th, 2008

Here’s a utility prompted by zxbruno and Eq both asking, in the space of two days, how to convert STC, SQT and PT3 music files to something you can actually play on a Spectrum. For those not in the know, STC and friends are Spectrum tracker file formats originally introduced by Sergey Bulba‘s AY Emulator and which are now the de-facto standard for archiving Spectrum demoscene music (most prominently on ZXDemo, ZXTunes and Sergey’s epic Tr_songs archive). Which means it’s a bit unfortunate that there’s not been an obvious way to transfer them back to the Spectrum.

In principle it should just be a case of locating the appropriate Z80 player routine and bundling that together with the music data. In practice it involves a lot of faffing about (such as repointing pointers to make up for slight rubbishness in the SQT data format, and writing a 5-line Basic loader/player). Now, thanks to this utility, you just need to type tracker2ay mysong.stc mysong.tap instead. (Oh, and it can convert to TAP, TZX or AY.)

If at this point you’re screaming “But why does it have separate source code if it’s written in Ruby, which is an interpreted language?” then award yourself 20 geek points. Ah, you see, this time I’ve been playing with rubyscript2exe (and tar2rubyscript) to create all-in-one executables that everyone can enjoy without worrying about library dependencies and things. (But obfuscates the code in the process. But in a good way.) Please do check out the source code if you’re curious about that sort of thing, because I reckon it’s one of the best bits of code I’ve written in a long time, in a ‘nicely-written code’ sort of way rather than ‘evil complicated hacks that go together to do something superficially elegant’.