lib/gloo/web_svr/response.rb in gloo-3.5.0 vs lib/gloo/web_svr/response.rb in gloo-3.6.0
- old
+ new
@@ -16,10 +16,11 @@
TEXT_TYPE = 'text/plain'.freeze
JSON_TYPE = 'application/json'.freeze
HTML_TYPE = 'text/html'.freeze
attr_reader :code, :type, :data
+ attr_accessor :location
# ---------------------------------------------------------------------
# Initialization
# ---------------------------------------------------------------------
@@ -35,10 +36,11 @@
@log = @engine.log if @engine
@code = code
@type = type
@data = data
+ @location = nil
end
# ---------------------------------------------------------------------
# Static Helper Functions
@@ -69,11 +71,30 @@
code = Gloo::WebSvr::ResponseCode::SUCCESS )
return Gloo::WebSvr::Response.new( engine, code, HTML_TYPE, data )
end
+ #
+ # Helper to create a redirect response.
+ #
+ def self.redirect_response( engine, target )
+ code = Gloo::WebSvr::ResponseCode::FOUND
+ data = <<~TEXT
+ <head>
+ <html>
+ <body><a href="#{target}">target is here</a></body>
+ </html>
+ </head>
+ TEXT
+ response = Gloo::WebSvr::Response.new( engine, code, HTML_TYPE, data )
+ response.location = target
+
+ return response
+ end
+
+
# ---------------------------------------------------------------------
# Data Functions
# ---------------------------------------------------------------------
#
@@ -94,9 +115,13 @@
# https://stackoverflow.com/questions/3295083/how-do-i-set-a-cookie-with-a-ruby-rack-middleware-component
# https://www.rubydoc.info/gems/rack/1.4.7/Rack/Session/Cookie
#
headers = { CONTENT_TYPE => @type }
+
+ if @location
+ headers[ 'Location' ] = @location
+ end
session = @engine&.running_app&.obj&.session
headers = session.add_session_for_response( headers ) if session
return headers