Class: Silicon::Request

Inherits:
Object
  • Object
show all
Includes:
Hypo::Scope
Defined in:
lib/silicon/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(route_matcher, container, chain_factory, view_factory) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
# 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

Instance Method Details

#handle(rack_env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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