Class: Lamby::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/lamby/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Command

Returns a new instance of Command.



16
17
18
19
# File 'lib/lamby/command.rb', line 16

def initialize(event)
  @event = event
  @body = ''
end

Class Method Details

.cmd(event:, context:) ⇒ Object



10
11
12
# File 'lib/lamby/command.rb', line 10

def cmd(event:, context:)
  new(event).call
end

.handle?(event) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/lamby/command.rb', line 6

def handle?(event)
  event.dig 'lamby', 'command'
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lamby/command.rb', line 21

def call
  begin
    body = eval(command, TOPLEVEL_BINDING).to_s
    body = body.inspect if body =~ /\A"/ && body =~ /"\z/
    { 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

#commandObject



37
38
39
# File 'lib/lamby/command.rb', line 37

def command
  @event.dig 'lamby', 'command'
end