Class: Jets::Controller::Renderers::RackRenderer
- Inherits:
-
BaseRenderer
- Object
- BaseRenderer
- Jets::Controller::Renderers::RackRenderer
- Defined in:
- lib/jets/controller/renderers/rack_renderer.rb
Instance Attribute Summary
Attributes inherited from BaseRenderer
Instance Method Summary collapse
- #cors_headers ⇒ Object
-
#map_status_code(code) ⇒ Object
maps: :continue => 100 :success => 200 etc.
- #normalized_base64_option(options) ⇒ Object
-
#render ⇒ Object
Example response:.
Methods inherited from BaseRenderer
Constructor Details
This class inherits a constructor from Jets::Controller::Renderers::BaseRenderer
Instance Method Details
#cors_headers ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 43 def cors_headers case Jets.config.cors when true { "Access-Control-Allow-Origin" => "*", # Required for CORS support to work "Access-Control-Allow-Credentials" => "true" # Required for cookies, authorization headers with HTTPS } when Hash Jets.config.cors # contains Hash with Access-Control-Allow-* values else {} end end |
#map_status_code(code) ⇒ Object
maps:
:continue => 100
:success => 200
etc
29 30 31 32 33 34 35 |
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 29 def map_status_code(code) if code.is_a?(Symbol) Rack::Utils::SYMBOL_TO_STATUS_CODE[code] else code end end |
#normalized_base64_option(options) ⇒ Object
37 38 39 40 41 |
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 37 def normalized_base64_option() base64 = @options[:base64] if .key?(:base64) base64 = @options[:isBase64Encoded] if .key?(:isBase64Encoded) base64 end |
#render ⇒ Object
Example response:
[200, {"my-header" = > "value" }, "my body" ]
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 9 def render # we do some normalization here status = map_status_code(@options[:status]) || 200 status = status.to_s # API Gateway requires a string but rack is okay with either body = @options[:body] base64 = normalized_base64_option(@options) headers = @options[:headers] || {} headers = cors_headers.merge(headers) headers["Content-Type"] ||= @options[:content_type] || Jets::Controller::DEFAULT_CONTENT_TYPE # x-jets-base64 to convert this Rack triplet to a API Gateway hash structure later headers["x-jets-base64"] = base64 ? 'yes' : 'no' # headers values must be Strings body = StringIO.new(body) [status, headers, body] # triplet end |