Class: Hyperion::FakeServer::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Hyperion::Formats, Headers
Defined in:
lib/hyperion_test/fake_server/dispatcher.rb

Constant Summary

Constants included from Headers

Headers::ContentTypes

Instance Method Summary collapse

Methods included from Headers

#content_type_for, #format_for, #route_headers, #short_mimetype

Methods included from Hyperion::Formats

get_from, #read, #write

Methods included from Logger

#log_stub, #logger, #with_request_logging

Constructor Details

#initialize(rules) ⇒ Dispatcher

Returns a new instance of Dispatcher.



11
12
13
# File 'lib/hyperion_test/fake_server/dispatcher.rb', line 11

def initialize(rules)
  @rules = rules
end

Instance Method Details

#dispatch(mimic_route, request) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hyperion_test/fake_server/dispatcher.rb', line 15

def dispatch(mimic_route, request)
  rule = find_matching_rule(mimic_route, request)
  rule or return [404, {}, "Not stubbed: #{mimic_route.inspect} #{request.env}"]
  request = make_req_obj(request.body.read, request.env['CONTENT_TYPE'])
  response = rule.handler.call(request)
  if rack_response?(response)
    code, headers, body = *response
    [code, headers, write(body, :json)]
  else
    if rule.rest_route
      rd = rule.rest_route.response_descriptor
      [200, {'Content-Type' => content_type_for(rd)}, write(response, rd)]
    else
      # better to return a 500 than raise an error, since we're executing in the forked server.
      [500, {}, "An 'allow' block must return a rack-style response if it was not passed a RestRoute"]
    end
  end
end