Archive for Ruby

Facebooker: Creating an Iframe Application (Incorrect Signature)

I started developing a Facebook app for one of my existing websites.  Since, I already had a working website and I prefer normal html and js over fbml and JS over FBML and FBJS, I decided to take the IFrame route. While working with my app, I ran into this bizarre problem where clicking on any link after loading the facebook app, gave me an invalid Signature exception. After digging my way through the Facebooker (Rails plugin for Facebook app dev) code, I find that the expected signature is nil inside the "verify_signature function". Turns out you need to append all your links in a Facebook iframe app with the fb_sig params. So, I created a function in Application.rb and set it as a before_filter

before_filter :set_facebook_params
ensure_application_is_installed_by_facebook_user
 
protected
def set_facebook_params
@fb_params = params.inject({}) do |collection, pair|
collection[pair.first] = pair.second if pair.first =~ /^fb_sig/
collection
end
end

The above code will extract out all the fb_sig params from the url and store it in @fb_params. Now add this hash to every url that you create inside you Iframe. This was the fix that I came in the middle of the might, and I am sure this may not be the best fix. But this works. Let me know if there is another workaround.

Leave a Comment

action_mailer_tls and Ruby 1.8.7

If you are using action_mailer_tls rails pluin and you shifted to Ruby 1.8.7, your app might break. I recently ran into this problem, coz my host providers shifted to Ruby 1.8.7 and I started getting errors when I was trying to send emails. The problem is because the check_auth_args function has changed in Ruby 1.8.7. Now it requires only 2 args as opposed to 3. The action_mailer_tls plugin uses this function but uses three arguments. So, to get back running again, make the following changes:

1).  Go to your action_mailer_tls plugin installation
2). Inside the do_start function, replace the call to "check_auth_args" with the following snippet:

 
no_args = method(:check_auth_args).arity
if no_args == 2
  check_auth_args user, secret if user or secret
else
  check_auth_args user, secret, authtype if user or secret
end
 

Comments (3)

Ruby 1.8.7 String#chars compatibility problem with rails

Recently, I started getting weird "LoadError" with my Rails app on Dreamhost. I had an "Article" model which contained many "Photo" models. When the "Article" controller tried to refer to the photos in its object, I got the following error:
LoadError: Expected {full server path}/photo.rb to define Photo.

I had a photo.rb file which had class Photo defined. I was using attachment_fu plugin to implement photos. After some head scratching, I found that the error was in photo.rb file. Hence photo.rb was not loading correctly, and therefore a load error.

The actual error happened coz the Dreamhost guys had updated Ruby to 1.8.7 which has some incompatibility with Rails 2.0.2 that I was using. When I tried to create a Photo object on the command line, I got this error:
NoMethodError: undefined method '[]' for #<Enumerable:enumerator></code>

Ruby 1.8.7 has String#chars. This returns an Enumerator object but Rails 2.0.2 expects an ActiveSupport::MultiBye::Chars object.

We can avoid this conflict, by putting the following code in the config/initializers/ directory.

  1.  
  2. unless '1.9'.respond_to?(:force_encoding)
  3. String.class_eval do
  4. begin
  5. remove_method :chars
  6. rescue NameError
  7. # OK
  8. end
  9. end
  10. end
  11.  

Comments (3)

Insert comments in an erb file

How does one insert comments in an erb file. The normal http tag to insert comments do not work well when there are embedded ruby statements in the html.

<!-- <b>Hello</b><%= @article.author %> -->

The above statement will not stop the evaluation of article.author. Instead, @article.author will still be evaluated and placed as a comment in the generated html file. Say the author was Balpreet, then we have a html comment:

<!-- <b>Hello</b>Balpreet -->

So, what if we want to just comment stuff out, so that the embedded ruby statements are not evaluated at all. There are basically two ways that I think we can achieve this:

1. Insert block level comments using embedded ruby.
<%
=begin %>
<b>Hello</b><%= @article.author %><%
=end %>

2. Insert conditional
<% if false %>
<b>Hello</b><%= @article.author %>
<% end %>

