ruby google charts api wrapper, RFC2396 and ruby open-uri woes
UPDATE:found a simple fix/hack for the googlecharts gem
Uhh!, this for sure is one of the more scary regular expressions you normally encounter. Its URI::REGEXP::PATTERN::ABS_URI from uri/common.rb:137. Looks complete, doesn’t it? And still it fails to grasp my seemingly valid Google Charts URL which is: http://chart.apis.google.com/chart?chdl=requests(cached)|requests&chd=s:Fb9JJfgZ,Fb9KJfgq&cht=lc&chs=300×200
It’s the googlecharts gem which creates this URL here(not me!), but than bails out when trying to download an image from it.
/opt/local/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?):...Now, who is right? Google? The URI RFC2396? The googlecharts gem? Debugging such things are the less thrilling moments in life. Besides that, even when bailing out on this one, the googlecharts gem feels good. It offers efortless chart creation and basic cases are already covered, the advanced stuff will follow i think. Go check it out when in search for an easy charting solution. Googlecharts gem felt better to me than the gchartrb which is another wrapper for the same service, but your milage may vary.
have fun
Technorati Tags: ruby, googlecharts, api
Trackbacks
Use this link to trackback from your own site.
You’re not alone with this problem … everytime I write a webbot I encounter this problem way too many times. That regexp looks scary to start poking around in though ;)
[...] 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({ } | ^ [ ] )<br />query_params.each do |p|<br /> unwise.each { |c| p.gsub!(c, “%#{c[0].to_s(16).upcase}”) }<br />end<br /><br />jstize(@@url + query_params.join(’&’))</code></pre>this escapes the ‘|’ (pipe) characters which were causing the exception, like this one:<br /><code>/opt/local/lib/ruby/1.8/uri/common.rb:436:insplit’: bad URI(is not URI?): http://chart.apis.google.com/chart?chdl=requests(cached)|requests&chd=s:Fb9JJfgZ,Fb9KJfgq&cht=lc&chs=300×200 (URI::InvalidURIError)Technorati Tags: ruby, googlecharts, google, gem, fix [...]