Server changes 0

Posted by andy

I’ve kinda changed the whole platform mibly.com is running on. I’ll outline here what’s changed and why.

Source control iPlayerlist and some other projects I’m involved with now use Git, rather than Subversion for source management. The Rails core team have also moved over. So being the Technology magpie I sometimes am I jumped on to the latest shinny thing. Github. My code is now stored at Github, and I deploy from there too. It’s such a nice interface and nice not to have to worry about setting up repositories and deal with user management.

Application server The old server used to run on Apache 2 front end with 3 mongrel instances in the background. 2 for iPlayerlist and one for this blog. It was a pain to set up and rather limited. Since then however, Phusion Passenger, a.k.a. mod_rails has come on leaps and bounds. Its fast, spawns new threads when needed and easier to use and to implement. I now Passenger along with apache2. yay.

Deployment The old install used Capistrano. So does this one, as its lovely. However, I’m now up and running with the latest (version 2 strain) of Capistrano with support for Git. A little bit code is added to help Passenger to gracefully restart after all my code is checked out from github.

Host I’ve moved from slicehost to get a larger 512mb slice at railsplayground. It was a bit of a bargain and to be honest, 256mb can be a bit limiting when it comes to RubyGems and passenger.

iPlayerlist gets a little love 0

Posted by andy

Lots has happened since my last post. I’ve moved to a new server and suffered domain name issues. However on the positive side, I’ve had a little spring clean of iPlayerlist.

One of the big things I’ve added is the “featured episodes” on the front page. Now this is mostly powered by what appears on the front page of bbc.co.uk/iplayer. However, there is a little twist. The featured episodes are also powered by the reviews posted upon the rather ace Watchification blog. When an episode gets mentioned by that blog it will become featured, and a link to the review will appear on the episode page. Thanks to the people there for letting me do this :)

Thats about it for now. In the next week or so I’ll be putting up a re-factored version of the back end, which should get rid of those annoying bugs that keep cropping up. Or I might do a bit of it at the BBC Mashed event.

Radiofall

Posted by andy

Following on from the XMPP stuff, I made a little site that just reads and output what’s playing on BBC radio. It is a toy app at the moment with no real point (and might well fall over), its worth putting on the DAB radio to see the sort of delay it might be experiencing.

mibly.com/radiofall

What is it using I hear you bellow?
  • Ruby – well what else?
  • xmpp4r gem – for connecting to the xmpp server and parsing the messages
  • json gem – for outputting the messages in …. json
  • jQuery – to make all those Ajax requests

The next steps are to add some intelligence into the script, interacting with some APIs or something. I have a few ideas, but it depends upon time and if I can get somebody to help with design.

BBC Radio Labs and XMPP

Posted by andy

God bless BBC Radio Labs. They get to build some cool little things including a fun little XMPP server. Its pushing out messages that usually only get seen on the front of DAB radios. I whipped this ruby script together pretty quick (you can tell), but shows how easy you can connect to it.

require 'rubygems'
require 'ruby-growl'
require 'xmpp4r-simple'

username = 'myusername'
password = 'password'
domain = 'hug.hellomatty.com'
im = Jabber::Simple.new("#{username}@#{domain}", password)
im.status(:chat, 'Hello world')

#  This bit just adds a few radio stations.  Should only be run once for your user
#
#  %w(radio1 radio2 radio3 radio4 5live 6music radio7 1xtra).each do |r|
#    im.add("#{r}@#{domain}")
#  end

g = Growl.new "localhost", "ruby-growl", ["ruby-growl Notification"], nil, "growl"
i = 0
while true
  i+=1
  if im.connected? == true
    im.presence_updates.each do |friend, old_presence, new_presence|
      if new_presence != nil
        puts "#{friend.gsub("@#{domain}", '')} #{new_presence}"
        g.notify("ruby-growl Notification", friend.gsub("@#{domain}", ''), new_presence)
      end
    end
  else
    puts '**************************** disconnected *************************'
    im = Jabber::Simple.new("#{username}@#{domain}", password)
    im.status(:chat, 'Hello world')
  end
  sleep(2)
  if i == 10
    # reconnect every now and then as we seem to be having odd issues.  a bug really
    im.reconnect
    i = 0
  end