Leave a Comment

Rfeedparser and Hpricot

RFeedParser is one of the ruby gems to parse rss feeds. Its a translation of Mark Pilgrims's Universal Feed Parser from Python into Ruby. It has a few dependencies and requires you to install a few other gems. The installation instructions are simple and the usage is simple.

One of the dependencies for this was hpricot. Everything was working fine till one day, I updated my hpricot gem and did a cleanup of my rubygems. I started getting an error:

balpreet-pankajs-macbook:digg balpreetpankaj$ ruby bloo.rb
/Library/Ruby/Site/1.8/rubygems.rb:142:in `activate': can't activate hpricot (= 0.6, runtime), already activated hpricot-0.6.164 (Gem::Exception)
from /Library/Ruby/Site/1.8/rubygems.rb:158:in `activate'
from /Library/Ruby/Site/1.8/rubygems.rb:157:in `each'
from /Library/Ruby/Site/1.8/rubygems.rb:157:in `activate'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:510:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:355:in `new_constants_in'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:510:in `require'

After a little digging arnd with rubygems, I figured out that the rfeedparser has a requirement of running with a hpricot gem with version = 0.6. I changed that to >= 0.6 and things started working fine. I was using version 0.9.951 of rfeedparser. If you use rfeedparser and run into a similar problem, this is how you fix it:

1) Update the gemspec for rfeedparser. Go to your gem installation dir. You can find this by doing:
gem env gempath
Then edit the $gemPATH/specifications/rfeedparser0.9.951.gemspec and update all references of hpricot from "=0.6" to ">=0.6".
2). Go to $GEMPATH/gems/rfeedparser-0.9.951/lib/rfeedparser.rb and again update the reference of hpricot.

This should get you rolling with all new versions of hpricot.

Comments (1)

Crawling the web with Ruby

Recently for one of my projects, I needed to write my own custom crawler to crawl some of my favorite blogs. I am a recent Ruby convert and I am totally in love with the language. So i decided to have the crawler in Ruby. After some Googling around, I found out this very neat crawler, Spider written in Ruby by Mike Burns. You can find the source code here. The code is currently maintained by John Nagro.

The crawler is simple and yet has support for Amazon SQS and memcahed, so that you can run multiple crawler processes at the same time.  There are also some drawbacks:

1. The urls ae just crawled in the order they are discovered ( breadth-first search). There is no way to specify the ordering in which the urls should be searched.

2. There is no HTML parsing on top of the crawled data.

The first drawback was not much of a problem for my purposes, coz I needed to crawl all the web pages of a blog. Order of crawling was not a deal for me. The crawler has a way in which you can specify the urls in a regex form which are to be crawled. This served my purposes.

I needed HTML parsing though. But this was easy. I just used the hpricot gem to do HTML parsing on top of the crawled data:

Here is a sample code, on how to crawl a particular blog using Spider and using hpricot to print out the meta content on each page.

  1.  
  2. require 'rubygems'
  3. require 'spider'
  4. require 'hpricot'
  5.  
  6. Spider.start_at('http://simpliflying.com/') do |s|
  7. # Limit the pages to just this domain.
  8. s.add_url_check do |a_url|
  9. a_url =~ %r{^http://simpliflying.com.*}
  10. end
  11.  
  12. # Handle 404s.
  13. s.on 404 do |a_url, err_code|
  14. puts "URL not found: #{a_url}"
  15. end
  16.  
  17. # Handle 2xx.
  18. s.on :success do |a_url, resp, prior_url|
  19. puts "#{a_url}: #{resp.code}"
  20. doc = Hpricot(resp.body)
  21. (doc/"meta").each do |meta|
  22. puts meta.attributes['content']
  23. end
  24. end
  25.  
  26. # Handle everything.
  27. s.on :any do |a_url, resp|
  28. puts "URL returned anything: #{a_url} with this code #{resp.code}"
  29. end
  30. end
  31.  

A very good Hpricot documentation is available here.

Leave a Comment