Class: Lambchop::Dump

Inherits:
Object
  • Object
show all
Defined in:
lib/lambchop/dump.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function_name, options = {}) ⇒ Dump

Returns a new instance of Dump.



6
7
8
9
10
11
# File 'lib/lambchop/dump.rb', line 6

def initialize(function_name, options = {})
  @function_name = function_name
  @client        = options[:client] || Aws::Lambda::Client.new
  @out           = options[:out] || $stdout
  @options       = options
end

Class Method Details

.dump(function_name, options = {}) ⇒ Object



2
3
4
# File 'lib/lambchop/dump.rb', line 2

def self.dump(function_name, options = {})
  self.new(function_name, options).dump
end

Instance Method Details

#dumpObject



13
14
15
16
17
18
# File 'lib/lambchop/dump.rb', line 13

def dump
  page = @client.get_function(:function_name => @function_name)
  puts_shebang
  puts_magic_comment(page.configuration)
  puts_source(page.code.location)
end

#puts_magic_comment(configuration) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lambchop/dump.rb', line 24

def puts_magic_comment(configuration)
  comment_attrs = {}

  %w(
    function_name
    runtime
    role
    handler
    description
    timeout
    memory_size
  ).each do |name|
    comment_attrs[name] = configuration.send(name)
  end

  yaml = YAML.dump(comment_attrs).sub(/\A---\n/, '')
  @out.puts("/*\n#{yaml}*/")
end

#puts_shebangObject



20
21
22
# File 'lib/lambchop/dump.rb', line 20

def puts_shebang
  @out.puts('#!/usr/bin/env lambchop')
end

#puts_source(location) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/lambchop/dump.rb', line 43

def puts_source(location)
  Lambchop::Utils.open_source(location) do |name, src|
    unless src =~ %r|\A// #{Regexp.escape(name)}\n|
      @out.puts("// #{name}")
    end

    @out.puts(src)
  end
end