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 =
%w[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.



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

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

Instance Attribute Details

#errorObject

Returns the value of attribute error.



25
26
27
# File 'lib/fdk/call.rb', line 25

def error
  @error
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#contextObject



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

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

#error_response(error:) ⇒ Object



72
73
74
75
76
77
# File 'lib/fdk/call.rb', line 72

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



48
49
50
# File 'lib/fdk/call.rb', line 48

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

#format_response_body(fn_return:) ⇒ Object



60
61
62
63
64
65
# File 'lib/fdk/call.rb', line 60

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



67
68
69
70
# File 'lib/fdk/call.rb', line 67

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

#headers_inObject



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

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

#headers_outObject



40
41
42
# File 'lib/fdk/call.rb', line 40

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

#inputObject



36
37
38
# File 'lib/fdk/call.rb', line 36

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

#processObject



52
53
54
55
56
57
58
# File 'lib/fdk/call.rb', line 52

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