Class: Lam::Process::ControllerProcessor

Inherits:
BaseProcessor show all
Defined in:
lib/lam/process/controller_processor.rb

Instance Attribute Summary

Attributes inherited from BaseProcessor

#context, #event, #handler

Instance Method Summary collapse

Methods inherited from BaseProcessor

#initialize

Constructor Details

This class inherits a constructor from Lam::Process::BaseProcessor

Instance Method Details

#runObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lam/process/controller_processor.rb', line 5

def run
  # Use the handler value (ie: posts.create) to deduce the user's business
  # code to require and run.
  deducer = ProcessorDeducer.new(handler)
  path = deducer.controller[:path]
  code = deducer.controller[:code]

  begin
    require path  # require "app/controllers/posts_controller.rb"
    # Puts the return value of user's code to stdout because this is
    # what eventually gets used by API Gateway.
    # Explicitly using $stdout since puts redirected to $stderr.

    # result = PostsController.new(event, context).create
    result = instance_eval(code, path)

    # JSON.dump is pretty robust.  If it cannot dump the structure into a
    # json string, it just dumps it to a plain text string.
    $stdout.puts JSON.dump(result) # only place where we write to stdout.
  rescue Exception => e
    # Customize error message slightly so nodejs shim can process the
    # returned error message.
    # The "RubyError: " is a marker that the javascript shim scans for.
    $stderr.puts("RubyError: #{e.class}: #{e.message}") # js needs this as the first line
    backtrace = e.backtrace.map {|l| "  #{l}" }
    $stderr.puts(backtrace)
    # $stderr.puts("END OF RUBY OUTPUT")
    exit 1 # instead of re-raising to control the error backtrace output
  end
end