ruby - Download an image from a URL? -
i trying use http::get download image of google chart url created.
this first attempt:
failures_url = [title, type, data, size, colors, labels].join("&") require 'net/http' net::http.start("http://chart.googleapis.com") { |http| resp = http.get("/chart?#{failures_url") open("pie.png" ,"wb") { |file| file.write(resp.body) } }
which produced empty png file.
for second attempt used value stored inside failure_url
inside http.get()
call.
require 'net/http' net::http.start("http://chart.googleapis.com") { |http| resp = http.get("/chart?chtt=builds+in+the+last+12+months&cht=bvg&chd=t:296,1058,1217,1615,1200,611,2055,1663,1746,1950,2044,2781,1553&chs=800x375&chco=4466aa&chxl=0:|jul-2010|aug-2010|sep-2010|oct-2010|nov-2010|dec-2010|jan-2011|feb-2011|mar-2011|apr-2011|may-2011|jun-2011|jul-2011|2:|months|3:|builds&chxt=x,y,x,y&chg=0,6.6666666666666666666666666666667,5,5,0,0&chxp=3,50|2,50&chbh=23,5,30&chxr=1,0,3000&chds=0,3000") open("pie.png" ,"wb") { |file| file.write(resp.body) } }
and, reason, version works though first attempt had same data inside http.get()
call. know why is?
solution:
after trying figure why happening found "how download binary file on http?".
one of comments mentions removing http://
in net::http.start(...)
call otherwise won't succeed. sure enough after did this:
failures_url = [title, type, data, size, colors, labels].join("&") require 'net/http' net::http.start("chart.googleapis.com") { |http| resp = http.get("/chart?#{failures_url") open("pie.png" ,"wb") { |file| file.write(resp.body) } }
it worked.
i'd go after file using ruby's open::uri:
require "open-uri" file.open('pie.png', 'wb') |fo| fo.write open("http://chart.googleapis.com/chart?#{failures_url}").read end
the reason prefer open::uri handles redirects automatically, when google makes change back-end , tries redirect url, code handle magically. handles timeouts , retries more gracefully if remember right.
if must have lower level control i'd @ 1 of many other http clients ruby; net::http fine creating new services or when client doesn't exist, i'd use open::uri or besides net::http until need presents itself.
the url:
http://chart.googleapis.com/chart?chtt=builds+in+the+last+12+months&cht=bvg&chd=t:296,1058,1217,1615,1200,611,2055,1663,1746,1950,2044,2781,1553&chs=800x375&chco=4466aa&chxl=0:|jul-2010|aug-2010|sep-2010|oct-2010|nov-2010|dec-2010|jan-2011|feb-2011|mar-2011|apr-2011|may-2011|jun-2011|jul-2011|2:|months|3:|builds&chxt=x,y,x,y&chg=0,6.6666666666666666666666666666667,5,5,0,0&chxp=3,50|2,50&chbh=23,5,30&chxr=1,0,3000&chds=0,3000
makes uri upset. suspect seeing characters should encoded in urls.
for documentation purposes, here uri says when trying parse url as-is:
uri::invalidurierror: bad uri(is not uri?)
if encode uri first, successful parse. testing further using open::uri shows able retrieve document @ point , returns 23701 bytes.
i think appropriate fix problem if of characters not acceptable uri and out of rfc.
just information, addressable::uri gem great replacement built-in uri.
Comments
Post a Comment