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.
-
#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.
-
.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.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gloo/web_svr/response.rb', line 30 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 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 |
#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.
68 69 70 71 72 |
# File 'lib/gloo/web_svr/response.rb', line 68 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.
50 51 52 53 54 |
# File 'lib/gloo/web_svr/response.rb', line 50 def self.json_response( engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS ) return Gloo::WebSvr::Response.new( engine, code, JSON_TYPE, data ) end |
.text_response(engine, data, code = Gloo::WebSvr::ResponseCode::SUCCESS) ⇒ Object
Helper to create a successful text response with the given data.
59 60 61 62 63 |
# File 'lib/gloo/web_svr/response.rb', line 59 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.
82 83 84 85 |
# File 'lib/gloo/web_svr/response.rb', line 82 def add content @data = '' if @data.nil? @data << content end |
#headers ⇒ Object
Get the headers for the response.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/gloo/web_svr/response.rb', line 90 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 } 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.
122 123 124 125 126 |
# File 'lib/gloo/web_svr/response.rb', line 122 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.
110 111 112 |
# File 'lib/gloo/web_svr/response.rb', line 110 def result return [ @code, headers, @data ] end |