#coding   #dirtyhack   #horror   #ruby

googlecharts gem URI::InvalidURIError fixed

January 12, 2008

Yesterday I was wondering who is right about the URI and as it turns out the culprit is the googlecharts gem. The RFC defines ‘UNWISE’ characters, the ruby URI implementation seem to correctly fail on them and the Google API defines the just the URI, not how it has to be encoded/escaped.

I posted the scary URI regex from the ruby open-uri package which fails to grasp the Google Charts URIs. Debugging such thing is no fun so i went for a quick fix instead and actually could find it in the googlecharts gem. Just replace:

jstize(@@url + query_params.join('&'))

from ...ruby/gems/1.8/gems/googlecharts-0.2.0/lib/gchart.rb with:

unwise = []#%w({ } |  ^ [ ] `)
query_params.each do |p|
   unwise.each { |c| p.gsub!(c, "%#{c[0].to_s(16).upcase}") }
end

jstize(@@url + query_params.join('&'))

this escapes the ‘|’ (pipe) characters which were causing the exception, like this one:

/opt/local/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): http://chart.apis.google.com/chart?chdl=requests(cached)|requests&chd=s:Fb9JJfgZ,Fb9KJfgq&cht=lc&chs=300x200 (URI::InvalidURIError)

Technorati Tags: , , , ,

share this: