jose.costa

RailsConf 2010: Interview with George Guimarães 0

Posted by jose.costa
on Wednesday, July 28

Among the great people we met at the RailsConf was George Guimarães (@georgeguimaraes), by no surprise he was a very cool and funny dude, and he was open to talk and hang around with us.

George is co-founder of Plataforma Tec, that many of us got to know well through their amazing set of gems/plugins and the stunning work José Vailm has been doing as a Rails Core team member. Kind of what I hope it’s happening with the great work Santiago Pastorino is doing with his contributions and how it’s reinforcing WyeWorks reputation.

But still there are a lot of guys behind the scenes that support this great Open Source effort that’s being carried in these companies, probably not so much by direct intervention but with more hard and ‘real’ work as we like to say to joke around and bother the OSS guys. The kind of work that pay the bills :P

Not that George doesn’t do OSS, actually he’s currently involved in the development of Restfulie that got many people pretty excited at the RailsConf this year, and much more stuff.

Hope you enjoy this not so serious take.

Thanks George!

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

jose.costa

RailsConf 2010: Interview with Fabio Akita 3

Posted by jose.costa
on Thursday, July 01

In the last day of the RailsConf 2010 Santiago and I got the pleasure to hang out a few hours with Fabio Akita, a very successful guy in the Ruby and Rails community, but yet so humble and open as many of the great people we met in these past days.

Fabio is well known as a great evangelist of this movement, as a great speaker, for his bundle of plugins for the vim editor but also for carrying interviews with important people of the community on every conference he attends. The thing is that personally I never got to see him as the subject of the interview, and he is a wise guy that has a lot to say. So even though he was very tired, he accepted to do this improvised short interview with me, almost the first time I was holding a camera.

This is the first of a small series of interviews that will be posted soon.

I hope you all enjoy it and share his thoughts on what are the good things that makes this community so special, so that we never loose that spirit.

Thanks Fabio!

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

sebastian.martinez

Making Paperclip work with Sinatra & Datamapper 4

Posted by sebastian.martinez
on Wednesday, February 10

I was working lately on a Sinatra project, and got fascinated on how fast you can get things up and running. Everything was beautiful, until I tried to upload a file using paperclip.

Although Paperclip was originally built for rails Ken Robertson ported it to Datamapper. Let me explain in few steps how you can upload with Paperclip, using Datamapper.

Start declaring your model like this:
class Resource
  include DataMapper::Resource
  include Paperclip::Resource

  property :id,     Serial

  has_attached_file :file,
                    :url => "/system/:attachment/:id/:style/:basename.:extension",
                    :path => "#{APP_ROOT}/public/system/:attachment/:id/:style/:basename.:extension" 
end

You’ll need to specify your :url and :path options as the ones built into dm-paperclip are merb centric which won’t quite work. Also set APP_ROOT to where ever your application root directory with your static Sinatra folder is.

Now your routes should look something like this:
post '/upload' do
  resource = Resource.new(:file => make_paperclip_mash(params[:file]))
  halt "There were some errors processing your request..." unless resource.save
end

And there’s the tricky part, on the make_paperclip_mash method. Paperclip expects the file object loaded from the form to be in a different form than what is created by default. To fix this you should create a Mash (which is just a Hash, unless you’re actually using merb):

def make_paperclip_mash(file_hash)
  mash = Mash.new
  mash['tempfile'] = file_hash[:tempfile]
  mash['filename'] = file_hash[:filename]
  mash['content_type'] = file_hash[:type]
  mash['size'] = file_hash[:tempfile].size
  mash
end

And that’s it, now you can upload files using Paperclip right on your Sinatra app with Datamapper. You can check out the code of this example at: sinatra_paperclip.rb

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

santiago.pastorino

4 Ways to Retrieve a Twitter List Timeline 1

Posted by santiago.pastorino
on Monday, February 01

For past few days we had been working on the new version of the WyeWorks site, so stay tunned. This new version will have a twitter section, where the last 5 tweets of our team will be displayed.

So first thing I had to do was installing the twitter gem:
gem install twitter

In order to achieve this, I’ve found 3 different ways using the twitter gem, plus one not yet implemented on the gem, that I’ve already proposed the patch. The dumbest one would be:

#!/usr/bin/env ruby

require 'rubygems'
require 'twitter'

users = %w(wyeworks spastorino joseicosta smartinez87 nartub)

tweets = users.map { |user_name| Twitter::Search.new.from(user_name) }.
               inject([]) { |tweets, search| tweets + search.fetch(5).results }.
               sort { |t1, t2| Date.parse(t2.created_at) <=> Date.parse(t1.created_at) }[0,5]

tweets.each do |tweet|
  puts tweet.created_at
  puts tweet.from_user
  puts tweet.profile_image_url
  puts tweet.text
end

What this does is retrieve the last 5 tweets of each user and merge them sorted by date. Obviously, best thing to do would be directly retrieving the last 5 tweets from the @wyeworks/team list. Only way to do this using the gem requires authentication, despite the list being public. In order to authenticate, we may take two paths, the first one would be using HTTP Authentication:

#!/usr/bin/env ruby

require 'rubygems'
require 'twitter'

httpauth = Twitter::HTTPAuth.new('wyeworks', 'password')
base = Twitter::Base.new(httpauth)
base.list_timeline('wyeworks', 'team', :page => 1, :per_page => 5).each do |tweet|
  puts tweet.created_at
  puts tweet.user.screen_name
  puts tweet.user.profile_image_url
  puts tweet.text
end

The other and preferred way for authentication is OAuth, since we don’t have to send the user and password through the network. In order to make OAuth work with twitter, we have to create an application at http://twitter.com/apps Once we’ve created the app, twitter provides us with a Consumer Key and a Consumer Secret, needed to authenticate using OAuth

#!/usr/bin/env ruby

require 'rubygems'
require 'twitter'

oauth = Twitter::OAuth.new('consumer token', 'consumer secret')

begin
  oauth.authorize_from_access(oauth.request_token.token, oauth.request_token.secret)

  base = Twitter::Base.new(oauth)
  base.list_timeline('wyeworks', 'team', :page => 1, :per_page => 5).each do |tweet|
    puts tweet.created_at
    puts tweet.user.screen_name
    puts tweet.user.profile_image_url
    puts tweet.text
 end
rescue OAuth::Unauthorized
  puts "> FAIL!" 
end

Either of these ways works just fine, but no one completely satisfied me, since we are working with a public list, so as far as I can see authentication is out the question, even more when anyone can see it directly from the web without authenticating. For this reason, I started looking at the Twitter API searching for a non-authentication way to do it: Here’s what I found. You can test that making a request to http://api.twitter.com/1/wyeworks/lists/team/statuses.json?page=1&per_page=5, obtaining the list as a json, or xml in case you change the .json to .xml

So I’ve came up with this monkey-patch:

module Twitter
  # :per_page = max number of statues to get at once
  # :page = which page of tweets you wish to get
  def self.list_timeline(list_owner_username, slug, query = {})
    response = HTTParty.get("http://api.twitter.com/1/#{list_owner_username}/lists/#{slug}/statuses.json", :query => query, :format => :json)
    response.map{|tweet| Hashie::Mash.new tweet}
  end
end

Being able to get the list without authenticating by:

Twitter.list_timeline('wyeworks', 'team', :page => 1, :per_page => 5).each do |tweet|
   puts tweet.created_at
   puts tweet.user.screen_name
   puts tweet.user.profile_image_url
   puts tweet.text
end

I’ve already contacted the gem’s authors, proposing this patch: http://github.com/spastorino/twitter/commit/aed3a298b613a508bb9caf93afc7f12c50626ad7. Wynn Netherland already told me it’s pretty probable that it will be approved.

Until then, you can make use of this functionality from my fork http://twitter.com/spastorino/twitter