end

Here is some sample output.

radio3: Details from bbc.co.uk/radio3 or call 08700 100 300.
1xtra: 1Xtra Dancehall Show - Robbo Ranx. Txt 88111 [network rates apply].
radio4: The World Tonight. Web - bbc.co.uk/news
radio2: Mark Lamarr. Mark presents the Shake, Rattle and Roll Show.
5live: Richard Bacon: Lively and provocative discussion regarding the day's key stories

Now I have no idea what I can do with this (twitter posting is already done to death), so I might have a think about this sort of thing. Be fun if it makes it into production.

Custom validation with Paperclip

Posted by andy

In my rails apps I used to use attachment_fu for uploading files, but of late I have moved over to thoughtbots Paperclip plugin. Its perfect for most instances, and indeed simple to use. I required it to do a little more than it can do out of the box. In my case I needed to check that the uploaded document was a well formed XML document. The result was a nice little custom validation method levered into the plugin.

# lib/paperclip_mixin.rb
module Paperclip
  module ClassMethods
    def validates_attachment_valid_xml name
       require 'xml/libxml'
       @attachment_definitions[name][:validations] << lambda do |attachment, instance|
         if attachment.file.nil? || !File.exist?(attachment.file.path)
           "must be set" 
         else
         # check file is valid xml
           begin
             # use libxml-ruby gem to do basic check on the file
             parser = XML::Parser.new
             parser.string = File.open(attachment.file.path).read
             parser.parse
             return nil # i.e no exceptions equals validity
           rescue
             "must be a valid xml document" 
           end
         end
       end
     end
  end
end

Then initiallise this patch it in our initializers.

# config/initializers/custom_requires.rb
require 'paperclip_mixin'

Now we can just add this validation method to our model that is using paperclip

# app/models/asset.rb
class Asset < ActiveRecord::Base
  has_attached_file :config_file
  validates_attachment_valid_xml :config_file
end

And that worked fine. yay.

iPod and iPhone version of iPlayerlist 1

Posted by andy

Over a friday night I rebuilt iPlayerlist iPhone templates so that it uses Joe Hewitt’s iui. The result is quick to navigate, no silly pinch zooming to get what I want. Just point your iphone/ipod at iplayerlist.mibly.com.

Home Episode

There is a slight problem that is getting on my nerves a little. When you want to view an episode you are redirected to bbc.co.uk/iplayer. I had to do this as the iPlayer site uses mp4 embeds that are protected by a couple of layers of user agent strings and cookie validation. So, although I attempted a some rather hackey iframes, a link to iPlayer seemed to be the best option.

Building this version which emulates the iPhone system ui made me want to build a version using the Apple Touch SDK. I fear that getting my head around Objective-C might just take too much time. Would be damn cool though.

Anyhow give this a go and see what you think of it.

Giving twitter a go

Posted by andy

I finally understand the point of twitter. It’s just a tiny status update of your life that anybody can see. Simple, useful and fun. It took me a while … but now I’m a bit hooked

http://twitter.com/mibly

A plea to the twitter team. I consider twitter to be feature complete, please don’t “do a facebook” and clog it full of vampire bites/IM clients and super fun tweets pro. Keep it clean, keep it crisp.

Time for a Wii

Posted by andy

I got hold of a Wii this weekend, mainly because of Mariokart but also so I could faff around with its browser. The result is iPlayerlist for the Wii (thank you BBC). Again, its work in progress and consider it to be my programming play thing rather than a fully fledged service.

iPlayerlist on the wii

Its a shame the little Wii cant handle anything more than Flash 7, as its a bit blocky and clunky for video playback, worth a try though. Just point your Wii at http://iplayerlist.mibly.com.

