Sha256: 252fff685a6e5cd9e3206a679b25604e7f8f9b5a2ccf4082ef18d1f00f056c8f

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

class Rack::App::Endpoint

  attr_reader :properties

  LAST_MODIFIED_HEADER = "Last-Modified".freeze

  def initialize(properties)
    @properties = properties

    @error_handler = properties[:error_handler]
    @serializer = properties[:serializer]
    @api_class = properties[:app_class]
    @headers = properties[:default_headers]

    @path_params_matcher = {}
    @endpoint_method_name = register_method_to_app_class(properties[:user_defined_logic])
  end

  def call(request_env)

    request = Rack::Request.new(request_env)
    response = Rack::Response.new

    set_response_body(response, get_response_body(request, response))
    return response.finish

  end

  def get_response_body(rack_request, rack_response)

    request_handler = @api_class.new
    set_default_headers(rack_response)
    request_handler.request = rack_request
    request_handler.response = rack_response
    rack_request.env['rack.app.path_params_matcher']= @path_params_matcher.dup
    return @error_handler.execute_with_error_handling_for(request_handler, @endpoint_method_name)

  end

  def register_path_params_matcher(params_matcher)
    @path_params_matcher.clear
    @path_params_matcher.merge!(params_matcher)
  end

  protected

  def set_response_body(response, result)
    if result.is_a?(::Rack::App::File::Streamer)
      response.length += result.length
      response.headers[LAST_MODIFIED_HEADER]= result.mtime
      response.body = result

    else
      response.write(String(@serializer.serialize(result)))

    end
    nil
  end

  def set_default_headers(response)
    @headers.each do |header, value|
      response.header[header]=value
    end
  end

  def register_method_to_app_class(proc)
    method_name = ::Rack::App::Utils.uuid
    @api_class.__send__(:define_method, method_name, &proc)
    return method_name
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-app-0.22.0 lib/rack/app/endpoint.rb
rack-app-0.21.0 lib/rack/app/endpoint.rb
rack-app-0.20.0 lib/rack/app/endpoint.rb