UPDATE #1: My fork was merged into http://github.com/jnunemaker/twitter master branch and twitter 0.8.3 was published through Gemcutter

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

santiago.pastorino

Rails Bugmash an exciting first experience 3

Posted by santiago.pastorino
on Tuesday, January 19

As many of you must know, last weekend a Railsbridge Bugmash was celebrated, and I had the honor to be part of it for the first time. For those who are not aware about what this is, it’s a virtual event that takes place on irc.freenode.net at #railsbridge channel. The general idea of these events is to work on the Rails Core, taking a look at the Rails Issue Tracker. However, given that Rails 3 is about to see the light, this opportunity was intended to deeply test it looking for:

  • Fix a known issue
  • Report a bug
  • Make sure your favorite gem or plugin still works. If not, fork it and make it so.
  • Write a blog post about a certain component
  • Write some documentation
  • Get an app up and running and document what you had to do to upgrade.
  • Create a screencast

Each accepted contribution or patch done by a participant, gives him chances to win one of several prizes, the more contributions the more chances you have to win. For further details check out the Railsbridge Bugmash Guide and the RailsBridge Wiki.

Many bugs were fixed during the event, some plugins and gems were tested, really interesting discussions took place (specially regarding generators) and some other articles were made. To mention some:

And then Jose Valim, motivated by the posts done regarding rails 3 generators, published one himself called Discovering Rails 3 Generators

This was my first participation and it was a bloody great experience. During the event I was specially devoted to search bugs on Rails 3 and try to patch them. I managed myself to find 4 code bugs and 1 documentation bug, proposing a solution for all of them. One of them was not approved though, for there was a bigger problem that required José Valim’s intervention, the 3 other patches were approved.

I would also want to highlight the gigantic work done by the organizers, the core team members, and also to all the participants. I strongly valued the support José Valim, Pratik Naik and Michael Koziarski gave me. They were a hell of a help when trying to find solutions for the bugs. Would also want to thank Ryan Bigg for his cooperation, and specially to Sam Elliott, an incredible 18 year old guy with a great future ahead. Besides working together with Ryan and Sam, we kept a really nice chat during almost the entire bugmash. The awesome will to help at #railsbridge really overwhelmed me, where everybody was willing to help each other, something that makes it so much easier for the ones starting to contribute to Rails.

I would also want to congrat and thank all the participants for trying to make Rails better. So don’t hesitate to join the next time, it’s not as hard as I expected it to be and you would definitely not regret it. Now I’m really motivated to help others so I’m going to join to Rails Mentors on RailsBridge to give back the good things I received during this experience.

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

jose.costa

Generating PDF with odf-report and images support 0

Posted by jose.costa
on Friday, September 18

Well i guess many of you might have checked the last edition of the Rails Magazine. If not, you may should.

In particular one of the articles by Rodrigo Rosenfeld Rosas talks about PDF generation with odf templates, something I’ve been playing around in the last few weeks. He explains the mechanism that can be used for this purpose and finally mentions the odf-report gem by Sandro Duarte which i’ve been using and forked to add support for image substitutions.

More info here: http://github.com/josecosta/odf-report

Give it a try!

UPDATE Fork was merged to master branch.

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

santiago.pastorino

My Emacs for Rails 12

Posted by santiago.pastorino
on Friday, September 11

I’d like to share with the community my emacs init file and a set of plugins to give a nicer experience on Ruby on Rails development, which you can checkout from http://github.com/spastorino/my_emacs_for_rails/tree/master. I have been using this environment under emacs 23, and it has not been tested on other emacs versions, so all feedback is welcome, if something goes wrong please feel free to contact me.

What’s in the package?

The package provides my customized Emacs init file and some plugins I found very useful to enhance Ruby on Rails development experience.

Plugins

Installation

Before you can use some plugins you have to install a few packages:

To use rcodetools you need to install the rcodetools, gem with
sudo gem install rcodetools
To use autotest you need the ZenTest gem, install it with
sudo gem install ZenTest
On further post I’m going to explain how to set it up under the gnome environment using all the beauty that gnome-notifier has. Checkout the package
git clone github.com/spastorino/my_emacs_for_rails
and copy all the files under my_emacs_for_rails directory to ~/.emacs.d directory if it doesn’t exist you have to create it.

Screenshot

You can see here some screenshots to get a taste of the look and feel that this one has.

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

sebastian.martinez

Scheduling in Ruby 5

Posted by sebastian.martinez
on Monday, August 10

Scheduling tasks is something we all need to know to do, for it’s quite common in applications. Fetching feeds, indexing some data, processing files at a periodical time, that happens a lot. You are probably quite familiar then with the linux cron, if you had to deal with scheduling stuff in the past, but there is something you may not. Let me introduce you the Whenever gem. What is it? A simple gem to schedule tasks writing them in nice ruby syntax…just let the gem work it’s magic and deal with the cron.

In order to install it, you have to add first the github source, only if you never done it :
$ gem sources -a http://gems.github.com
$ sudo gem install javan-whenever
To get started, just place yourself in the app path and type
$ wheneverize . 
This will create an initial config/schedule.rb for you.

There you can nicely write tasks to run.

Some examples are:
every 3.hours do
    runner "MyModel.some_process" 
    rake "my:rake:task" 
    command "/usr/bin/my_great_command" 
  end

  every 1.day, :at => '4:30 am' do
    runner "MyModel.task_to_run_at_four_thirty_in_the_morning" 
  end

  every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
    runner "SomeModel.ladeeda" 
  end

  every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
    runner "Task.do_something_great" 
  end
Another nice thing to do is integrate it with Capistrano.

 after "deploy:symlink", "deploy:update_crontab" 

  namespace :deploy do
    desc "Update the crontab file" 
    task :update_crontab, :roles => :db do
      run "cd #{release_path} && whenever --update-crontab #{application}" 
    end
  end

The official documentation provides this way to integrate it, but we found some little details, and the solution is here for you.

If you integrate it by the regular way, when making multiple deploys, it will configure several tasks to run in your cron file. Why? Because the path of the current application changed, and the gem uses the path (by adding a comment line) when updating the cron. What should we do then? Simple, change the run line with this:
run "cd #{current_path}; whenever -i #{current_path}/config/schedule.rb" 

What have we done? We used the -i option, which makes an update, but passing the comment we want, and make it not to change in every deploy.

Feel free to try it and leave your comments !!

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

jose.costa

Method missing to simplify queries to an external service 5

Posted by jose.costa
on Wednesday, August 05

I know there are several discussions on the usage of method_missing in Ruby. In this post i’ll present a pretty simple, yet useful solution that uses method_missing to interact with the Brightcove Media Read API (you don’t need to be familiar with this service, i’ll explain a little bit in the next few lines).

Brightcove Media Read API accepts calls of the form:

http://api.brightcove.com/services/library?command=find_all_videos
&token=0Z2dtxTdJAxtbZ-d0U7Bhio2V1Rhr5Iafl5FFtDPY8E.

http://api.brightcove.com/services/library?command=find_related_videos
&video_id=123123&token=0Z2dtxTdJAxtbZ-d0U7Bhio2V1Rhr5Iafl5FFtDPY8E.

http://api.brightcove.com/services/library?command=find_videos_by_text
&text=sometextsample&pageSize=100&token=0Z2dtxTdJAxtbZ-d0U7Bhio2V1Rhr5Iafl5FFtDPY8E.

A token must be passed on each call, and you could also add more parameters like you would do in a regular GET request. What comes back is a JSON string that can be easily picked up.

The key thing here is to notice that there are several commands you could execute from the API, naturally each with its own name that must be specified in the request right after “command=”. Since the API also provides a set of error codes to address all wrong requests or non-existent commands requests, we simply wanted to forward all the calls to the API and reply back with its answer. So, in order to avoid defining all API methods in our Ruby module, we just used the method_missing and forwarded all calls to it, using the name of the method as the API command.

The idea was that a call like:

http://api.brightcove.com/services/library?command=find_videos_by_user_id
&user_id=34876423&token=0Z2dtxTdJAxtbZ-d0U7Bhio2V1Rhr5Iafl5FFtDPY8E.

became just:

Brightcove::ReadProxy.find_videos_by_user_id :user_id => 34876423

We then implemented what we called the Brightcove::ReadProxy in a few lines like shown below:

module Brightcove
  module ReadProxy

    TOKEN = '0Z2dtxTdJAxtbZ-d0U7Bhio2V1Rhr5Iafl5FFtDPY8E.'
    SITE = 'http://api.brightcove.com/services/library'

    def self.method_missing(method_id, *args)
      args[0] ||= { }
      args[0].merge!({ :command => method_id, :token => TOKEN })
      get SITE, args[0]
    end

    def self.get(url, params)
      http_client = HTTPClient.new
      result = http_client.get(url, params)
      content = nil
      begin
        content = JSON.parse(result.content)
      rescue Exception => e
        Rails.logger.error e.message
      end
      return content
    end

 end

Basically all the magic relies in the method_missing that would convert any call to the Brightcove::ReadProxy module in the format accepted by the API, without having to define every API method and maintaining the Rails like finders syntax. We also used the httpclient gem to simplify the GET request calls and the json gem to parse the result of the call.

I’m not saying that this is the best usage, but i think in this particular situation it suits pretty well.

What do you think?

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

sebastian.martinez

Drag & Drop Sortable Lists 0

Posted by sebastian.martinez
on Monday, July 27

Time has come for us to make a sortable list, and let’s face it, drag&drop are the prettiest ones. So, let me explain how to proceed.

Suppose you have a playlist with many videos, and want to establish an order on which they will be played. First thing you will need is to add a ‘position’ attribute to your Video model. To do that, we’ll generate a migration first:

script/generate migration add_position_to_videos position:integer

Now just run rake db:migrate.

View Code

First thing you need to make sure, is that you have the prototype and scriptaculous libraries in your application. Now let’s see how your index.html.erb file should look like
   <h1>Videos</h1>  
   <ul id="videos">  
     <% @videos.each do |video| %>  
       <% content_tag_for :li, video do %>  
         <%= link_to video.name, video_path(video) %>  
       <% end %>  
     <% end %>  
   </ul>  
   <%= link_to "New Video, new_video_path %>  

Now all we have to do to make the list sortable is add the sortable_element helper method to the index view. You need to pass it the id of the element you want to be sortable, and a URL that is called via an AJAX request so that the updated positions can be stored in the database.

   <%= sortable_element('videos', :url => 'sort_videos_path') %>  

The videos in the list can now be dragged and dropped, but the new order isn’t persisted back to the database. So let’s code the sort method in the videos_controller.rb

   def sort  
     params[:videos].each_with_index do |id, index|  
       Video.update_all([’position=?’, index+1], [’id=?’, id])  
     end  
     render :nothing => true  
   end  

And that’s it. We are now updating the position of the videos. Most probably, we would want now our videos to be shown in the index in the correct order, so we just need to touch the index method on the controller a bit.

   def index  
     @videos = Video.all(:order => ’position’)  
   end  

At this point we are almost set to go…did you guess what’s missing? Yes, we need to add the route to the sort method :)

   map.resources :videos, :collection => { :sort => :post}  

The line above adds the new action and makes it a POST request, which is the type that our AJAX call uses when making XMLHTTP requests. And that’s pretty much it. Now you have a fully functional sortable list of videos.

There are some more tricks we can use to improve this, for example, the sortable_element helper receives one option called ‘handle’. What’s this? The way to specify the draggable area of the element. This I think is the most used one. Let’s give it a try:

We should first add a span tag on the index view:
 <% content_tag_for :li, video do %>  
   <span class="handle">[drag]</span>  
   <%= link_to video.name, video_path(video) %>  
 <% end %> 

Note the span has a ‘handle’ class, and that’s what you are going to set as draggable, using the :handle option:

   <%= sortable_element(’videos’), :url => sort_videos_path, :handle => ’handle’ %>  
You can style this a bit with a CSS, adding a line similar to this one:
 li .handle { color: #777; cursor: move; font-size: 12px; } 

Beautiful….just beautiful…

Ok, we have this magical sortable list now, but we added a ‘position’ field to videos…we can handle this manually when creating or updating the videos, or we can use the acts_as_list plugin, which I recommend. Just install it by typing

script/plugin install git://github.com/rails/acts_as_list.git
in your console. All configuration this needs to work is to add acts_as_list in our model, that should look like this:

   class Video < ActiveRecord::Base  
     acts_as_list  
   end  

Congrats!!! Enjoy your sortable list !!!

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

jose.costa

RVideo for video processing and inspection 2

Posted by jose.costa
on Monday, July 20

At WyeWorks headquarters, every once in a while, we come across some project that needs a media edition/transcoding solution to build into. This was the case of our latest project in which we built a pretty simple interface with Brightcove, a powerful video platform on which we may write something about it in our forthcoming posts, but it’s not the point right now.

Turns out to be that Brightcove recommends that files should be encoded using either H.264 or VP6. As usual, we ask ffmpeg for salvation when we need to transcode media files and this was not the exception. But we didn’t want to transcode just every file nor make the choice based on the file’s extension. We wanted a way to check the current file encoding.

Searches made at that time lead us to think that the usual way to get a media file encoding is by running:

ffmpeg -i 

which i must say that it’s pretty ugly for me since that command it’s supposed to be used for conversion and as far as i know it doesn’t offer some flag to get only the file information. In fact, that command returns an error (but still prints the information we need):

jose:~$ ffmpeg -i barsandtone.flv 
FFmpeg version 0.5-svn17737+3:0.svn20090303-1ubuntu6, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --extra-version=svn17737+3:0.svn20090303-1ubuntu6 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --disable-stripping --disable-vhook --enable-libdc1394 --disable-armv5te --disable-armv6 --disable-armv6t2 --disable-armvfp --disable-neon --disable-altivec --disable-vis --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 0 / 52.20. 0
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Apr 10 2009 23:18:41, gcc: 4.3.3
Input #0, flv, from 'barsandtone.flv':
  Duration: 00:00:06.00, start: 0.000000, bitrate: 505 kb/s
    Stream #0.0: Video: vp6f, yuv420p, 360x288, 409 kb/s, 1k tbr, 1k tbn, 1k tbc
    Stream #0.1: Audio: mp3, 44100 Hz, stereo, s16, 96 kb/s
  Must supply at least one output file

The information we need is the video codec (in this case vp6f) to determine if we need to transcode it.

Another thing to mention is that nothing that you see as the output there outputs to the stdout. No rocket science needed but i was kind of lazy to parse this from scratch. Lucky for me, there was this not so new rvideo gem (still unknown for me) that made me save some precious time.

RVideo still relies in ffmpeg command and also it’s internal work to get the information we need involves that same output parsing, you just don’t have to do it yourself. After installation (not covered here, just check rvideo readme file), you can do things as shown below:

inspector = RVideo::Inspector.new(:file => file.path)
inspector.video_codec
=> "vp6f" 
inspector.duration
=> 6000
inspector.audio_codec
=> "mp3"

We’ve just made a tiny introduction to inspection. I’m not covering video processing. Check rvideo for more details! Enjoy!

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

santiago.pastorino

Paperclip file rename 0

Posted by santiago.pastorino
on Tuesday, July 14

While developing an application with Sebastián that allow users to upload videos with some file name restrictions, meaning that it must contain only A-Z and 0-9 digits, underscores (_) as a valid component as well, and also the name must be preceded by it’s own #id, we came up with the need of applying this custom filter to each uploaded video. After doing some research on paperclip source code and internet tutorials, we suggest the following solution:

class Video < ActiveRecord::Base
  has_attached_file :video,
    :path => ":rails_root/public/system/:attachment/:id/:style/:normalized_video_file_name",
    :url => "/system/:attachment/:id/:style/:normalized_video_file_name" 

  Paperclip.interpolates :normalized_video_file_name do |attachment, style|
    attachment.instance.normalized_video_file_name
  end

  def normalized_video_file_name
    "#{self.id}-#{self.video_file_name.gsub( /[^a-zA-Z0-9_\.]/, '_')}" 
  end
end
What are we doing here? Easy, in has_attached_file we edit the way paperclip returns the path and url by default, the most relevant components when saving and loading the file in order to display it. Paperclip default values are:
path default => ":rails_root/public/system/:attachment/:id/:style/:filename" 
url default => "/system/:attachment/:id/:style/:filename"

Values preceded by ’:’ are the standard interpolations paperclip has. For further information on this visit http://wiki.github.com/thoughtbot/paperclip/interpolations.

What we did was change * with *:normalized_video_file_name in both path and url, being the second a custom interpolation and then added the ‘normalized_video_file_name’ method to video.rb.

By doing this we not only achieve a way for paperclip to handle the file by this normalized way, but also have a method to access the normalized file name, plus being able to access the original file name through paperclip video_file_name method.

So remember on video_file_name you have the uploaded filename and on normalized_video_file_name you have the server filename.

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

sebastian.martinez

Rails delegate method 4

Posted by sebastian.martinez
on Thursday, June 04

Delegation is a feature Rails introduced in it’s 2.2 version, and in my opinion are quite useful and somehow something we don’t see too much around. The concept of delegation is to take some methods and send them off to another object to be processed.

Let me explain this with a brief example:

Suppose you have a User class for anyone registered on your site, and a Customer class for those who have actually placed orders:

class User < ActiveRecord::Base  
   belongs_to :customer  
end  

class Customer < ActiveRecord::Base  
   has_one :user  
end

As for now, if you are in a Customer instance, you can get their User information doing @customer.user.name, or @customer.user.email. Delegation allows you to simplify this:

class User < ActiveRecord::Base  
   belongs_to :customer  
end  

class Customer < ActiveRecord::Base  
   has_one :user  
   delegate :name, :name=, :email, :email=, :to => :user  
end

Now you can refer to @customer.name and @customer.email to retrieve and set values for those attributes directly. Pretty nice, huh?

We are now working on some code to make possible to inherit behaviour, along with polymorphic associations, so when you create a Cutomer, the User gets created as well with the data you provided when creating the customer, and so on.

So keep posted, for there will be more to come…

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

jose.costa

Wildcard search with Thinking Sphinx 1

Posted by jose.costa
on Monday, April 20

Right after starting with Thinking Sphinx, it was quite hard to find a concise guide on how to enable wildcard search. For those out there who might not know, Sphinx searches default to matching whole words, not partial ones, so you won’t get any results of you search, for example, for one letter or part of a name. So, how to get around this?? .. well .. Sphinx provides wildcard search and below is how you can enable this with Thinking Sphinx

How wildcard search works in Sphinx?

There are basically three settings that rule the wildcard search world:
  • enable_star
  • min_prefix_len
  • min_infix_len

enabled_star is required plus one of the other two for settings for enabling either prefix or infix search (can’t have both, at least on the same index).

enable_star

This settings enables “star-syntax”, or wildcard syntax (means that ’*’ can be used at the start and/or end of the keyword), when searching through indexes which were created with prefix or infix indexing enabled. The star will match zero or more characters.

It only affects searching; so it can be changed without reindexing by simply restarting searchd.

For example, assume that the index was built with infixes and that enable_star is 1. Searching should work as follows:

  1. “abcdef” query will match only those documents that contain the exact “abcdef” word in them.
  2. “abc*” query will match those documents that contain any words starting with “abc” (including the documents which contain the exact “abc” word only);
  3. ”*cde*” query will match those documents that contain any words which have “cde” characters in any part of the word (including the documents which contain the exact “cde” word only).
  4. ”*def” query will match those documents that contain any words ending with “def” (including the documents that contain the exact “def” word only).

min_prefix_len

Minimum word prefix length to index. Optional, default is 0 (do not index prefixes). For instance, indexing a keyword “example” with min_prefix_len=3 will result in indexing “exa”, “exam”, “examp”, “exampl” prefixes along with the word itself. Searches against such index for “exam” will match documents that contain “example” word, even if they do not contain “exam” on itself. Too short prefixes (below the minimum allowed length) will not be indexed.

min_infix_len

Infix indexing allows to implement wildcard searching by ‘start*’, ’*end’, and ’*middle*’ wildcards. When mininum infix length is set to a positive number, indexer will index all the possible keyword infixes (ie. substrings) in addition to the keywords themselves. Too short infixes (below the minimum allowed length) will not be indexed.

For instance, indexing a keyword “test” with min_infix_len=2 will result in indexing:

  • “te”
  • “es”
  • “st”
  • “tes”
  • “est”

infixes along with the word itself. Searches against such index for “es” will match documents that contain “test” word, even if they do not contain “es” on itself.

Drawbacks :(

Indexing prefixes will make the index grow significantly (because of many more indexed keywords), and will degrade both indexing and searching times. Also, there’s no automatic way to rank perfect word matches higher in a prefix index, but there’s a number of tricks to achieve that (setup to indexes or extended-mode queries. Read more here).

Show me some code!

So, here it’s how you can set a wildcard search on a particular index for any of your models. In this case i’m setting an infix search.

class Article < ActiveRecord::Base
  ....
  ....

  define_index do
    indexes title, body, author
    set_property :enable_star => 1
    set_property :min_infix_len => 3
  end

  ....
  ....
end

You can also set these properties in your sphinx.yml settings file under config/ folder for any environment you want. It might look like this:

production:
  enable_star: 1
  min_infix_len: 3

and it will apply to all of your indexes.

Re-index your data and restart the sphinx deamon (remember i’m using Thinking Sphinx, so i have a set of nice and short rake tasks to achieve this).

rake ts:stop
rake ts:in
rake ts:start
Now you should be able to fire searches like this:
Article.search "Sphinx" 
Article.search "Think*" 
Article.search "*inx*" 

And many more cool stuff that’s beyond the scope of this post :) Enjoy!

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

sebastian.martinez

Parsing an OPML with Ruby 0

Posted by sebastian.martinez
on Saturday, February 21

And Ruby just doesn’t stop surprising us!! In the past we have to deal with XML files and parse them, incredibly easy task using Hpricot library. Now the turn was for OPML (Outline Processor Markup Language) files. In case you are not familiar with this type of files, its most common use is to exchange lists of web feeds between web feed aggregators.

We found this function to parse the OPML document recursively preserving its structure in the desktop weblog, that does the job of extracting the feeds, and modified it a bit. Now it returns a hash containing the title of the articles as keys, and its links as values.

Here’s the function:
def self.parse_opml(opml_node, parent_names=[])
    feeds = {}
    opml_node.elements.each('outline') do |el|
      if (el.elements.size != 0)
        feeds.merge!(parse_opml(el, parent_names + [el.attributes['text']]))
      end
      if (el.attributes['xmlUrl'])
        feeds[el.attributes['title']] = el.attributes['xmlUrl']
      end
    end
    return feeds
  end
All you have to do is call it this way:
require 'rexml/Document'

opml = REXML::Document.new(File.read('my_feeds.opml'))
feeds = parse_opml(opml.elements['opml/body'])

Pretty easy, huh? Try it out and leave your comments…

|

If you have found this material to be useful, you might
consider recommending me on Working With Rails.