Created
July 27, 2018 18:06
-
-
Save TrevorHinesley/fa8952b4295c3f6afefdf0777f3061d8 to your computer and use it in GitHub Desktop.
Fast JSON API MIME type configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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