ruby_lambda 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f646027b7f81f705b0472a745971a06da4b80db8e45a492638fc12a0b9425356
4
- data.tar.gz: d0bf726f45c71e52d965a03dca6af04db1a1fe90e12c9fe47f0db78fc4f73862
3
+ metadata.gz: 01a15ca221c38d90226bf1650359ac671a2596a5efc9ed60600c8c89c0054e5b
4
+ data.tar.gz: d6243b2995a7c207a911d27764711801f9a1d4d0062f9934a9b7ea8be2d8e768
5
5
  SHA512:
6
- metadata.gz: 64ea3b68aa6e3e789bcd17bdfc337a61cfed2678025cb8c52a911af9702c3289edc6a1feeb83b41ef287464b96ae0d1be117c7f474f6764f1b87bf5e10a314f1
7
- data.tar.gz: b02f4be3d106db803cb2ed5301521baddcc969285aab632be9c5009edc08cf6b9b4c51970019db49e88f86598530d5ea573540f3d5e6aa3da1d148cbf74de059
6
+ metadata.gz: f90537875be6ab246ecbf48fecc0488bdea4ee71f2dc2ab01293d976d6c04681123bac4020c2f4d730a461495be43f4526c62b63c1c25c3ba4422bb9f48bc9e7
7
+ data.tar.gz: 7415bb05652a69a515f26d9c7f6c260fe24cf76d258d08b1936877feb3bc662cf2560149d2578c71f004beb7d18ae5d90dae9c9261a9d1a951b7fa58338a6f0d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_lambda (0.2.2)
4
+ ruby_lambda (0.3.0)
5
5
  awesome_print (~> 1.8.0)
6
6
  thor (~> 0.19)
7
7
 
data/README.md CHANGED
@@ -26,7 +26,7 @@ $ ruby-lambda init
26
26
  ```
27
27
 
28
28
  Initializes the `.gitignore`, `config.yml`, `env`, `event.json`, `lambda_function.rb`, `Gemfile`, `.ruby-version` files.
29
- * `event.json` is where you mock your event.
29
+ * `event.json` is where you keep mock data that will be passed to your function when the `execute` command has ran.
30
30
  * `config.yml` contains some default configuration for your function.
31
31
  * `env` will be renamed to `.env` after the init command runs, it will contain `AWS_ACCESS_KEY` and `AWS_SECRET_ACCESS_KEY`. You will need these to be able to deploy your function.
32
32
 
@@ -36,6 +36,27 @@ Please have a read of the `config.yml` and update any of the default configurati
36
36
  ```
37
37
  $ ruby-lambda execute
