Rails : Validate availablity of a distant ressource over HTTP

Posted by Antonin AMAND Wed, 18 Jul 2007 22:39:00 GMT

Sometimes you may host some ressources on a different website and you may want to validate the availability of it when creating a Rails model object (ActiveRecord::Base).

It would be dramatically unefficient to download the full media with a GET request. So, to achieve this we will use the HEAD http request that act as the GET request but return only the HTTP header without the body.

Here is a exemple of an ActiveRecord::Base object that validates the presence of the ressource among the Internet.


require 'net/http'

# the class is supposed to have a "media" attribute
# that is a relative url to a ressource hosted an a different
# website
class Article < ActiveRecord::Base

  RESOURCE_HOST = "flickr.com"
  RESOURCE_PORT = 80

  validate :validates_presence_of_external_media

  protected

  def validates_presence_of_external_media
    response = nil
    # initiating the HTTP connection
    Net::HTTP.start(RESOURCE_HOST, RESOURCE_PORT) do |http|
      # doing the HEAD request
      response = http.head(media)
    end
    # add an error if the request is not successful
    errors.add :media, "ressource unavailable : #{response.code} #{response.msg}" unless
      responce.kind_of?(Net::HTTPSuccess)
  end

end

This is a very minimalist example, but this kind of technic may have a lot of uses.

Default Order Plugin for Ruby On Rails

Posted by Antonin AMAND Mon, 16 Oct 2006 18:40:00 GMT

I’ve started the development of Default Order Plugin for Ruby on Rails

more…

FLV encoding with ffmpeg 15

Posted by Antonin AMAND Mon, 25 Sep 2006 17:22:00 GMT

ffmeg is a command-line tool for video encoding which has the ability to encode videos in FLV format (Macromedia plugin for direct-streaming).

First, you need to install ffmpeg with liblame support.

You may grab it as package or compile from sources.

We will compile from sources in this article for more compatibility.

first step : Installing lame get sources at http://lame.sourceforge.net, untar the archive and chdir to unpacked sources directory.

$ ./configure && make && sudo make install

second step : Installing ffmpeg

Getting sources from svn :

 $ svn export svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg

Change dir to ffmpeg and compile with liblame

$ ./configure --enable-mp3lame && make && sudo make install

Your now setup. You can continue with encoding your first video

$ /usr/local/bin/ffmpeg  -i input.mov -ar 22050 -ab 56 -aspect 4:3 \
 -b 200 -r 12 -f flv -s 320x240 -acodec mp3 -ac 1 output.flv

to view the result download a swf FLV player and create a html file :

<html>
<head>
<title>Flash FLV Player</title>
</head>
<body>
<h3>My First FLV video</h3>
<object type="application/x-shockwave-flash" width="320" height="260" wmode="transparent" data="flvplayer.swf?file=output.flv&amp;autoStart=false">
<param name="movie" value="flvplayer.swf?file=output.flv&amp;autoStart=false" />
<param name="wmode" value="transparent" />
</object>

</body>
</html>

Enjoy !

Creating favicon.ico on Unix machines

Posted by Antonin AMAND Sat, 23 Sep 2006 14:27:00 GMT

Favicon are little icons that you can see in your browser before URL address.

To create this icons on unix-likes proceed as follow.

Open your favourite Image manipulator program, for me It will be The GIMP.

Create a 16×16 px PNG, and optionally a 32×32 px PNG.

Install png2ico from sources, package or compile it via darwinports.

For OS X with darwinports :

$ sudo port install png2ico

and create your favicon file :

$ png2ico favicon.ico favicon16.png favicon32.png

Now that you have your favicon file, just put it in your web server root directory.

You can had the following for IE compatibily in your web pages :


<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />

Ruby on Rails / Prototype : Shortcuts for javascript

Posted by Antonin AMAND Fri, 22 Sep 2006 12:05:00 GMT

If, as me, you use the prototype javascript library on top of all your scripts, you might want to use this shortcuts to save some time and increase readability of your code (But only if you know them ;) ).

Here is the list of shorcuts from Johnathan Tron’s blog

Ruby on Rails : Find with default order 1

Posted by Antonin AMAND Tue, 05 Sep 2006 12:56:00 GMT

I’ve found a solution (I am propably not the first ;-) ) for default ordering on ActiveRecord:Base children. The tip is to override the find method :


class Contact < ActiveRecord:Base

  def Contact.find(*args)
    if args[1] 
      args[1][:order] = "last_name, first_name" if args[1].is_a?(Hash) && !args[1][:order]
    else
      args[1] = {:order => "last_name, first_name"}
    end
    super(*args)
  end

end

If you have something cleaner please comment this post.