Class: Hanami::Action::Config

Inherits:
Dry::Configurable::Config
  • Object
show all
Defined in:
lib/hanami/action/config.rb,
lib/hanami/action/config/formats.rb

Overview

Config for ‘Hanami::Action` classes.

See Also:

Since:

  • 2.0.0

Defined Under Namespace

Classes: Formats

Constant Summary collapse

DEFAULT_PUBLIC_DIRECTORY =

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

Default public directory

This serves as the root directory for file downloads

Since:

  • 1.0.0

"public"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookiesHash{Symbol=>String}

Sets default cookie options for all responses.

By default this, is an empty hash.

Examples:

config.cookies = {
  domain: "hanamirb.org",
  path: "/action",
  secure: true,
  httponly: true
}

Returns:

  • (Hash{Symbol=>String})

    the cookie options

Since:

  • 0.4.0



# File 'lib/hanami/action/config.rb', line 119

#default_charsetString

Sets a charset (character set) as default fallback for all the requests without a strict requirement for the charset.

By default, this value is nil.

Returns:

  • (String)

See Also:

Since:

  • 0.3.0



# File 'lib/hanami/action/config.rb', line 73

#default_headersHash{String=>String}

Sets default headers for all responses.

By default, this is an empty hash.

Examples:

config.default_headers = {"X-Frame-Options" => "DENY"}

Returns:

  • (Hash{String=>String})

    the headers

See Also:

Since:

  • 0.4.0



# File 'lib/hanami/action/config.rb', line 86

#default_tld_lengthInteger

Sets the default TLD length for host names. It is used to extract the subdomain(s) in ‘Request#subdomains`.

Defaults to 1.

Examples:

# For *.example.com
config.default_tld_length = 1

# Or for *.example.co.uk
config.default_tld_length = 2

Returns:

  • (Integer)

    the number of subdomains

Since:

  • 2.3.0



# File 'lib/hanami/action/config.rb', line 101

#formatsConfig::Formats (readonly)

Returns the format config for the action.

Returns:

Since:

  • 2.0.0



# File 'lib/hanami/action/config.rb', line 65

#handled_exceptionsHash{Exception=>Integer}

Specifies how to handle exceptions with an HTTP status.

Raised exceptions will return the corresponding HTTP status.

Examples:

config.handled_exceptions = {ArgumentError => 400}

Returns:

  • (Hash{Exception=>Integer})

    exception classes as keys and HTTP statuses as values

Since:

  • 0.2.0



# File 'lib/hanami/action/config.rb', line 23

#public_directoryString

Sets the path to public directory. This directory is used for file downloads.

This given directory will be appended onto the root directory.

By default, the public directory is ‘“public”`.

Examples:

config.public_directory = "public"
config.public_directory # => "/path/to/root/public"

Returns:

  • (String)

    the public directory path

See Also:

Since:

  • 2.0.0



166
167
168
169
# File 'lib/hanami/action/config.rb', line 166

def public_directory
  # This must be a string, for Rack compatibility
  root_directory.join(super).to_s
end

#root_directoryString

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.

Sets the the for the public directory, which is used for file downloads. This must be an existent directory.

Defaults to the current working directory.

Returns:

  • (String)

    the directory path

Since:

  • 1.0.0



# File 'lib/hanami/action/config.rb', line 137

Instance Method Details

#handle_exception(exceptions) ⇒ void

This method returns an undefined value.

Specifies how to handle exceptions with an HTTP status

Raised exceptions will return the corresponding HTTP status

The specified exceptions will be merged with any previously configured exceptions

Examples:

config.handle_exceptions(ArgumentError => 400}

Parameters:

  • exceptions (Hash{Exception=>Integer})

    exception classes as keys and HTTP statuses as values

See Also:

Since:

  • 0.2.0



54
55
56
57
58
59
60
61
62
63
# File 'lib/hanami/action/config.rb', line 54

def handle_exception(exceptions)
  self.handled_exceptions = handled_exceptions
    .merge(exceptions)
    .sort do |(ex1, _), (ex2, _)|
      next 0 if [ex1, ex2].any?(String)

      ex1.ancestors.include?(ex2) ? -1 : 1
    end
    .to_h
end