I use RVideo gem a lot for a video processing application I have been building in Rails which will be open sourced soon on github. Using the gem you can create a RVideo::Inspector object to gather information about the uploaded file including its fps and content type.

If you use the latest version of ffmpeg (mine is SVN-r16905), I find that I keep getting nil values whenever I run the inspector objects. I googled around and found that this is due to a regular expression mismatch in the source. Change line 53 in inspector.rb in the source to:

metadata = /(Input \#.*)\n(Must|At\sleast)/m.match(@raw_response)

Hello there. First of all apologies for not posting for a while. I have been busy working on commercial Rails projects for the very first time and as such, that takes precedence. However, I am also aware of the need to keep blogging and keep up the practise more as a note to myself about the things I have encountered through work and also to show potential exmployers that I KNOW WHAT I AM TALKING ABOUT.

To start off 2009, I would like to highlight an interesting error I came across today with regards to using attachment_fu plugin. It is a great plugin for handling file uploads in Rails and there are plenty of resources on the web to get you started. The issue arose when I fired up an old Rails application running on 2.0.2 with a view that uses attachment_fu for uplaods. The following error appears on the console:

NoMethodError in XXXController

undefined method `[]‘ for #<Enumerable::Enumerator……..>

So I upgraded to Rails 2.1 and 2.2 but the error still persisted. It transpired that it is a conflict with the verion of Ruby you have running on your system (I use a MAC running on Ruby 1.8.7.) – it appears to be an issue with Ruby 1.8.7 ’s string methods mdofications.

To fix it simply navigate to vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb and change the following line:


attachment_options[:path_prefix]   = attachment_options[:path_prefix]
[1..-1] if options[:path_prefix].starts_with?(‘/’)

Note that starts_with? is a String method only in 1.8.7. Save an relaunch yor application and all should be well.

I obtained the above from the following google group link:

http://groups.google.com/group/attachment_fu/browse_thread/thread/502dd4504a59c84d

Hope this helps someone – certainly helps me after hours of debugging!

P/S if the above has changed since then please let me know as I may not be the most up to date.

If you have just completed an application, its more than likely you would like to have an admin portal to administer users. The easiest way to do this in Merb is to use the ‘merb-gen resource_controller’ command in your application directory to generate a CRUD controller and views for the resource. This is similar to using scaffold in rails.

Say for instance I have a resource called Posts and I want to generate an admin front-end to it. All I need to do is change to the application directory and type:

merb-gen resource_controller admin/posts

This creates a subfolder called ‘admin’ within your controllers and a new posts controller within that to handle all the CRUD functions. However, on MERB 0.9.5, the controller is missing the module keyword and if you try to run the above you would receive a namespace or controller not found error.

To fix this, open up ‘admin/posts’ controller and add ‘module Admin’ to the file:

module Admin

class Posts < Application

....

end

end

Next add the required namespace to the ‘config/router.rb’ file:

Merb::Router.prepare do |r|

r.namespace :admin do |admin|

admin.resources :posts

end

end

Fire up your app and it should all work. I’m not certain if this is fixed in subsequent versions of Merb but I did notice a ticket has been submitted for nested namespaces error. If anyone reading this has more information on the official line, please do leave a comment

NEWS FLASH:

Just received an up-to-date comment from one of the MERB framework developers who read this post and confirmed that the latest version of ‘merb-gen’ has been fixed to prevent the error above. So upgrade your version of Merb to prevent it. And you still have to add in the namespace in your router file manually.

One of the commercial websites I implemented (www.bauhaus-bathrooms.co.uk) has a RSS feed for their news items. I have considered various alternatives for parsing the feed but nothing is more elegant and efficient than using Ruby. Here is an example snippet of it:


require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'

source = "http://www.bauhaus-bathrooms.co.uk/news/rss.xml"
content=""

open(source) do |s|
  content = s.read
end

rss = RSS::Parser.parse(content,false)

puts "Root values"
print "RSS title: ", rss.channel.title, "\n"
print "RSS link: ", rss.channel.link, "\n"
print "RSS description: ", rss.channel.description, "\n"
print "RSS publication date: ", rss.channel.date, "\n"

puts "Item values"
print "number of items: ", rss.items.size, "\n"
print "title of first item: ", rss.items[0].title, "\n"
print "link of first item: ", rss.items[0].link, "\n"
print "description of first items: ", rss.items[0].description, "\n"
print "date of first items: ", rss.items[0].date, "\n"


Run the above on the command prompt and it should print out the requested properties of the feed.

It would be really good to be able to build a GUI interface using SHOES to display the feed contents. Might use that as a future project!

Using Rack

August 20, 2008

I heve been experimenting with Rack for a while and thought I would write a short post about it here.

In essence, Rack is an API that sits between your web framework and the webserver itself. A basic Rack application consists of a call method that takes a hash of environmen variabes such as whre the request comes from and returns a hash of variables containing a status code, content-type and a response string.

A minimal Rack application is listed below:

  %w(rubygems rack).each {|dep| require dep}

  app = lambda{|env| [200, {}, 'Another Test!']}

  Rack::Handler::Mongrel.run(app, :P ort=> 3000)

Make sure the Rack gem in installed onto your local system first. Fire up the console and run the script.

You will see a flashing command prompt as the script is waiting to receive a HTTP request. Type ‘http://localhost:3000/’ into your browser an you should see the string ‘Another Test’ being returned to the browser.

I utilised the technique above to create a middleware class in my ongoing Merb application to handle file uploads away from the framework itself but stilll being able to access certain components of the framework (i.e. ActiveRecord objects) if needs to. An example of how to accomplish this can be found on Ezra’s blog(the creator of Merb).

A snippet of the middleware code will be made available soon after refactoring.

I have just started experimenting with the MERB framework recently after using Rails for about a year. Although I am of the opinion that there are far too many web frameworks out there and I’m still in the process of learning Ruby properly as a language, I can’t help but be curious about the features this much-talked about framework can deliver.

I use a Windows box for development work in the offices. After a few failed attempts to get the gems to install correctly, I setup a simple file uploading script using just a controller as follows:


class Uploader < Application
  def index
    render
  end

  def upload
    FileUtils.mv params[:file][:tempfile].path, Merb.root+”/uploads/#{params[:file][:filename]}”

    redirect “/uploader”
end

end

The controller is uploading a file into the ‘uploads’ directory in the application path. However, if you run the above as it is on a windows machine, you would receive a ‘permission denied’ error. Changing the permissions on the folders would not make any difference.

The only way to fix this is to add a line of code to the gem library as follows:

  1. On your windows box, browse to you gem directory and locate ‘merb-core-<version>/lib/merb-core/dispatch/request.rb’.
  2. Browse to line 526 and add the following:

    else
    data = body
    end (approx line 526 here)

    (only add the body.close line after end on line 526)

    body.close

Save the file. Restart your application and you should find that the controller is able to upload to the specified directory.

Official documentation on this can be found here.

Although the bug has been found since January this year, I hope that anyone who comes across this problem on Windows will find this useful.