Class: RubyLambda::Execute

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lambda/execute.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_directory, options = {"config"=>"config.yml"}) ⇒ Execute

Returns a new instance of Execute.



5
6
7
8
9
# File 'lib/ruby_lambda/execute.rb', line 5

def initialize(current_directory, options = {"config"=>"config.yml"})
  @current_directory  = current_directory
  @shell = Thor::Base.shell.new
  @options = options
end

Instance Method Details

#check_for_handler(lambda_function, lambda_handler) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/ruby_lambda/execute.rb', line 56

def check_for_handler(lambda_function, lambda_handler)
  if lambda_handler.nil? || lambda_handler.nil?
    no_defined_handler_message = 'No config or handler function defined, create a config.yml file or use the -c / --config flag to use a different config file. Alternativly you can use the -H flag to pass the name of the handler that should be executed'

    @shell.say(no_defined_handler_message, :red)

    exit 1
  end
end

#load_handler_from_fileObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_lambda/execute.rb', line 36

def load_handler_from_file
  begin
    config_data = YAML.load_file "#{@current_directory}/#{@options['config']}"

    raise RubyLambda::ExecuteError.new('Invalid config file') unless config_data.is_a?(Hash)

    config_data['handler'].split('.')
  rescue Errno::ENOENT
    no_config_file_message = 'Config file missing, create a config.yml file or use the -c / --config flag to use a different config file. Alternativly you can use the -H flag to pass the name of the handler that should be executed'

    @shell.say(no_config_file_message, :red)

    exit 1
  rescue RubyLambda::ExecuteError
    @shell.say('Invalid config file', :red)

    exit 1
  end
end

#runObject



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/ruby_lambda/execute.rb', line 11

def run
  begin
    if @options.has_key?('handler')

      lambda_function, lambda_handler = @options['handler'].split('.')
    elsif @options.has_key?('config')
      lambda_function, lambda_handler = load_handler_from_file
    end

    check_for_handler(lambda_function, lambda_handler)

    load "#{@current_directory}/#{lambda_function}.rb"

    event = JSON.parse(File.read("#{@current_directory}/event.json"))

    context = LambdaContext.new()

    ap send(:"#{lambda_handler}", event: event, context: context), options = { :indent => -2 }
  rescue LoadError
    @shell.say('Handler file or function, can not be found', :red)

    exit 1
  end
end