Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oat, Rails Responders and hypermedia mime types #50

Closed
apsoto opened this issue Oct 9, 2014 · 3 comments · Fixed by #52
Closed

Oat, Rails Responders and hypermedia mime types #50

apsoto opened this issue Oct 9, 2014 · 3 comments · Fixed by #52

Comments

@apsoto
Copy link
Contributor

apsoto commented Oct 9, 2014

When doing the following in a Rails app (example)

# config/initializers/oat.rb
Mime::Type.register 'application/vnd.siren+json', :siren

ActionController::Renderers.add :siren do |resource, options|
  self.content_type ||= Mime[:siren]
  resource.to_json
end

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  respond_to :siren, :json

 def show
    respond_with UserSerializer.new(User.find(params[:id])
  end
end

Works great if the http request is json using the Accepts: application/json header or the format=json query parameter. However, if I request with the Siren mime type (Accepts: application/vnd.siren+json or format=siren query parameter), it fails with a standard rails error saying it can't find a template for the siren format.

After digging into it, the root of the problem is that the serializer needs to implement a to_siren method. If I add a to_siren method to my serializer, then the responder block is called and all is well.

I'm wondering if anyone is having success with Rails and using respond_with SERIALIZER AND also using the format's specific mime-type without implementing a to_FORMAT method?

Maybe I'm using responders incorrectly? I have not found any documentation that really explains how to do something like this for an API response without a template. I managed to find something that works thanks to the README in roar-rails

I'm thinking Oat should provide support for this. I've added such in my fork, but I want to verify it's not a problem localized to my app's specific implementation before I submit a PR.

@ismasan
Copy link
Owner

ismasan commented Oct 9, 2014

Hi. Good catch. I'm not using renderers myself (I also found the docs to be lacking, so opted not to use an under-documented feature), but I can definitely see the value of this (better adapter swapping on a per-request level).

Since Oat is not Rails-specific, I think this addition should belong in an hypothetical oat-rails gem, which I think has been discussed before elsewhere.

@apsoto
Copy link
Contributor Author

apsoto commented Oct 9, 2014

There's so little code needed, that it doesn't seem to warrant that effort at this time. How about I just add a section to the README about rails integration?

@ismasan
Copy link
Owner

ismasan commented Oct 9, 2014

Good solution :) Appreciated.

apsoto added a commit to apsoto/oat that referenced this issue Oct 9, 2014
apsoto added a commit to apsoto/oat that referenced this issue Oct 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants