googlecharts gem URI::InvalidURIError fixed

Posted by crux on 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: , , , ,

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. [...] a simple fix/hack for the googlecharts gemUhh!, this for sure is one of the more scary regular expressions you normally encounter. Its [...]

  2. tim Mon, 28 Jan 2008 00:40:12 UTC

    Many thanks! This did the job

  3. Matt Aimonetti Wed, 06 Feb 2008 08:42:59 UTC

    Googlecharts 1.0.0 fixes this problem. Thanks Crux for your blogpost. Too bad I didn’t see it earlier. I just released an update of my gem fixing the problem you mentioned. The bug fix is based on your recommendation.

    -Matt

  4. [...] Aimonetti released a 1.0.0 version of his googlecharts gem which includes my URI bugfix i blogged about earlier. Me haven’t had so many opensource contributions yet, feels good though.Technorati Tags: [...]

  5. Sytse Sijbrandij Thu, 04 Nov 2010 14:26:35 UTC

    Also consider using URI.escape(url), see http://www.ruby-doc.org/core/classes/URI/Escape.html

Comments