Incompatibility with RABL
I'm using RABL to generate the output for an API in JSON. Along side this I'm also exposing an image representation of a resource on the same RESTful end point.
I just plugged in sinatra-respond_to to enable me to respond to the intention to serve JSON and an image on the same resource and currently have something like the following...
get "/:slug", provides: [:json, :png] do
@model = Thing.find_by_slug params[:slug]
if @model
respond_to do |format|
format.json { rabl :"things/show" }
format.png { send_file "my_thing.png" }
end
else
404
end
end
This however is breaking my tests with responses coming back with 404 status codes where I have success if I remove the respond_to block.
I've tracked the problem to this line...
args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
The result of #{args[1]}.#{format} is things/show.application/json which breaks the template resolution further down the chain. Leaving the arg unchanged as things/show fixes things and all tests pass.
The RABL template is as follows...
# show.rabl
object @model
attributes :something_important, :something_else
I've not investigated further than this but I'd guess that the code in respond_to.rb is intended to look for templates named like thing/show.json and things.xml. My RABL templates are simply named show.rabl as I wish different formats to produce the same output structure and splitting them would not be DRY.
I'd have submitted a pull request with the change I made but am not sure of the consequences and the reason sinatra-respond_to changes the arg in the first place (the comment describes storing the original value due to Sinatra reusing the render call for layout but I'm not clear what it's describing).
Maybe we could check if RABL is defined and bypass this line if it is?