Update I’ve put a PS3 user agent look up in there too. So PS3 users can have a play, a la ps3iplayer.com

Mixtapes

Posted by andy

Stumbled upon this awesome little site today that allows users to create online mixtapes. Yay retro. Mixwit uses seeqpod to find and play its music via its very easy to use flash app. I fear it won’t bring back the glory days of the mixtape, but its still rather cool.

This is making me want to go watch “High Fidelity” one more time.

A little about BBC iPlayer, youtube and playing 3

Posted by andy

Its nice to have a project that you dabble with as a hobby. A project that even if it fails, you won’t lose cash, job, respect or pets over and can only really gain through lessons learned. Currently, my hobby project is iPlayerlist.

I started to build iPlayerlist as I wasn’t a fan of the original bbc.co.uk/iplayer site. It didn’t group episodes by series, rather graphics heavy and required too much clicking about for my liking. So over a Christmas, I began writing some code that would give me something I would use, but also as a vehicle to play around with some APIs that are about.

I’m happy with the result in that I use it and have learned stuff through developing. If anybody else uses it, its a bonus.

Anyhow, “Whats in the pot?” I hear you ask. Well here are the services the site uses.

These services all contribute in a way. The BBC iPlayer site is screen scraped (yeah I know, I know, but there are no RSS feeds or API yet so I had to). We grab a little more information from /programmes relating to each episode such as descriptions and broadcast dates. Then for each series we search youtube via its API for related videos. We then get some related blog posts via Google Blog Search. These blog results are passed through Akismet to reduce the spam, then moderated by myself.

For all this to happen Rails 2.0.2 handles the donkey work with the following

There is loads more I could ramble on about here, but I’ll leave that for another post.

Why I'm shit and need to know more

Posted by andy

Its rare for a developer to talk about their weaknesses on the internet. It can damage their next job opportunity. However, fuck that, here is what I need to improve upon as I suck in these areas.

  1. Regular expressions
    Its a fucking travesty that I don't know regex inside out. I get by, but I'm no super hero.
  2. Python
    Never dabbled with it. I have moved from java to php and now Ruby (with a spattering of others in between). Python is one of those languages that looks awesome and enjoyable to arse around with.
  3. SQL advancement
    Many people boast that they know SQL. I could state that I do. However I feel that I should know more advanced SQL, looking at streamlining my code much more.
  4. C
    I, for my sins never learned C at university. I did history instead. *puffs on an invisible pipe*
  5. Some Microsoft pap
    I have never learned VB, nor Basic nor ASP. I don't know the suited "certified" world of Microsoft. This is wrong, I should at least try and use it, it can't all be bad.
  6. CSS
    I sodding hate IE 6. I should learn some CSS techniques that prevent me from foaming at the mouth and swearing at Bill Gates.
  7. Java web systems
    A mystery to me, although I understand its awesome power and "enterprise" deployment worth.
  8. Flash
    Flash is awesome at some things. Notably video delivery. I should play more with that.
  9. Growing hair
    Unix == Beard
    Flash == Ponytail
    Fortran == Sideburns
    Ada == That weird hair that comes out of your ears when you are old
  10. Realise that you shouldn't do all of this list and specialise a little ....

Ajax and accessibility

Posted by andy

I can chat about accessibility for an age, its become a bit of a passion. This passion was fanned just before Christmas when I met up with a web accessibility tester who was blind. This was a by chance meeting over a drink at an induction type thing in london. It was fascinating chatting to a person who experiences every day the problems that I knew existed, but could never imagine how they coped. From this brief chat it became clear that some people are still too fascinated by javascript without knowing how people with disabilities suffer as a result.

