Class: FDK::Call

Inherits:
Object
  • Object
show all
Defined in:
lib/fdk/call.rb

Overview

Call represents a call to the target function or lambda

Constant Summary collapse

FILTER_HEADERS =
["content-length", "te", "transfer-encoding",
"upgrade", "trailer"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request:, response:) ⇒ Call

Returns a new instance of Call.



10
11
12
13
# File 'lib/fdk/call.rb', line 10

def initialize(request:, response:)
  @request = request
  @response = response
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/fdk/call.rb', line 8

def error
  @error
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/fdk/call.rb', line 7

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/fdk/call.rb', line 7

def response
  @response
end

Instance Method Details

#contextObject



15
16
17
# File 'lib/fdk/call.rb', line 15

def context
  @context ||= FDK::Context.new(headers_in, headers_out)
end

#error_response(error:) ⇒ Object



55
56
57
58
59
60
# File 'lib/fdk/call.rb', line 55

def error_response(error:)
  response["content-type"] = "application/json"
  response.status = 502
  response.body = { message: "An error occurred in the function",
                    detail: error.to_s }.to_json
end

#filtered_request_headerObject



31
32
33
# File 'lib/fdk/call.rb', line 31

def filtered_request_header
  request.header.reject { |k| FILTER_HEADERS.include? k }
end

#format_response_body(fn_return:) ⇒ Object



43
44
45
46
47
48
# File 'lib/fdk/call.rb', line 43

def format_response_body(fn_return:)
  return response.body = fn_return.to_s unless fn_return.respond_to?(:to_json)

  response.body = fn_return.to_json
  response["content-type"] = "application/json" unless response["content-type"]
end

#good_responseObject



50
51
52
53
# File 'lib/fdk/call.rb', line 50

def good_response
  response.status = 200
  headers_out.each { |k, v| response[k] = v.join(",") }
end

#headers_inObject



27
28
29
# File 'lib/fdk/call.rb', line 27

def headers_in
  @headers_in ||= FDK::InHeaders.new(filtered_request_header, nil)
end

#headers_outObject



23
24
25
# File 'lib/fdk/call.rb', line 23

def headers_out
  @headers_out ||= FDK::OutHeaders.new({}, nil)
end

#inputObject



19
20
21
# File 'lib/fdk/call.rb', line 19

def input
  @input ||= ParsedInput.new(raw_input: request.body.to_s)
end

#processObject



35
36
37
38
39
40
41
# File 'lib/fdk/call.rb', line 35

def process
  format_response_body(fn_return: yield(context: context, input: input.parsed))
  good_response
rescue StandardError => e
  FDK.log_error(error: e)
  error_response(error: e)
end