Sha256: c1eb61dc3ef343c6f2df7c0c652f1deefdb6561f1894241cda7fb49a12078919

Contents?: true

Size: 935 Bytes

Versions: 5

Compression:

Stored size: 935 Bytes

Contents

module Resty
  class ChildPath
    def initialize(app, rootpath)
      @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(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
      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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
resty-generators-0.4.0 lib/resty/child_path.rb
resty-generators-0.3.3 lib/resty/child_path.rb
resty-generators-0.3.2 lib/resty/child_path.rb
resty-generators-0.3.1 lib/resty/child_path.rb
resty-generators-0.3.0 lib/resty/child_path.rb