Skip to content

Instantly share code, notes, and snippets.

@TrevorHinesley
Created July 27, 2018 18:06
Show Gist options
  • Save TrevorHinesley/fa8952b4295c3f6afefdf0777f3061d8 to your computer and use it in GitHub Desktop.
Save TrevorHinesley/fa8952b4295c3f6afefdf0777f3061d8 to your computer and use it in GitHub Desktop.
Fast JSON API MIME type configuration
# JSONAPI_MEDIA_TYPE = 'application/vnd.api+json'
Mime::Type.register JSONAPI_MEDIA_TYPE, :jsonapi
ActionController::Renderers.add :jsonapi do |json, options|
json = json.to_json(options) unless json.is_a?(String)
self.content_type ||= Mime[:jsonapi]
self.response_body = json
end
ActionDispatch::Request.parameter_parsers[:jsonapi] = lambda do |body|
data = JSON.parse(body)
data = { _json: data } unless data.is_a?(Hash)
data.with_indifferent_access
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment