Class: Dandy::SafeExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/dandy/safe_executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(container, dandy_config, view_factory) ⇒ SafeExecutor

Returns a new instance of SafeExecutor.



3
4
5
6
7
# File 'lib/dandy/safe_executor.rb', line 3

def initialize(container, dandy_config, view_factory)
  @container = container
  @dandy_config = dandy_config
  @view_factory = view_factory
end

Instance Method Details

#execute(route, headers) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dandy/safe_executor.rb', line 9

def execute(route, headers)
  chain = Chain.new(@container, @dandy_config)

  begin
    result = chain.run_commands(route.commands, route.last_command)
    if route.view
      result = @view_factory.create(route.view, headers['Accept'], {keys_format: headers['Keys-Format'] || 'snake'})
    end

    body = result.is_a?(String) ? result : format_response(result, headers)
  rescue Exception => error
    @container
      .register_instance(error, :dandy_error)
      .using_lifetime(:scope)
      .bound_to(:dandy_request)

    action = @container.resolve(route.catch.name.to_sym)
    body = format_response(action.call, headers)
  end

  body
end