Class: AwsLambda::Marshaller

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/overrides/lambda/marshaller.rb

Class Method Summary collapse

Class Method Details

.marshall_response(method_response) ⇒ Object

By default, just runs #to_json on the method’s response value. This can be overwritten by users who know what they are doing. The response is an array of response, content-type. If returned without a content-type, it is assumed to be application/json Finally, StringIO/IO is used to signal a response that shouldn’t be formatted as JSON, and should get a different content-type header.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jets/overrides/lambda/marshaller.rb', line 18

def marshall_response(method_response)
  case method_response
  when StringIO, IO
    [method_response, 'application/unknown']
  else
    # Orignal method calls .to_json but this collides with ActiveSupport's to_json
    # method_response.to_json # application/json is assumed
    # https://stackoverflow.com/questions/18067203/ruby-to-json-issue-with-error-illegal-malformed-utf-8
    if method_response.is_a?(Hash)
      method_response.deep_transform_values! do |v|
        if v.respond_to?(:force_encoding) && !v.frozen?
          v.force_encoding("ISO-8859-1").encode("UTF-8")
        else
          v # IE: Integer
        end
      end
    end
    JSON.dump(method_response)
  end
end