Class: Gloo::WebSvr::Response
- Inherits:
-
Object
- Object
- Gloo::WebSvr::Response
- Defined in:
- lib/gloo/web_svr/response.rb
Constant Summary collapse
- CONTENT_TYPE =
for a list of content types.
'Content-Type'.freeze
- TEXT_TYPE =
'text/plain'.freeze
- JSON_TYPE =
'application/json'.freeze
- HTML_TYPE =
'text/html'.freeze
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#location ⇒ Object
Returns the value of attribute location.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.html_response(engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS) ⇒ Object
Helper to create a successful web response with the given data.
-
.json_response(engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS) ⇒ Object
Helper to create a successful JSON response with the given data.
-
.redirect_response(engine, target) ⇒ Object
Helper to create a redirect response.
-
.text_response(engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS) ⇒ Object
Helper to create a successful text response with the given data.
Instance Method Summary collapse
-
#add(content) ⇒ Object
Add content to the payload.
-
#headers ⇒ Object
Get the headers for the response.
-
#initialize(engine = nil, code = Gloo::WebSvr::ResponseCode::SUCCESS, type = HTML_TYPE, data = nil, assetCache = false, file_name = nil, download = false) ⇒ Response
constructor
Set up the web server.
-
#log ⇒ Object
Write the result information to the log.
-
#result ⇒ Object
Get the final result that will be returned as the response to the web request.
Constructor Details
#initialize(engine = nil, code = Gloo::WebSvr::ResponseCode::SUCCESS, type = HTML_TYPE, data = nil, assetCache = false, file_name = nil, download = false) ⇒ Response
Set up the web server.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gloo/web_svr/response.rb', line 32 def initialize( engine = nil, code = Gloo::WebSvr::ResponseCode::SUCCESS, type = HTML_TYPE, data = nil, assetCache = false, file_name = nil, download = false ) @engine = engine @log = @engine.log if @engine @code = code @type = type @data = data @assetCache = assetCache @location = nil @file_name = file_name @download = download end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
20 21 22 |
# File 'lib/gloo/web_svr/response.rb', line 20 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/gloo/web_svr/response.rb', line 20 def data @data end |
#file_name ⇒ Object
Returns the value of attribute file_name.
22 23 24 |
# File 'lib/gloo/web_svr/response.rb', line 22 def file_name @file_name end |
#location ⇒ Object
Returns the value of attribute location.
21 22 23 |
# File 'lib/gloo/web_svr/response.rb', line 21 def location @location end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
20 21 22 |
# File 'lib/gloo/web_svr/response.rb', line 20 def type @type end |
Class Method Details
.html_response(engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS) ⇒ Object
Helper to create a successful web response with the given data.
78 79 80 81 82 |
# File 'lib/gloo/web_svr/response.rb', line 78 def self.html_response( engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS ) return Gloo::WebSvr::Response.new( engine, code, HTML_TYPE, data ) end |
.json_response(engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS) ⇒ Object
Helper to create a successful JSON response with the given data.
60 61 62 63 64 |
# File 'lib/gloo/web_svr/response.rb', line 60 def self.json_response( engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS ) return Gloo::WebSvr::Response.new( engine, code, JSON_TYPE, data ) end |
.redirect_response(engine, target) ⇒ Object
Helper to create a redirect response.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gloo/web_svr/response.rb', line 87 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 |
.text_response(engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS) ⇒ Object
Helper to create a successful text response with the given data.
69 70 71 72 73 |
# File 'lib/gloo/web_svr/response.rb', line 69 def self.text_response( engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS ) return Gloo::WebSvr::Response.new( engine, code, TEXT_TYPE, data ) end |
Instance Method Details
#add(content) ⇒ Object
Add content to the payload.
111 112 113 114 |
# File 'lib/gloo/web_svr/response.rb', line 111 def add content @data = '' if @data.nil? @data << content end |
#headers ⇒ Object
Get the headers for the response.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gloo/web_svr/response.rb', line 119 def headers # # TO DO: Add more cookie headers here. # # 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 if @assetCache || @file_name headers[ 'Cache-Control' ] = 'public, max-age=604800' headers[ 'Expires' ] = (Time.now.utc + 604800).to_s end if @file_name disp = @download ? 'attachment' : 'inline' headers[ 'Content-Disposition' ] = "#{disp}; filename=#{@file_name}" headers[ 'Content-Length' ] = @data.length.to_s end session = @engine&.running_app&.obj&.session headers = session.add_session_for_response( headers ) if session # Clear out session data after the response is prepared. @engine&.running_app&.obj&.reset_session_data return headers end |
#log ⇒ Object
Write the result information to the log.
169 170 171 172 173 |
# File 'lib/gloo/web_svr/response.rb', line 169 def log return unless @log @log.info "Response #{@code} #{@type}" end |
#result ⇒ Object
Get the final result that will be returned as the response to the web request.
157 158 159 |
# File 'lib/gloo/web_svr/response.rb', line 157 def result return [ @code, headers, @data ] end |