Module: LambdaOpenApi::Builder

Defined in:
lib/lambda_open_api/builder.rb

Constant Summary collapse

CRUD_VERBS =
[:get, :put, :post, :delete]

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Raises:

  • (NameError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lambda_open_api/builder.rb', line 23

def method_missing(method, *args, &block)
  if CRUD_VERBS.include?(method)
    crud_verb(method.to_s, args.first, &block)
    return
  end

  if method.to_s.start_with?("event")
    variable = method.to_s.sub("event_", "")
    return @event.send("#{variable}=", args.first)
  end

  if method.to_s.start_with?("default_event")
    variable = method.to_s.sub("default_event_", "")
    return @default_event.send("#{variable}=", args.first)
  end

  if method.to_s.start_with?("path")
    string = method.to_s.sub("path_", "")
    if @action.respond_to?(string)
      return @action.send("#{string}=", args.first)
    end
  end

  raise NameError.new("NameError (undefined local variable or method `#{method}' for #{self})")
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



7
8
9
# File 'lib/lambda_open_api/builder.rb', line 7

def actions
  @actions
end

#default_event_hashObject

Returns the value of attribute default_event_hash.



7
8
9
# File 'lib/lambda_open_api/builder.rb', line 7

def default_event_hash
  @default_event_hash
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/lambda_open_api/builder.rb', line 7

def name
  @name
end

Instance Method Details

#create_classObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/lambda_open_api/builder.rb', line 102

def create_class
  dynamic_name = "ClassName#{rand(10000)}"
  klass = Object.const_set(dynamic_name, Class.new do
    def self.set_event(event)
      @event = event
    end

    def self.event
      @event
    end

    def self.set_described_class(klass)
      @klass = klass
    end

    def self.invoke
      invokcation = LambdaOpenApi::Invoker.new(klass: @klass, method: "process", event: @event.json, context: OpenStruct.new(aws_request_id: "123"))
      @action.set_response(invokcation.response_body)
      invokcation.response
    end
    def self.set_action(action)
      @action = action
    end
  end)

  klass.set_event(@event)
  klass.set_action(@action)
  klass.set_described_class(described_class)
  klass
end

#crud_verb(verb, path) ⇒ Object

get, put, post, delete



50
51
52
53
54
55
56
57
58
59
# File 'lib/lambda_open_api/builder.rb', line 50

def crud_verb(verb, path)
  @action = LambdaOpenApi::Action.new(name: @name, http_verb: verb, path_name: path)
  @event = @default_event.dup
  @event.request_context = {"httpMethod" => verb.upcase, "http" => {"method" => verb.upcase}}
  @event.path = path
  yield

  @action.set_request_body(JSON.parse(@event.body))
  actions << @action
end

#example_case(code) ⇒ Object



61
62
63
64
65
# File 'lib/lambda_open_api/builder.rb', line 61

def example_case(code)
  @action.code = code

  yield
end

#invoke_lambdaObject



90
91
92
93
94
95
96
# File 'lib/lambda_open_api/builder.rb', line 90

def invoke_lambda
  parameter({}) if @event.resource.nil?

  invokcation = LambdaOpenApi::Invoker.new(klass: described_class, method: "process", event: @event.json, context: lambda_context)

  invokcation.response_body
end

#lambda_contextObject



98
99
100
# File 'lib/lambda_open_api/builder.rb', line 98

def lambda_context
  OpenStruct.new(aws_request_id: "123")
end

#parameter(hash) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/lambda_open_api/builder.rb', line 82

def parameter(hash)
  path = @action.path_name.dup
  @event.resource = path.gsub(LambdaOpenApi::Action::PARAMATER_EXPRESION) {|match|
    match = hash[match.delete('{}:').to_sym]
  }
  @event.path_parameters = hash.inject({}) {|hash, (key, value)| hash[key.to_s] = value; hash}
end

#resource(name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lambda_open_api/builder.rb', line 11

def resource(name)
  @actions = []
  @default_event = LambdaOpenApi::Event.new
  @name = name

  yield

  actions.each do |request|
    LambdaOpenApi::Formatter.add_request(request)
  end
end

#run_example(test_name = nil, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lambda_open_api/builder.rb', line 67

def run_example(test_name=nil, &block)
  klass = create_class
  test_name ||= "#{@action.http_verb} #{@action.path_name}"

  it "#{test_name}" do
    @klass = klass
    def lambda_response
      @lambda_response ||= @klass.invoke
    end

    instance_eval &block
  end
end