38
38
  ```
39
+ This command is used to invoke / run the function locally
40
+
41
+ ```
42
+ Options:
43
+ -c, [--config=CONFIG] # Default: config.yml
44
+ -H, [--handler=HANDLER]
45
+ ```
46
+
47
+ **Examples**
48
+ * `$ ruby-lambda execute -c=config.yml`
49
+ * `$ ruby-lambda execute -H=lambda_function.handler`
50
+
51
+ The handler function is the function AWS Lambda will invoke / run in response to an event. AWS Lambda uses the event argument to pass in event data to the handler. If the `handler` flas is passed with execute, this will take precedence over the handler function defined within the `config.yml`
52
+
53
+ ```ruby
54
+ def handler(event:, context:)
55
+ { statusCode: 200, body: JSON.generate('Hello from Ruby Lambda') }
56
+ end
57
+ ```
58
+
59
+ The `execute` command gets the values stored in the `event.json` file and passes them to your handler function.
39
60
 
40
61
  ## Development
41
62
 
data/bin/ruby-lambda CHANGED
@@ -5,7 +5,7 @@ require 'ruby_lambda'
5
5
 
6
6
  class RubyLambdaCLI < Thor
7
7
  map '-v' => :version, '--version' => :version
8
- desc '-v (--versions)', 'Outputs the version of Ruby Lambda.'
8
+ desc '-v (--versions)', 'Outputs the version of Ruby_Lambda.'
9
9
  def version
10
10
  puts RubyLambda::VERSION
11
11
  end
@@ -15,9 +15,19 @@ class RubyLambdaCLI < Thor
15
15
  RubyLambda::Init.new(FileUtils.pwd).run
16
16
  end
17
17
 
18
- desc 'execute', 'execute'
18
+ desc 'execute', 'Invokes the function locally'
19
+ long_desc <<-LONGDESC
20
+ This command is used to invoke / run the function locally
21
+
22
+ With -c / --config flag to pass a custom config file to use
23
+
24
+ With -H, [--handler=HANDLER]
25
+ LONGDESC
26
+
27
+ option :config, default: 'config.yml', type: :string, aliases: :c
28
+ option :handler, type: :string, aliases: :H
19
29
  def execute
20
- RubyLambda::Execute.new(FileUtils.pwd).run
30
+ RubyLambda::Execute.new(FileUtils.pwd, options).run
21
31
  end
22
32
  end
23
33
 
data/lib/ruby_lambda.rb CHANGED
@@ -5,6 +5,7 @@ require 'awesome_print'
5
5
  require 'yaml'
6
6
 
7
7
  require 'ruby_lambda/version'
8
+ require 'ruby_lambda/error'
8
9
  require 'ruby_lambda/init'
9
10
  require 'ruby_lambda/execute'
10
11
 
@@ -0,0 +1,15 @@
1
+ module RubyLambda
2
+ class Error < StandardError
3
+ attr_reader :exception_message
4
+
5
+ def initialize(message, exception_message: nil)
6
+ # Call the parent's constructor to set the message
7
+ super(message)
8
+
9
+ # Store the exception_message in an instance variable
10
+ @exception_message = exception_message
11
+ end
12
+ end
13
+
14
+ class ExecuteError < Error; end
15
+ end
@@ -2,30 +2,70 @@ require 'ruby_lambda/lambda_context'
2
2
 
3
3
  module RubyLambda
4
4
  class Execute
5
- def initialize(current_directory)
5
+ def initialize(current_directory, options = {"config"=>"config.yml"})
6
6
  @current_directory = current_directory
7
7
  @shell = Thor::Base.shell.new
8
+ @options = options
8
9
  end
9
10
 
10
11
  def run(mute: false)
11
- config_file = "#{@current_directory}/config.yml"
12
+ @mute = mute
12
13
 
13
- config_data = YAML.load_file config_file
14
+ begin
15
+ if @options.has_key?('handler')
14
16
 
15
- lambda_function, lambda_handler = config_data['handler'].split('.')
17
+ lambda_function, lambda_handler = @options['handler'].split('.')
18
+ elsif @options.has_key?('config')
19
+ lambda_function, lambda_handler = load_handler_from_file
20
+ end
16
21
 
17
- load "#{@current_directory}/#{lambda_function}.rb"
22
+ check_for_handler(lambda_function, lambda_handler)
18
23
 
19
- event_json_file = File.read("#{@current_directory}/event.json")
24
+ load "#{@current_directory}/#{lambda_function}.rb"
20
25
 
21
- event = JSON.parse(event_json_file)
26
+ event = JSON.parse(File.read("#{@current_directory}/event.json"))
22
27
 
23
- context = LambdaContext.new()
28
+ context = LambdaContext.new()
24
29
 
25
- if mute
26
- send(:"#{lambda_handler}", event: event, context: context)
27
- else
28
- ap send(:"#{lambda_handler}", event: event, context: context)
30
+ if mute
31
+ send(:"#{lambda_handler}", event: event, context: context)
32
+ else
33
+ ap send(:"#{lambda_handler}", event: event, context: context), options = { :indent => -2 }
34
+ end
35
+ rescue LoadError
36
+ @shell.say('Handler file or function, can not be found', :red) unless @mute
37
+
38
+ exit 1
39
+ end
40
+ end
41
+
42
+ def load_handler_from_file
43
+ begin
44
+ config_data = YAML.load_file "#{@current_directory}/#{@options['config']}"
45
+
46
+ raise RubyLambda::ExecuteError.new('Invalid config file') unless config_data.is_a?(Hash)
47
+
48
+ config_data['handler'].split('.')
49
+ rescue Errno::ENOENT
50
+ 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'
51
+
52
+ @shell.say(no_config_file_message, :red) unless @mute
53
+
54
+ exit 1
55
+ rescue RubyLambda::ExecuteError
56
+ @shell.say('Invalid config file', :red) unless @mute
57
+
58
+ exit 1
59
+ end
60
+ end
61
+
62
+ def check_for_handler(lambda_function, lambda_handler)
63
+ if lambda_handler.nil? || lambda_handler.nil?
64
+ 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'
65
+
66
+ @shell.say(no_defined_handler_message, :red) unless @mute
67
+
68
+ exit 1
29
69
  end
30
70
  end
31
71
  end
@@ -1,3 +1,3 @@
1
1
  module RubyLambda
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_lambda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cookieshq
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-01-09 00:00:00.000000000 Z
12
+ date: 2019-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -147,6 +147,7 @@ files:
147
147
  - bin/ruby-lambda
148
148
  - bin/setup
149
149
  - lib/ruby_lambda.rb
150
+ - lib/ruby_lambda/error.rb
150
151
  - lib/ruby_lambda/execute.rb
151
152
  - lib/ruby_lambda/init.rb
152
153
  - lib/ruby_lambda/lambda_context.rb