lib/resty/child_path.rb in resty-generators-0.2.0 vs lib/resty/child_path.rb in resty-generators-0.3.0

- old
+ new

@@ -4,15 +4,28 @@ @app = app @rootpath = rootpath end def call(env) + is_json = (env['CONTENT_TYPE'] || env['HTTP_ACCEPT']) =~ /^application\/json/ + is_child = false ['REQUEST_PATH','PATH_INFO','REQUEST_URI','SCRIPT_NAME'].each do |key| - if(env[key] =~ /\.json([?].*)?$/) - env[key].gsub!(/^\/#{@rootpath}\//, "/") + if(is_json || env[key] =~ /\.json([?].*)?$/) + is_json = true + value = env[key] + if value + is_child = is_child || value =~ /^\/#{@rootpath}\// + value.gsub!(/^\/#{@rootpath}\//, "/") + end end end - @app.call(env) + status, headers, response = @app.call(env) + if is_child && headers['Location'] + uri = URI(headers['Location']) + uri.path.gsub!(/^\//, "/#{@rootpath}/") + headers['Location'] = uri.to_s + end + headers['Content-Type'] = 'application/json' if is_json + [status, headers, response] end - end end