Module: Tzispa::Helpers::Response
- Defined in:
- lib/tzispa/helpers/response.rb
Defined Under Namespace
Classes: NotFound
Class Method Summary collapse
Instance Method Summary collapse
- #attachment!(filename = nil, disposition = 'attachment') ⇒ Object
-
#back ⇒ Object
Sugar for redirect (example: redirect back).
-
#body(value = nil, &block) ⇒ Object
Set or retrieve the response body.
-
#client_error? ⇒ Boolean
whether or not the status is set to 4xx.
-
#content_type(type = nil, params = {}) ⇒ Object
Set the Content-Type of the response body given a media type or file extension.
-
#error(code, body = nil) ⇒ Object
Halt processing and return the error status provided.
-
#halt(*response) ⇒ Object
Exit the current block, halts any further processing of the request, and returns the specified response.
-
#headers(hash = nil) ⇒ Object
Set multiple response headers with Hash.
-
#informational? ⇒ Boolean
whether or not the status is set to 1xx.
-
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 Not Found.
-
#not_found? ⇒ Boolean
whether or not the status is set to 404.
-
#permanent_redirect(uri, absolute, *args) ⇒ Object
Halt processing and permanet_redirect redirect to the URI provided.
-
#redirect(uri, absolute, *args) ⇒ Object
Halt processing and redirect to the URI provided.
-
#redirect? ⇒ Boolean
whether or not the status is set to 3xx.
-
#send_file(path, opts = {}) ⇒ Object
Use the contents of the file at
pathas the response body. -
#server_error? ⇒ Boolean
whether or not the status is set to 5xx.
-
#status(value = nil) ⇒ Object
Set or retrieve the response status code.
-
#success? ⇒ Boolean
whether or not the status is set to 2xx.
-
#unauthorized(body = nil) ⇒ Object
Halt processing and return a 401 Unauthorized.
-
#uri(addr = nil, absolute = true) ⇒ Object
Generates the absolute URI for a given path in the app.
Class Method Details
.included(base) ⇒ Object
10 11 12 13 14 |
# File 'lib/tzispa/helpers/response.rb', line 10 def self.included(base) base.class_eval do include Tzispa::Helpers::Mime end end |
Instance Method Details
#attachment!(filename = nil, disposition = 'attachment') ⇒ Object
127 128 129 130 131 |
# File 'lib/tzispa/helpers/response.rb', line 127 def (filename = nil, disposition = 'attachment') content_disposition = disposition.to_s content_disposition += "; filename=\"#{filename}\"; filename*=UTF-8''#{URI.escape(filename)}" if !filename.nil? response['Content-Disposition'] = content_disposition end |
#back ⇒ Object
Sugar for redirect (example: redirect back)
163 164 165 |
# File 'lib/tzispa/helpers/response.rb', line 163 def back request.referer end |
#body(value = nil, &block) ⇒ Object
Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with #each.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tzispa/helpers/response.rb', line 24 def body(value = nil, &block) if block_given? def block.each; yield(call) end response.body = block elsif value headers.delete 'Content-Length' unless request.head? || value.is_a?(Rack::File) || value.is_a?(Stream) response.body = value else response.body end end |
#client_error? ⇒ Boolean
whether or not the status is set to 4xx
183 184 185 |
# File 'lib/tzispa/helpers/response.rb', line 183 def client_error? response.status.between? 400, 499 end |
#content_type(type = nil, params = {}) ⇒ Object
Set the Content-Type of the response body given a media type or file extension.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tzispa/helpers/response.rb', line 107 def content_type(type = nil, params = {}) return response['Content-Type'] unless type default = params.delete :default mime_type = mime_type(type) || default fail "Unknown media type: %p" % type if mime_type.nil? mime_type = mime_type.dup unless params.include? :charset params[:charset] = params.delete('charset') || config.default_encoding end params.delete :charset if mime_type.include? 'charset' unless params.empty? mime_type << (mime_type.include?(';') ? ', ' : ';') mime_type << params.map do |key, val| val = val.inspect if val =~ /[";,]/ "#{key}=#{val}" end.join(', ') end response['Content-Type'] = mime_type end |
#error(code, body = nil) ⇒ Object
Halt processing and return the error status provided.
83 84 85 86 87 |
# File 'lib/tzispa/helpers/response.rb', line 83 def error(code, body = nil) code, body = 500, code.to_str if code.respond_to? :to_str response.body = body unless body.nil? halt code end |
#halt(*response) ⇒ Object
Exit the current block, halts any further processing of the request, and returns the specified response.
38 39 40 41 |
# File 'lib/tzispa/helpers/response.rb', line 38 def halt(*response) response = response.first if response.length == 1 throw :halt, response end |
#headers(hash = nil) ⇒ Object
Set multiple response headers with Hash.
100 101 102 103 |
# File 'lib/tzispa/helpers/response.rb', line 100 def headers(hash = nil) response.headers.merge! hash if hash response.headers end |
#informational? ⇒ Boolean
whether or not the status is set to 1xx
168 169 170 |
# File 'lib/tzispa/helpers/response.rb', line 168 def informational? response.status.between? 100, 199 end |
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 Not Found
90 91 92 |
# File 'lib/tzispa/helpers/response.rb', line 90 def not_found(body = nil) error 404, body end |
#not_found? ⇒ Boolean
whether or not the status is set to 404
193 194 195 |
# File 'lib/tzispa/helpers/response.rb', line 193 def not_found? response.status == 404 end |
#permanent_redirect(uri, absolute, *args) ⇒ Object
Halt processing and permanet_redirect redirect to the URI provided.
58 59 60 61 62 |
# File 'lib/tzispa/helpers/response.rb', line 58 def permanent_redirect(uri, absolute, *args) status 301 response['Location'] = uri(uri.to_s, absolute) halt(*args) end |
#redirect(uri, absolute, *args) ⇒ Object
Halt processing and redirect to the URI provided.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tzispa/helpers/response.rb', line 44 def redirect(uri, absolute, *args) if (env['HTTP_VERSION'] == 'HTTP/1.1' || env['HTTP_VERSION'] == 'HTTP/2.0') && env["REQUEST_METHOD"] != 'GET' status 303 else status 302 end # According to RFC 2616 section 14.30, "the field value consists of a # single absolute URI" response['Location'] = uri(uri.to_s, absolute) halt(*args) end |
#redirect? ⇒ Boolean
whether or not the status is set to 3xx
178 179 180 |
# File 'lib/tzispa/helpers/response.rb', line 178 def redirect? response.status.between? 300, 399 end |
#send_file(path, opts = {}) ⇒ Object
Use the contents of the file at path as the response body.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/tzispa/helpers/response.rb', line 134 def send_file(path, opts = {}) begin if opts[:type] or not response['Content-Type'] content_type opts[:type] || opts[:extension], :default => 'application/octet-stream' end disposition = opts[:disposition] filename = opts[:filename] disposition = 'attachment' if disposition.nil? and filename filename = path if filename.nil? filename, disposition last_modified opts[:last_modified] if opts[:last_modified] file = Rack::File.new nil file.path = path result = file.serving context.env result[1].each { |k,v| response.headers[k] ||= v } response.headers['Content-Length'] = result[1]['Content-Length'] #opts[:status] &&= Integer(opts[:status]) #halt opts[:status] || result[0], result[2] response.status = result[0] response.body = result[2] rescue not_found 'Fichero no encontrado' end end |
#server_error? ⇒ Boolean
whether or not the status is set to 5xx
188 189 190 |
# File 'lib/tzispa/helpers/response.rb', line 188 def server_error? response.status.between? 500, 599 end |
#status(value = nil) ⇒ Object
Set or retrieve the response status code.
17 18 19 20 |
# File 'lib/tzispa/helpers/response.rb', line 17 def status(value = nil) response.status = value if value response.status end |
#success? ⇒ Boolean
whether or not the status is set to 2xx
173 174 175 |
# File 'lib/tzispa/helpers/response.rb', line 173 def success? response.status.between? 200, 299 end |
#unauthorized(body = nil) ⇒ Object
Halt processing and return a 401 Unauthorized
95 96 97 |
# File 'lib/tzispa/helpers/response.rb', line 95 def (body = nil) error 401, body end |
#uri(addr = nil, absolute = true) ⇒ Object
Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tzispa/helpers/response.rb', line 67 def uri(addr = nil, absolute = true) return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/ uri = [host = String.new] if absolute host << "http#{'s' if request.secure?}://" if request.forwarded? or request.port != (request.secure? ? 443 : 80) host << request.host_with_port else host << request.host end end uri << (addr ? addr : request.path_info).to_s File.join uri end |