I admit, in the past, I built apps which were over reliant on javascript. If you used a screen reader or god forbid disabled javascript you were doomed. I have changed, learned more and realise the error of my javascript ways. As a result here are the rules I live by when making an app which might involve ajax.

  • Never build an app were javascript is required to do most tasks
  • If you can’t build your app without resorting to javascript. Read more as you are probably wrong.
  • Ajax can be awesome, if you do use it, use it unobtrusively
  • Only use ajaxy wizzy effects to give real feedback to the user. Not because it looks pretty
  • Never ever try and excuse yourself by saying “well netvibes.com doesn’t work without javascript”
  • If you can get stuff accessibility tested, as you Andy don’t have a disability and don’t know what its like
  • Remember, flash fades and wizzy effects have to be accessible. Lets not make the new blink tag

Plumbers are like programmers, clients are always clients

Posted by andy

client: "Yeah, thats great. How about we move that sink over here? That shouldn't take you long"
plumber: *massive sigh*
plumber: "I have to rejig the water confrabulater H20 washers inductions to do that."
client: *stares blankly*
client: "So you can do it then?"
plumber: "errr.. yeah."
client: "OK. So how much will it cost me?"
plumber: "err ... err ... 6 days at a guess"
plumber: "thats a guess though"
client: "can you do it in 4?"

Slicehost

Posted by andy

This blog and iplayerlist (which I will go into more detail in a later post) are hosted at Slicehost. I think its wise just to go through how everything is set up over here.

This is on the cheapo slice with 256mb of memory. Thats enough for me.

I used deprec to set up everything over here. It didn’t work straight off the bat and required a little tweaking but the resulting server setup is pretty much ideal for my needs.

  • Ubuntu Gutsy
  • Ruby on Rails 2.0
  • Mysql
  • Apache2 with mod_proxy
  • mongrel cluster application servers. (one for the blog and 2 for iplayerlist)
  • subversion source management
  • capistrano deployment (this end)
  • mephisto blogging system

I went with slicehost after I finally got annoyed at dreamhost. Now dreamhost is awesome and very cheap but for Rails apps it really doesn’t cut the mustard. Its FastCGI was slow and rails apps were a pain to set up. Slicehost offered a blank canvas which I could install what I wanted and can only blame myself if something goes horribly wrong, hurray! :)

jQuery and Ajax in Rails 2.0

Posted by andy

I'm having loads of fun with jQuery of late. Its small size and increasing collection of plugins has made me want to throw it in any app that has a need for javascript and ajax controls. So I thought I would just put down the basics for anybody wanting to give it a bash.

In this example I will be avoiding RJS and helper methods, sticking to writing pure Javascript. Plus as jQuery and jQuery UI is, in my opinion, rather cleaner and easier to read than Prototype and script.acul.us, RJS and helper methods would just gets in the way.

First thing is first, load up your javascripts including jQuery in your layout.

<%= javascript_include_tag 'jquery-1.2.1.min.js' %>
<%= javascript_include_tag 'application.js' %>
Lets make a standard link in a view that we are going to attach our jQuery magic to.
<%= link_to 'What do little foxes like to eat?', 
                    { :controller => 'foods',  :action => 'fox' }, 
                    { :class => 'ajaxLink' } 
%>

In our application.js we are going to convert this regular joe soap html link into an ajax one unobtrusively. Here is a good place to start having a read through the jQuery docs.

$(function() {
  $(".ajaxLink").click( function() {
      $.ajax({
          url: this.href,
          dataType: "script",
          beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");}
      });
      return false;
  });
});

Create our action in our foods controller that will spit out the food that foxes love to eat.

def fox
    @food = 'foxes love chunky Bacon'    
    respond_to do |format|
        format.js {}  # renders fox.js.html
        format.html { render :text => @food }
    end
end

This method will respond to an Ajax request by rendering fox.js.erb, however, if the user’s browser doesn't have javascript enabled we will just render the food string, this could easily be fox.html.erb.

Create our javascript ‚view‚ called fox.js.erb. We will just have an alert box for now, but this could easily be any other javascript method.

alert('<%= @food %>');
As you can see this is a fairly trivial example, but works rather well. Now if I wanted to Ajaxify any link, all I need do is give it the class .ajaxLink in the html. Much much cleaner than the onclick links the link_to_remote produced.