Class: LambdaConsole::Interact

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda-console-ruby/interact.rb

Constant Summary collapse

DIG =
[NAMESPACE, 'interact']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Interact

Returns a new instance of Interact.



17
18
19
# File 'lib/lambda-console-ruby/interact.rb', line 17

def initialize(event)
  @event = event
end

Class Method Details

.handle(event) ⇒ Object



11
12
13
# File 'lib/lambda-console-ruby/interact.rb', line 11

def handle(event)
  new(event).interact
end

.handle?(event) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/lambda-console-ruby/interact.rb', line 7

def handle?(event)
  !!event.dig(*DIG)
end

Instance Method Details

#commandObject



36
37
38
# File 'lib/lambda-console-ruby/interact.rb', line 36

def command
  @event.dig(*DIG)
end

#interactObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lambda-console-ruby/interact.rb', line 21

def interact
  begin
    body = eval(command, TOPLEVEL_BINDING).inspect
    { statusCode: 200, headers: {}, body: body }
  rescue Exception => e
    body = "#<#{e.class}:#{e.message}>".tap do |b|
      if e.backtrace
        b << "\n"
        b << e.backtrace.join("\n")
      end
    end
    { statusCode: 422, headers: {}, body: body }
  end
end