Created
August 1, 2012 21:10
-
-
Save raedatoui/3230784 to your computer and use it in GitHub Desktop.
json responses
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
def render_json_response(type, hash) | |
unless [ :ok, :error ].include?(type) | |
raise "Invalid json response type: #{type}" | |
end | |
default_json_structure = { | |
:status => type, | |
:data => nil, | |
:error => nil, | |
:message => nil}.merge(hash) | |
render_options = {:json => default_json_structure.reject{|k,v| v.nil?}} | |
render_options[:status] = 400 if type == :error | |
render(render_options) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment