Blake Hilscher

Thoughts on ruby development.

Grape API Formatter for Sending Inline Data.

I needed to add a .png formatter to the quandl Grape API.

The png is generated using phantomjs, so it needed to be sent down inline.

1
2
3
4
5
6
7
content_type :png, "image/png"

formatter :png, lambda { |response, env|
    path = response.try(:object).try(:to_png)
    rack = Rack::Response.new(File.read(path) , 200, { "Content-type" => "image/png" }).finish
    rack[2].body[0]
}