Sha256: 849e7ff9b7e3e2c751fcbb5783e6026dcfc44f14837aad74aedb05aaf517c1ac

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

class Rack::App::Endpoint

  attr_reader :properties

  def initialize(properties)
    @properties = properties
    @logic_block = properties[:user_defined_logic]
    @serializer = properties[:serializer] || default_serializer
    @api_class = properties[:app_class]

    @path_params_matcher = {}
  end


  def execute(request_env)

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

    request_handler = @api_class.new

    request_handler.request = request
    request_handler.response = response
    request.env['rack.app.path_params_matcher']= @path_params_matcher.dup

    call_return = request_handler.instance_exec(&@logic_block)

    return call_return if is_a_rack_response_finish?(call_return)
    add_response_body_if_missing(call_return, response)

    return response.finish

  end

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

  protected

  def add_response_body_if_missing(call_return, response)
    response.write(String(@serializer.call(call_return))) if response.body.empty?
  end

  def is_a_rack_response_finish?(call_return)
    call_return.is_a?(Array) and
        call_return.length == 3 and
        call_return[0].is_a?(Integer) and
        call_return[1].is_a?(Hash)
  end

  def default_serializer
    lambda { |o| o.to_s }
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rack-app-0.12.1 lib/rack/app/endpoint.rb
rack-app-0.12.0 lib/rack/app/endpoint.rb
rack-app-0.11.0 lib/rack/app/endpoint.rb
rack-app-0.10.1 lib/rack/app/endpoint.rb
rack-app-0.9.2 lib/rack/app/endpoint.rb