Here is a little code snippet which 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 theconfig/initializerfolder 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 archived comments
Comments are closed. These were posted on Disqus, and are archived here.
-
Apologies, I did not mean to be so harsh! Still, it would be better to move this into a
feedalizer.rbfile and just haverequire 'feedalizer'
in
environments.rb.-
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.rbyou suggest by something likemonkey-patch-feedalizer.rb, as I like having very explicit names that pop up, especially when they belongs to hacks and workarounds...
-
-
Hi,
Would you be willing to share your Rails code, so I can see how you made
feedalizerwork in the context of a Rails app? I'm having trouble getting that to work, and (afaik) there's not really any documentation forfeedalizer(other than just using the.methodsmethod and trying them each out to see what they do, which still doesn't solve my problem of context).Thanks!
-
Whoops... forgot to check "Notify me of followup comments via e-mail"
-
I think the best way to make Feedalizer use an HTTP proxy is to set the
http_proxyenvironment variable (thatOpenURIrespects).@B: You're supposed to study the source code. (It's only 49 SLOC.) I know I'm a bit late reading this, but I wrote you an example:
http://code.vemod.net/svn/misc/feedalizer/trunk/examples/slashdot_feedalizer_controller.rb -
@B: I've just released the project in which I used Feedalizer under an Open-Source license. The code your looking for is available on GitHub.
-