Module: Hanami::Action::Rack

Defined in:
gems/gems/hanami-controller-1.3.2/lib/hanami/action/rack.rb,
gems/gems/hanami-controller-1.3.2/lib/hanami/action/rack/file.rb,
gems/gems/hanami-controller-1.3.2/lib/hanami/action/rack/callable.rb,
gems/gems/hanami-controller-1.3.3/lib/hanami/action/rack.rb,
gems/gems/hanami-controller-1.3.3/lib/hanami/action/rack/file.rb,
gems/gems/hanami-controller-1.3.3/lib/hanami/action/rack/errors.rb,
gems/gems/hanami-controller-1.3.3/lib/hanami/action/rack/callable.rb

Overview

Rack integration API

Since:

  • 0.1.0

Constant Summary

ROUTER_PARSED_BODY =

The key that returns router parsed body from the Rack env

Since:

  • 0.1.0

'router.parsed_body'.freeze

Instance Method Summary collapse

Instance Method Details

#body=(body) ⇒ void (private)

This method returns an undefined value.

Sets the body of the response

Examples:

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    # ...
    self.body = 'Hi!'
  end
end

Parameters:

  • body (String)

    the body of the response

Since:

  • 0.1.0

def body=(body)
  body   = Array(body) unless body.respond_to?(:each)
  @_body = body
end

#head?TrueClass, FalseClass (private)

Check if the current request is a HEAD

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.3.2

def head?
  request_method == HEAD
end

#headersHash (protected)

Gets the headers from the response

Examples:

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    # ...
    self.headers            # => { ... }
    self.headers.merge!({'X-Custom' => 'OK'})
  end
end

Returns:

  • (Hash)

    the HTTP headers from the response

Since:

  • 0.1.0

def headers
  @headers
end

#parsed_request_bodyObject (protected)

Deprecated.

Return parsed request body

Since:

  • 0.1.0

def parsed_request_body
  Hanami::Utils::Deprecation.new('#parsed_request_body is deprecated and it will be removed in future versions')
  @_env.fetch(ROUTER_PARSED_BODY, nil)
end

#requestHanami::Action::Request (protected)

Returns a Hanami specialized rack request

Examples:

require 'hanami/controller'

class Create
  include Hanami::Action

  def call(params)
    ip     = request.ip
    secure = request.ssl?
  end
end

Returns:

Since:

  • 0.3.1

def request
  @request ||= ::Hanami::Action::Request.new(@_env)
end

#request_idString (protected)

Calculates an unique ID for the current request

Returns:

  • (String)

    The unique ID

Since:

  • 0.3.0

def request_id
  # FIXME make this number configurable and document the probabilities of clashes
  @request_id ||= SecureRandom.hex(DEFAULT_REQUEST_ID_LENGTH)
end

#send_file(path) ⇒ void (private)

This method returns an undefined value.

Send a file as response. This method only sends files from the public directory

It automatically handle the following cases:

  • Content-Type and Content-Length

  • File Not found (returns a 404)

  • Conditional GET (via If-Modified-Since header)

  • Range requests (via Range header)

Examples:

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    # ...
    send_file Pathname.new('path/to/file')
  end
end

Parameters:

  • path (String, Pathname)

    the body of the response

Since:

  • 0.4.3

def send_file(path)
  _send_file(
    File.new(path, self.class.configuration.public_directory).call(@_env)
  )
end

#status=(status) ⇒ void (private)

This method returns an undefined value.

Sets the HTTP status code for the response

Examples:

require 'hanami/controller'

class Create
  include Hanami::Action

  def call(params)
    # ...
    self.status = 201
  end
end

Parameters:

  • status (Fixnum)

    an HTTP status code

Since:

  • 0.1.0

def status=(status)
  @_status = status
end

#unsafe_send_file(path) ⇒ void (private)

This method returns an undefined value.

Send a file as response from anywhere in the file system.

Examples:

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    # ...
    unsafe_send_file Pathname.new('/tmp/path/to/file')
  end
end

Parameters:

  • path (String, Pathname)

    path to the file to be sent

See Also:

Since:

  • 1.0.0

def unsafe_send_file(path)
  directory = self.class.configuration.root_directory if Pathname.new(path).relative?

  _send_file(
    File.new(path, directory).call(@_env)
  )
end