class Tiny::Response

Overview

A facade which sits in front of HTTP::Server::Response, providing a few helper methods. The object is passed into the block of Tiny's serve method.

serve do |request, response|
  # Return text to the client
  response.send "Unicorns!"

  # Return text with a specific status code
  response.send 500, "An error occurred"

  # Return a JSON object
  response.json {
    "testing" => "test"
  }
end

Defined in:

tiny/response.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(context : HTTP::Server::Context) #

Create the response object


[View source]

Instance Method Detail

def allow_header(header : String) #

Set the Access-Control-Allow-Headers header


[View source]
def allow_origin(hostname : String?) #

Set the Access-Control-Allow-Origin header

response.allow_origin "example.com"

[View source]
def allowed_headers : Array(String) #

A list of allowed headers for CORS purposes


[View source]
def error(code : Int32, message : String) #

Send an error response with a specific status code

response.error 403, "Something happened"

[View source]
def error(message : String) #

Send an error response with a 500 status code

response.error "Something happened"

[View source]
def json(code : Int32, object : JSON::Type) #

Send a JSON response with a specific status code

response.json 500, {
  "error": "Something happened"
}

[View source]
def json(object : JSON::Type) #

Send a JSON response with a 200 status code

response.json {
  "rainbows": "unicorns"
}

[View source]
def request_methods(methods : Array(String)) #

Set the Access-Control-Request-Method header

response.request_methods ["GET", "POST"]

[View source]
def send(code : Int32) #

Send an empty response with a specific status code

response.send 200

[View source]
def send(code : Int32, *message : String) #

Send a response with a 200 status code

response.send 403, "You're not allowed to do that"

Multi-line responses can be sent by passing multiple strings

response.send 403,
  "Oh, crumbs!",
  "You're not allowed to do that"

[View source]
def send(*message : String) #

Send a response with a 200 status code

response.send "Tada!"

Multi-line responses can be sent by passing multiple strings

response.send
  "First Line",
  "Second Line",
  "Third Line"

[View source]

Macro Detail

macro method_missing(call) #

Cascade missing methods down to the server response


[View source]