On Mar 18, 2010, at 9:00 PM, Adam Z. wrote:
I’m not convinced that this approach will never work.
valid, or so I thought. I pass these kind of characters as parameters
get it to work. Also, the image URL they were using actually pointed
to
a PNG image, with a PNG extension, whereas the Google Chart URL will
return a PNG image.
I appreciate all the help!
You just need a URI that’s technically valid, not just one that
browsers understand. URI.parse and thus open-uri is rather more strict
than the other tools suggested.
Here’s an irb session that shows the right way to get a valid URI for
the chart you want.
$ irb -ruri -ropen-uri -rcgi
irb> orig =
‘http://chart.apis.google.com/chart?cht=bvg&chbh=a&chd=s:vttusty&chs=500x300&chxt=x,y&chxl=0
:|Sun|Mon|Tue|Wed|Thu|Fri|Sat|1:|0|2|4|6|8|10|12’
=>
“http://chart.apis.google.com/chart?cht=bvg&chbh=a&chd=s:vttusty&chs=500x300&chxt=x,y&chxl=0
:|Sun|Mon|Tue|Wed|Thu|Fri|Sat|1:|0|2|4|6|8|10|12”
irb> md = %r{(https?:)//((?i:[-a-z0-9.]+))(/[^?]+)??(.*)}.match(orig)
=> #MatchData:0x391070
irb> scheme, host, path, query_string = md.captures
=> [“http:”, “chart.apis.google.com”, “/chart”,
“cht=bvg&chbh=a&chd=s:vttusty&chs=500x300&chxt=x,y&chxl=0:|Sun|Mon|Tue|
Wed|Thu|Fri|Sat|1:|0|2|4|6|8|10|12”]
irb> query_string.split(‘&’).map{|pair| pair.split(‘=’)}
=> [[“cht”, “bvg”], [“chbh”, “a”], [“chd”, “s:vttusty”], [“chs”,
“500x300”], [“chxt”, “x,y”], [“chxl”, “0:|Sun|Mon|Tue|Wed|Thu|Fri|Sat|
1:|0|2|4|6|8|10|12”]]
irb> u = URI.parse “#{scheme}//#{host}”
=> #<URI::HTTP:0x37cf44 URL:http://chart.apis.google.com>
irb> u.path = path
=> “/chart”
irb> u.query = query_string.split(‘&’).map{|pair|
pair.split(‘=’)}.map{|n,v| “#{n}=#{CGI.escape(v)}”}.join(‘&’)
=> “cht=bvg&chbh=a&chd=s%3Avttusty&chs=500x300&chxt=x%2Cy&chxl=0%3A
%7CSun%7CMon%7CTue%7CWed%7CThu%7CFri%7CSat%7C1%3A
%7C0%7C2%7C4%7C6%7C8%7C10%7C12”
irb> open(u.to_s) do |chart|
?> File.open(‘chart.png’, ‘w’) {|f| f.write chart.read }
irb> end
=> 5445
irb> File.exist?(‘chart.png’)
=> true
Note that if you are building up the URI yourself, you could just
create it with valid escaping on the query string rather than pulling
it apart and escaping it as you put it back together.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]