Module: Modulator
- Defined in:
- lib/version.rb,
lib/modulator.rb
Constant Summary collapse
- VERSION =
"0.1.1"- LAMBDAS =
{}
Class Method Summary collapse
-
.add_lambda(lambda_def, **opts) ⇒ Object
opts are for overrides.
- .add_lambda_from_hash(hash) ⇒ Object
- .add_lambda_from_module(mod, **opts) ⇒ Object
- .init_stack(app_name:, bucket:, **stack_opts) ⇒ Object
- .set_env(lambda_def) ⇒ Object
Class Method Details
.add_lambda(lambda_def, **opts) ⇒ Object
opts are for overrides
11 12 13 14 15 16 17 |
# File 'lib/modulator.rb', line 11 def add_lambda(lambda_def, **opts) # opts are for overrides if lambda_def.is_a?(Hash) add_lambda_from_hash(lambda_def) else add_lambda_from_module(lambda_def, **opts) end end |
.add_lambda_from_hash(hash) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/modulator.rb', line 19 def add_lambda_from_hash(hash) LAMBDAS[hash[:name]] = { name: hash[:name], gateway: hash[:gateway], module: hash[:module], wrapper: hash[:wrapper] || {}, env: hash[:env] || {}, settings: hash[:settings] || {} } end |
.add_lambda_from_module(mod, **opts) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/modulator.rb', line 30 def add_lambda_from_module(mod, **opts) mod.singleton_methods.sort.each do |module_method| module_name = mod.to_s module_names = module_name.split('::').map(&:downcase) verb = 'GET' path_fragments = module_names.dup # process parameters # method(a, b, c = 1, *args, d:, e: 2, **opts) # [[:req, :a], [:req, :b], [:opt, :c], [:rest, :args], [:keyreq, :d], [:key, :e], [:keyrest, :opts]] mod.method(module_method).parameters.each do |param| param_type = param[0] param_name = param[1] # collect required params path_fragments << ":#{param_name}" if param_type == :req # post if we have optional key param, ie. pet: {} verb = 'POST' if param_type == :key end # delete is special case based on method name verb = 'DELETE' if %w[destroy delete remove implode].include? module_method.to_s # finalize path path_fragments << module_method path = path_fragments.join('/') add_lambda_from_hash( { name: "#{module_names.join('-')}-#{module_method}", gateway: { verb: opts.dig(module_method, :gateway, :verb) || verb, path: opts.dig(module_method, :gateway, :path) || path }, module: { name: module_name, method: module_method.to_s, path: module_names.join('/') # file name }, wrapper: opts.dig(module_method, :wrapper) || opts[:wrapper], env: opts.dig(module_method, :env), settings: opts.dig(module_method, :settings) } ) end end |
.init_stack(app_name:, bucket:, **stack_opts) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/modulator.rb', line 88 def init_stack(app_name:, bucket:, **stack_opts) stack = AwsStackBuilder.init({ app_name: app_name.camelize, bucket: bucket, }.merge(stack_opts)) # add lambdas to stack puts 'Generating endpoints' LAMBDAS.each do |name, config| puts "- adding #{config.dig(:module, :name)}.#{config.dig(:module, :method)} to #{config.dig(:gateway, :path)}" stack.add_lambda_endpoint( gateway: config[:gateway], mod: config[:module], wrapper: config[:wrapper] || {}, env: config[:env] || {}, settings: config[:settings] || {} ) end # validate stack # puts 'Validating stack' # puts '- it is valid' if stack.valid? # return humidifier instance stack end |
.set_env(lambda_def) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/modulator.rb', line 78 def set_env(lambda_def) # remove wrapper if already set %w(name method path).each{|name| ENV.delete('wrapper_' + name)} # set env values lambda_def[:module].each{|name, value| ENV['module_' + name.to_s] = value.to_s} lambda_def[:gateway].each{|name, value| ENV['gateway_' + name.to_s] = value.to_s} lambda_def[:wrapper]&.each{|name, value| ENV['wrapper_' + name.to_s] = value.to_s} lambda_def[:env]&.each{|name, value| ENV[name.to_s] = value.to_s} # custom values end |