How-to add proxy support to Feedalizer ruby library

Here is a little code snippet that monkey-patch Feedalizer to let it grab web content through a HTTP proxy:

# HTTP proxy settings
HTTP_PROXY_HOST = "123.456.78.90"
HTTP_PROXY_PORT = 8080

# Calculate proxy URL
HTTP_PROXY_URL = "http://#{HTTP_PROXY_HOST}:#{HTTP_PROXY_PORT}"

# Monkey patch feedalizer to support page grabbing through a proxy
require 'feedalizer'
class Feedalizer
  # Backup original grab_page method
  alias_method :grab_page_orig, :grab_page
  # Define new grab_page() method with proxy support
  def grab_page(url)
    open(url, :proxy => HTTP_PROXY_URL) { |io| Hpricot(io) }
  end
end

This fix, written for a Ruby on Rails-based project, lay in the environment.rb file, but I wonder if this is the right place and the right way of doing it… Anyway, it works for me ! :)

Update: A post from Matthew Higgins’ blog that answer my question above has just shown up in my feed aggregator. What’s he telling us ? That I’m a naughty programmer :

Previous to 2.0, naughty developers pasted code at the bottom of environment.rb, and the config/initializer folder was a welcome convention to help organize this madness.

For your instance, the code in this post is extracted from an “old” (prior to RoR 2.0) project, thus explaining my naughtyness… ;)

6 thoughts on “How-to add proxy support to Feedalizer ruby library

  1. Apologies, I did not mean to be so harsh! Still, it would be better to move this into a feedalizer.rb file and just have

    require 'feedalizer'
    

    in environments.rb.

  2. Don’t worry Matthew, your statement was quite funny ! And it’s good to read someone that can speak his mind.

    And thanks for your comment and the tip. I’ll probably rename the feedalizer.rb you suggest by something like monkey-patch-feedalizer.rb, as I like having very explicit names that pop up, especially when they belongs to hacks and workarounds…

  3. Hi,

    Would you be willing to share your Rails code, so I can see how you made feedalizer work in the context of a Rails app? I’m having trouble getting that to work, and (afaik) there’s not really any documentation for feedalizer (other than just using the .methods method and trying them each out to see what they do, which still doesn’t solve my problem of context).

    Thanks!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.