class StirFry::Response
Response is a wrapper around Rack::Response that makes it easier to generate rich responses easily.
Attributes
the rack request env.
the corresponding reqest to this reponse.
if there was an error during the request it will be set here.
the inner Rack::Response that we are building.
Public Class Methods
Source
# File lib/stir_fry/response.rb, line 27 def initialize(env) @env = env @req = StirFry::Request.new(env) @resp = Rack::Response.new content_type(Rack::MediaType.type(env["HTTP_ACCEPT"])) end
create a new Reponse
Public Instance Methods
Source
# File lib/stir_fry/response.rb, line 54 def bad_request(body) = respond(body, :bad_request)
sugar for respond(body, :bad_request)
Source
# File lib/stir_fry/response.rb, line 65 def bad_request!(msg = "") = raise BadRequest, msg
immediately return bad request status without running any following middleware.
Source
# File lib/stir_fry/response.rb, line 47 def bad_request? = status == 400
whether or not the status is set to 400
Source
# File lib/stir_fry/response.rb, line 124 def body = resp.body
Readonly body content. To change the content you should use one of the response methods text, html, render, json, stream or other methods available to set the body in a valid way.
Source
# File lib/stir_fry/response.rb, line 154 def cache_control(*values) hash = extract_cache_control_hash(values) values.map! { |value| value.to_s.tr("_", "-") } hash.each { |key, value| values << cache_control_pair(key, value) } headers["Cache-Control"] = values.join(", ") if values.any? end
Specify response freshness policy for HTTP caches (Cache-Control header). Any number of non-value directives (:public, :private, :no_cache, :no_store, :must_revalidate, :proxy_revalidate) may be passed along with a Hash of value directives (:max_age, :s_maxage).
cache_control :public, :must_revalidate, :max_age => 60 => Cache-Control: public, must-revalidate, max-age=60
Source
# File lib/stir_fry/response.rb, line 41 def client_error? = status.between?(400, 499)
whether or not the status is set to 4xx
Source
# File lib/stir_fry/response.rb, line 141 def content_length(len = nil) headers["Content-Length"] = len.to_s if len headers["Content-Length"] end
Set the content length header on the response
Source
# File lib/stir_fry/response.rb, line 135 def content_type(kind = nil) headers["Content-Type"] = mime_type(kind) if kind headers["Content-Type"] end
Set the content type header on the response
# File lib/stir_fry/response.rb, line 170 def etag(value, weak: false, new_resource: req.post?) headers["ETag"] = etag_header_value(value, weak) return unless success? || status == 304 raise(request_safe? ? NotModified : PreconditionFailed) if etag_matches?(env["HTTP_IF_NONE_MATCH"], new_resource) raise PreconditionFailed if env["HTTP_IF_MATCH"] && !etag_matches?(env["HTTP_IF_MATCH"], new_resource) end
Set the response entity tag (HTTP āETagā header) and halt if conditional GET matches. The +value+ argument is an identifier that uniquely identifies the current version of the resource. The weak argument will allow a weak value but defaults to strong.
When the current request includes an āIf-None-Matchā header with a matching etag, execution is immediately halted. If the request method is GET or HEAD, a ā304 Not Modifiedā response is sent.
Source
# File lib/stir_fry/response.rb, line 187 def expires(amount, *values) values << {} unless values.last.is_a?(Hash) time, max_age = expires_time_and_max_age(amount) values.last.merge!(max_age: max_age) { |_key, v1, v2| v1 || v2 } cache_control(*values) headers["Expires"] = time.httpdate end
Set the Expires header and Cache-Control/max-age directive. Amount can be an integer number of seconds in the future or a Time object indicating when the response should be considered āstaleā. The remaining āvaluesā arguments are passed to the cache_control helper:
expires 500, :public, :must_revalidate => Cache-Control: public, must-revalidate, max-age=500 => Expires: Mon, 08 Jun 2009 08:50:17 GMT
Source
# File lib/stir_fry/response.rb, line 76 def html(body, stat = :ok) = answer(stat, :html, body)
respond with html content
Source
# File lib/stir_fry/response.rb, line 35 def informational? = status.between?(100, 199)
whether or not the status is set to 1xx
Source
# File lib/stir_fry/response.rb, line 58 def internal_error(body) = respond(body, :internal_server_error)
sugar for respond(body, :internal_error)
Source
# File lib/stir_fry/response.rb, line 69 def internal_error!(msg = "") = raise InternalError, msg
immediately return internal error status without running any following middleware.
Source
# File lib/stir_fry/response.rb, line 74 def json(body, stat = :ok) = answer(stat, :json, JSON.dump(body))
respond with json content
Source
# File lib/stir_fry/response.rb, line 204 def last_modified(time) return unless time time = time_for(time) headers["Last-Modified"] = time.httpdate return if env["HTTP_IF_NONE_MATCH"] check_if_modified_since(time) check_if_unmodified_since(time) end
Set the last modified time of the resource (HTTP āLast-Modifiedā header) and halt if conditional GET matches. The +time+ argument is a Time, DateTime, or other object that responds to +to_time+.
When the current request includes an āIf-Modified-Sinceā header that is equal or later than the time specified, execution is immediately halted with a ā304 Not Modifiedā response.
Source
# File lib/stir_fry/response.rb, line 52 def not_found(body) = respond(body, :not_found)
sugar for respond(body, :not_found)
Source
# File lib/stir_fry/response.rb, line 63 def not_found!(msg = "") = raise NotFound, msg
immediately return not found status without running any following middleware.
Source
# File lib/stir_fry/response.rb, line 45 def not_found? = status == 404
whether or not the status is set to 404
Source
# File lib/stir_fry/response.rb, line 50 def ok(body) = respond(body, :ok)
sugar for respond(body, :ok)
Source
# File lib/stir_fry/response.rb, line 61 def ok!(msg = "") = raise OKAY, msg
immediately return okay status without running any following middleware.
Source
# File lib/stir_fry/response.rb, line 98 def redirect(uri) http_version = env["SERVER_PROTOCOL"] if (http_version == "HTTP/1.1") && (env["REQUEST_METHOD"] != "GET") status(303) else status(302) end headers["Location"] = uri.to_s end
redirect the request to a uri
Source
# File lib/stir_fry/response.rb, line 39 def redirect? = status.between?(300, 399)
whether or not the status is set to 3xx
Source
# File lib/stir_fry/response.rb, line 120 def redirect_back = redirect(req.referer)
Sugar for redirect(request.referer)
Source
# File lib/stir_fry/response.rb, line 78 def render(klass, stat = :ok) = html(klass.render, stat)
respond with a rendered component. sugar for html(view.render)
Source
# File lib/stir_fry/response.rb, line 88 def respond(obj, stat = :ok) case obj when Hash then json(obj, stat) when StirFry::Component then render(obj, stat) when String then text(obj, stat) else status(stat) end end
respond will choose what kind of content to respond with depending on what kind of data you give it.
respond({message: "hi"}, :ok) => application/json JSON response respond(AppView, :ok) => text/html HTML Component response respond("OKAY", :ok) => text/plain response
# File lib/stir_fry/response.rb, line 216 def send_file(req, filename, attachment: false) if attachment headers["Content-Disposition"] = format('attachment; filename="%s"', File.basename(filename).gsub(/["\r\n]/, MULTIPART_FORM_DATA_REPLACEMENT_TABLE)) end serve_file(req, filename) end
Responde directly with a file.
Source
# File lib/stir_fry/response.rb, line 43 def server_error? = status.between?(500, 599)
whether or not the status is set to 5xx
Source
# File lib/stir_fry/response.rb, line 129 def status(value = nil) resp.status = Rack::Utils.status_code(value) if value resp.status end
Set the response status, and return the current status.
Source
# File lib/stir_fry/response.rb, line 80 def stream(&block) = resp.body = block.to_proc
start a streaming body that doesnāt immeadiately close the connection.
Source
# File lib/stir_fry/response.rb, line 37 def success? = status.between?(200, 299)
whether or not the status is set to 2xx
Source
# File lib/stir_fry/response.rb, line 72 def text(body, stat = :ok) = answer(stat, :text, body)
respond with text content
# File lib/stir_fry/response.rb, line 109 def uri(addr = nil, absolute: true) port_required = !req.forwarded_authority.nil? || (req.port != (req.ssl? ? 443 : 80)) uri = [host = String.new] if absolute host.concat("http#{"s" if req.ssl?}://", port_required ? req.host_with_port : req.host) end uri << (addr || req.path_info).to_s File.join uri end
Build and format a link with the current host and port.