Class: WebSvr::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/web_svr/response.rb

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze
TEXT_TYPE =
'text/plain'.freeze
JSON_TYPE =
'application/json'.freeze
HTML_TYPE =
'text/html'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine = nil, code = WebSvr::ResponseCode::SUCCESS, type = HTML_TYPE, data = nil, assetCache = false, file_name = nil, download = false) ⇒ Response

Set up the web server.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/web_svr/response.rb', line 31

def initialize( engine = nil, 
  code = 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

#codeObject (readonly)

Returns the value of attribute code.



19
20
21
# File 'lib/web_svr/response.rb', line 19

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/web_svr/response.rb', line 19

def data
  @data
end

#file_nameObject

Returns the value of attribute file_name.



21
22
23
# File 'lib/web_svr/response.rb', line 21

def file_name
  @file_name
end

#locationObject

Returns the value of attribute location.



20
21
22
# File 'lib/web_svr/response.rb', line 20

def location
  @location
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/web_svr/response.rb', line 19

def type
  @type
end

Class Method Details

.html_response(engine, data, code = WebSvr::ResponseCode::SUCCESS) ⇒ Object

Helper to create a successful web response with the given data.



77
78
79
80
81
# File 'lib/web_svr/response.rb', line 77

def self.html_response( engine, data, 
  code = WebSvr::ResponseCode::SUCCESS )

  return WebSvr::Response.new( engine, code, HTML_TYPE, data )
end

.json_response(engine, data, code = WebSvr::ResponseCode::SUCCESS) ⇒ Object

Helper to create a successful JSON response with the given data.



59
60
61
62
63
# File 'lib/web_svr/response.rb', line 59

def self.json_response( engine, data, 
  code = WebSvr::ResponseCode::SUCCESS )

  return WebSvr::Response.new( engine, code, JSON_TYPE, data )
end

.redirect_response(engine, target) ⇒ Object

Helper to create a redirect response.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/web_svr/response.rb', line 86

def self.redirect_response( engine, target )
  code = WebSvr::ResponseCode::FOUND
  data = <<~TEXT
  <head>
    <html>
      <body><a href="#{target}">target is here</a></body>
    </html>
  </head>
  TEXT

  response = WebSvr::Response.new( engine, code, HTML_TYPE, data )
  response.location = target

  return response
end

.text_response(engine, data, code = WebSvr::ResponseCode::SUCCESS) ⇒ Object

Helper to create a successful text response with the given data.



68
69
70
71
72
# File 'lib/web_svr/response.rb', line 68

def self.text_response( engine, data, 
  code = WebSvr::ResponseCode::SUCCESS )

  return WebSvr::Response.new( engine, code, TEXT_TYPE, data )
end

Instance Method Details

#add(content) ⇒ Object

Add content to the payload.



110
111
112
113
# File 'lib/web_svr/response.rb', line 110

def add content
  @data = '' if @data.nil?
  @data << content
end

#headersObject

Get the headers for the response.



118
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
# File 'lib/web_svr/response.rb', line 118

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

#logObject

Write the result information to the log.



168
169
170
171
172
# File 'lib/web_svr/response.rb', line 168

def log
  return unless @log

  @log.info "Response #{@code} #{@type}"
end

#resultObject

Get the final result that will be returned as the response to the web request.



156
157
158
# File 'lib/web_svr/response.rb', line 156

def result
  return [ @code, headers, @data ]
end