Welcome

Hello i'm Luís Mendes a Ruby on Rails hacker/engineer/developer for Excentric in Portugal and a full stack web developer.

HTML5 video, ogv, firefox and nginx 

If you are having problems serving ogv video content on Firefox there are some steps that you should follow.

Serving ogg files with the correct MIME type

First you have to ensure that you are serving the correct mime type for ogv files, it should be video/ogg you can test it using web sniffer (see the return value in the Content-Type: tag, you should see video/ogg)

If not try adding the following mime types to your nginx (eg: /etc/nginx/mime.types) and restart it.

video/ogg   ogm;
video/ogg   ogv;
video/ogg   ogg;

Handle HTTP 1.1 byte range requests and serving X-Content-Duration headers

Firefox or better yet the Gecko engine that powers Firefox / Thunderbird / SeaMonkey and others uses a sequence of HTTP 1.1 byte-range requests to retrieve the media from the seek target position in order to determine the duration of the media, it does this because ogv files don't contain the duration of the animation, so in order to determinate the duration of the file it retrieves a 8k chuck in the beginning of the file to determinate if the file is a valid ogg file, than based on the Content-Length header sent from the server it asks for the last chuck as you can see here, and finally it resumes the download from the first chuck as you can see in this image.

print screen partial content

In order to reduce the amount of requests made to your web server you can send the X-Content-Duration header with the duration in seconds of the media. If your video has a duration of 1 minute and 32.6 seconds you should server the value as 92.6

X-Content-Duration: 92.6 Here is example how you can serve a media file using nginx:

location /media/video.ogv {
  add_header X-Content-Duration "92.6";
}

If you're curious to see in greater detail why Gecko makes some of the HTTP requests it does, read comment 1 of bug 502894.

Consider using preload attribute

The HTML5 video and audio tags provide the preload attribute whitch tell Gecko to attempt to download the entire media when the page loads. Without the preload attribute Gecko only downloads enough of the media to display the first video frame and determinate the media's duration.

preload if off by default so to enable just add the attribute to the video tag and set it's value to auto.

<video preload="auto">
  <source src="media/video.ogv" type="video/ogg" /></video>

User geolocation without HTML5 navigator.geolocation 

Funny enough Google provides basic user geolocation using a undocumented part of there Google Loader API.

First you only need to include the code for the Google Loader.

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

Once loaded the Google Loader exposes a object called google.loader.ClientLocation if the object returns null it's because Google couldn't determinate the users location.

Follows the properties list of the google.loader.ClientLocation object:

google.loader.ClientLocation.latitude
google.loader.ClientLocation.longitude
google.loader.ClientLocation.address.city
google.loader.ClientLocation.address.country
google.loader.ClientLocation.address.country_code
google.loader.ClientLocation.address.region

A short example of how to use the api:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
if( google.loader.ClientLocation ){
  alert( "You are visiting from: " + google.loader.ClientLocation.address.country );
}
</script>

Rails 3.2 e mongoid 

Recentemente surgiu-me a necessidade num projecto em Rails 3.2.1 de utilizar como backend o MongoDB em vez de um sistema de SGBD convencional como o MySQL ou PosgreSQL.

Assim sendo decidi utilizar o mongoid contudo é necessario remover-mos todas as referências ao ActiveRecord para que a app funcione.

 

Para tal basta abrir o ficheiro application.rb em config/application.rb e comentar a linha que diz:

require rails/all

E colar imediatamente por baixo o seguinte (as linhas que código que vamos inserir foram retiradas do ficheiro railties-3.2.1/lib/rails/all.rb, limitei-me a tirar a referência ao active_record deixo esta informação como referência para os mais curiosos)

%w(
  action_controller
  action_mailer
  active_resource
  rails/test_unit
  sprockets
).each do |framework|
  begin
    require "#{framework}/railtie"
  rescue LoadError
  end 
end

Por fim temos que editar o ficheiro config/environments/development.rb e commentar as seguintes linhas (que fazem referência ao ActiveRecord):

# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict

# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5

Et voilà! Já temos a nossa app a usar nativamente o mongoid.