Class: Hanami::API::Block::Context

Inherits:
Router::Block::Context
  • Object
show all
Defined in:
lib/hanami/api/block/context.rb

Overview

Execution context for Block syntax

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#backObject

Utility for redirect back using HTTP request header ‘HTTP_REFERER`

Examples:

get "/authenticate" do
  if authenticate(env)
    redirect back
  else
    # ...
  end
end

Since:

  • 0.1.0



103
104
105
# File 'lib/hanami/api/block/context.rb', line 103

def back
  env["HTTP_REFERER"] || "/"
end

#bodyString #body(value) ⇒ Object

Overloads:

  • #bodyString

    Gets the current HTTP response body

    Returns:

    • (String)

      the HTTP body

  • #body(value) ⇒ Object

    Sets the HTTP body

    Parameters:

    • value (String)

      the HTTP response body

Since:

  • 0.1.0



22
23
24
25
26
27
28
# File 'lib/hanami/api/block/context.rb', line 22

def body(value = nil)
  if value
    @body = value
  else
    @body
  end
end

#callObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hanami/api/block/context.rb', line 138

def call
  case caught
  in String => body
    [status, headers, [body]]
  in Enumerator => body
    [status, headers, body]
  in Integer => status
    #
    # NOTE: It must use `self.body` so it will pick the method defined above.
    #
    #       If `self` isn't enforced, Ruby will try to bind `body` to
    #       the current pattern matching context.
    #       When that happens, the body that was manually set is ignored,
    #       which results in a bug.
    [status, headers, [self.body || http_status(status)]]
  in [Integer => status, String => body]
    [status, headers, [body]]
  in [Integer => status, Enumerator => body]
    [status, headers, body]
  in [Integer => status, Hash => caught_headers, String => body]
    headers.merge!(caught_headers)
    [status, headers, [body]]
  in [Integer => status, Hash => caught_headers, Enumerator => body]
    headers.merge!(caught_headers)
    [status, headers, body]
  end
end

#halt(status, body = nil) ⇒ Object

Halts the flow of the block and immediately returns with the current HTTP status

Examples:

HTTP Status

get "/authenticate" do
  halt(401)

  # this code will never be reached
end

# It sets a Rack response: [401, {}, ["Unauthorized"]]

HTTP Status and body

get "/authenticate" do
  halt(401, "You shall not pass")

  # this code will never be reached
end

# It sets a Rack response: [401, {}, ["You shall not pass"]]

Parameters:

  • status (Integer)

    a valid HTTP status code

  • body (String) (defaults to: nil)

    an optional HTTP response body

Since:

  • 0.1.0



55
56
57
58
# File 'lib/hanami/api/block/context.rb', line 55

def halt(status, body = nil)
  body ||= http_status(status)
  throw :halt, [status, body]
end

#json(object, mime = "application/json") ⇒ Object

Sets a JSON response for the given object

Examples:

JSON serializable object

get "/user/:id" do
  user = UserRepository.new.find(params[:id])
  json(user)
end

JSON serializable object and custom MIME type

get "/user/:id" do
  user = UserRepository.new.find(params[:id])
  json(user, "application/vnd.api+json")
end

Parameters:

  • object (Object)

    a JSON serializable object

  • mime (String) (defaults to: "application/json")

    optional MIME type to set for the response

Since:

  • 0.1.0



125
126
127
128
129
130
131
132
133
# File 'lib/hanami/api/block/context.rb', line 125

def json(object, mime = "application/json")
  headers["Content-Type"] = mime
  case object
  in Enumerator => collection
    json_enum(collection)
  else
    JSON.generate(object)
  end
end

#redirect(url, status = 301) ⇒ Object

Redirects request and immediately halts it

Examples:

URL

get "/legacy" do
  redirect "/dashboard"

  # this code will never be reached
end

# It sets a Rack response: [301, {"Location" => "/new"}, ["Moved Permanently"]]

URL and HTTP status

get "/legacy" do
  redirect "/dashboard", 302

  # this code will never be reached
end

# It sets a Rack response: [302, {"Location" => "/new"}, ["Moved"]]

Parameters:

  • url (String)

    the destination URL

  • status (Integer) (defaults to: 301)

    an optional HTTP code for the redirect

See Also:

Since:

  • 0.1.0



86
87
88
89
# File 'lib/hanami/api/block/context.rb', line 86

def redirect(url, status = 301)
  headers["Location"] = url
  halt(status)
end