Skip to content

Instantly share code, notes, and snippets.

@tobi
Created January 6, 2010 20:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save tobi/270594 to your computer and use it in GitHub Desktop.
Save tobi/270594 to your computer and use it in GitHub Desktop.
class JsonHax
def initialize(app)
@app = app
end
def call(env)
if env['CONTENT_TYPE'] == 'application/json'
env['CONTENT_TYPE'] = 'application/xml'
env['REQUEST_URI'].gsub!(/\.json/, '.xml')
env['PATH_INFO'].gsub!(/\.json/, '.xml')
status, headers, body = @app.call(env)
if headers['Content-Type'] =~ /application\/xml/
body_string = ''
body.each { |s| body_string << s }
body = Hash.from_xml(body_string).to_json
headers['Content-Type'] = 'application/json'
headers['Content-Length'] = body.length.to_s
end
return [status, headers, body]
end
@app.call(env)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment