Class: Tck::Lambdas::AwsFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/tck/lambdas/aws_function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ AwsFunction

Returns a new instance of AwsFunction.



24
25
26
27
# File 'lib/tck/lambdas/aws_function.rb', line 24

def initialize(name)
  @name = name.to_s
  @conf = yaml ? yaml[@name] : {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



37
38
39
40
# File 'lib/tck/lambdas/aws_function.rb', line 37

def method_missing(method, *args, &block)  
  m = method.to_s
  @conf[m] || @conf[m.gsub("_","-")] || super
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



9
10
11
# File 'lib/tck/lambdas/aws_function.rb', line 9

def conf
  @conf
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/tck/lambdas/aws_function.rb', line 8

def name
  @name
end

Class Method Details

.clean_tmps!Object



19
20
21
22
# File 'lib/tck/lambdas/aws_function.rb', line 19

def self.clean_tmps!
  FileUtils.mkdir_p tmpdir
  FileUtils.rm_rf Dir.glob("#{tmpdir}/*")
end

.tmpdirObject



11
12
13
# File 'lib/tck/lambdas/aws_function.rb', line 11

def self.tmpdir
  @tmpdir ||= Dir.tmpdir + "/tck-lambdas"
end

.yamlObject



15
16
17
# File 'lib/tck/lambdas/aws_function.rb', line 15

def self.yaml
  @yaml ||= File.exist?('.lambdas.yml') ? YAML.load_file('.lambdas.yml') : nil
end

Instance Method Details

#dirObject



42
43
44
# File 'lib/tck/lambdas/aws_function.rb', line 42

def dir
  @dir ||= "lambdas/#{name}"
end

#function_nameObject



29
30
31
# File 'lib/tck/lambdas/aws_function.rb', line 29

def function_name
  @conf['function-name'] || name
end

#invoke_events_in_directory(event_type) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/tck/lambdas/aws_function.rb', line 54

def invoke_events_in_directory(event_type)
  Dir["lambdas/#{name}/test/#{event_type}/*.json"].each do |json_file|
    filename = File.basename(json_file)
    output = "#{tmpdir}/#{filename}.output"
    invoke_lambda json_file, output
    yield filename, File.read(output)
  end
end

#invoke_lambda(payload_file, output_file) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/tck/lambdas/aws_function.rb', line 63

def invoke_lambda(payload_file, output_file)
  cmd = "aws lambda invoke " <<
          "--function-name #{@conf['function-name']}_test " <<
          "--payload file://#{payload_file} #{output_file}"
  puts "$ #{cmd}"
  `#{cmd}`
end

#test_function_nameObject



33
34
35
# File 'lib/tck/lambdas/aws_function.rb', line 33

def test_function_name
  "#{@conf['function-name']}_test"
end

#tmpdirObject



46
47
48
# File 'lib/tck/lambdas/aws_function.rb', line 46

def tmpdir
  self.class.tmpdir
end

#yamlObject



71
72
73
# File 'lib/tck/lambdas/aws_function.rb', line 71

def yaml
  self.class.yaml
end

#zip_fileObject



50
51
52
# File 'lib/tck/lambdas/aws_function.rb', line 50

def zip_file
  @zip_file ||= "#{Dir.pwd}/#{dir}/#{function_name}.zip"
end