I’ve been experimenting using the following code to retrieve HTML from various sites:
require 'open-uri'
class DemoController < ApplicationController
def index
open('http://www.google.co.uk/search?q=ruby') {
|html| render_text html.read()
}
end
end
If you try it, you’ll get the HTML for a Google results page. So far so good.
However, this is where I’m stuck. One site I’ve been trying to access passes a GUID in the querystring and it’s wrapped in curly brackets. So I updated the ‘open’ line to say:
open('http://www.google.co.uk/search?q={3F2504E0-4F89-11D3-9A-0C-03-05-E8-2C-33-01}') {then refreshed my browser; only to be presented with the following error:
URI::InvalidURIError in DemoController#index
bad URI(is not URI?): http://www.google.co.uk/search?q={3F2504E0-4F89-11D3-9A-0C-03-05-E8-2C-33-01}
RAILS_ROOT: /code/public/../config/..
/usr/local/lib/ruby/1.8/uri/common.rb:432:in `split'
/usr/local/lib/ruby/1.8/uri/common.rb:481:in `parse'
/usr/local/lib/ruby/1.8/open-uri.rb:85:in `open'
#{RAILS_ROOT}/app/controllers/demo_controller.rb:5:in `index'
From what I’ve read, curly brackets aren’t allowed in a Ruby URI (despite my browser being capable of using them). I’ve tried encoding them but then the page is called with the encoded version, which in my case, the site isn’t expecting and it doesn’t work.
What I’m looking for is a way to allow an ‘unsafe’ URI to be called. Does anyone know if that’s possible?
Have you tried to “escape” the offending characters? Like this:
open(‘http://www.google.co.uk/search?q=\{3F2504E0-4F89-11D3-9A-0C-03-05-E8-2C-33-01\}’)
Comment by Jason — March 16, 2007 @ 4:52 pm