Rails : Validate availablity of a distant ressource over HTTP
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
I’ve started the development of Default Order Plugin for Ruby on Rails
FLV encoding with ffmpeg 15
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 installsecond step : Installing ffmpeg
Getting sources from svn :
$ svn export svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpegChange dir to ffmpeg and compile with liblame
$ ./configure --enable-mp3lame && make && sudo make installYour 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.flvto 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&autoStart=false">
<param name="movie" value="flvplayer.swf?file=output.flv&autoStart=false" />
<param name="wmode" value="transparent" />
</object>
</body>
</html>Enjoy !
Creating favicon.ico on Unix machines
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 png2icoand create your favicon file :
$ png2ico favicon.ico favicon16.png favicon32.pngNow 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
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 ;) ).
Ruby on Rails : Find with default order 1
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.

