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.
-
#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) ⇒ 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) ⇒ Response
Set up the web server.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gloo/web_svr/response.rb', line 31 def initialize( engine = nil, code = Gloo::WebSvr::ResponseCode::SUCCESS, type = HTML_TYPE, data = nil ) @engine = engine @log = @engine.log if @engine @code = code @type = type @data = data @location = nil 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 |
#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.
70 71 72 73 74 |
# File 'lib/gloo/web_svr/response.rb', line 70 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.
52 53 54 55 56 |
# File 'lib/gloo/web_svr/response.rb', line 52 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gloo/web_svr/response.rb', line 79 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.
61 62 63 64 65 |
# File 'lib/gloo/web_svr/response.rb', line 61 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.
103 104 105 106 |
# File 'lib/gloo/web_svr/response.rb', line 103 def add content @data = '' if @data.nil? @data << content end |
#headers ⇒ Object
Get the headers for the response.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/gloo/web_svr/response.rb', line 111 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 session = @engine&.running_app&.obj&.session headers = session.add_session_for_response( headers ) if session return headers end |
#log ⇒ Object
Write the result information to the log.
147 148 149 150 151 |
# File 'lib/gloo/web_svr/response.rb', line 147 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.
135 136 137 |
# File 'lib/gloo/web_svr/response.rb', line 135 def result return [ @code, headers, @data ] end |