class Silicon::Request

Public Class Methods

new(route_matcher, container, chain_factory, view_factory) click to toggle source
# File lib/silicon/request.rb, line 8
def initialize(route_matcher, container, chain_factory, view_factory)
  @container = container
  @route_matcher = route_matcher
  @chain_factory = chain_factory
  @view_factory = view_factory
end

Public Instance Methods

handle(rack_env) click to toggle source
# File lib/silicon/request.rb, line 15
def handle(rack_env)
  create_scope

  path = rack_env['PATH_INFO']
  request_method = rack_env['REQUEST_METHOD']
  match = @route_matcher.match(path, request_method)
  content_type = rack_env['CONTENT_TYPE'] || 'application/json'

  if match.nil?
    result = [404, { 'Content-Type' => content_type }, []]
  else
    query = Rack::Utils.parse_nested_query(rack_env['QUERY_STRING']).symbolize_keys
    register_params(query, :silicon_query)

    data = rack_env['rack.parser.result']
    register_params(data, :silicon_data)

    chain = @chain_factory.create(match)
    chain.execute

    body = ''
    if match.route.view
      body = @view_factory.create(match.route.view, content_type)
    end

    status = @container.resolve(:silicon_status)
    result = [status, { 'Content-Type' => content_type }, [body]]
  end

  release

  result
end

Private Instance Methods

create_scope() click to toggle source
# File lib/silicon/request.rb, line 50
def create_scope
  @container
    .register_instance(self, :silicon_request)
    .using_lifetime(:scope)
    .bound_to(self)
end
register_params(params, name) click to toggle source
# File lib/silicon/request.rb, line 57
def register_params(params, name)
  unless params.nil?
    @container.register_instance(params, name)
      .using_lifetime(:scope)
      .bound_to(:silicon_request)
  end
end