Class: Hanami::Action::Config::Formats Private

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

Overview

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

Action format configuration.

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accepted: [], default: nil, mapping: {}) ⇒ Formats

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.

Returns a new instance of Formats.

Since:

  • 2.0.0



62
63
64
65
66
# File 'lib/hanami/action/config/formats.rb', line 62

def initialize(accepted: [], default: nil, mapping: {})
  @accepted = accepted
  @default = default
  @mapping = mapping
end

Instance Attribute Details

#acceptedObject

The array of formats to accept requests by.

Examples:

config.formats.accepted = [:html, :json]
config.formats.accepted # => [:html, :json]

Since:

  • 2.0.0



28
29
30
# File 'lib/hanami/action/config/formats.rb', line 28

def accepted
  @accepted
end

#defaultSymbol?

Returns the default format name.

When a request is received that cannot

Examples:

@config.formats.default # => :json

Returns:

  • (Symbol, nil)

    the default format name, if any

Since:

  • 2.0.0



58
59
60
# File 'lib/hanami/action/config/formats.rb', line 58

def default
  @default
end

#mappingObject (readonly)

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:

  • 2.0.0



18
19
20
# File 'lib/hanami/action/config/formats.rb', line 18

def mapping
  @mapping
end

Instance Method Details

#accept(*formats) ⇒ Object

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:

  • 2.3.0



90
91
92
93
# File 'lib/hanami/action/config/formats.rb', line 90

def accept(*formats)
  self.default = formats.first if default.nil?
  self.accepted = accepted | formats
end

#accept_typesObject

Returns an array of all accepted media types.

Since:

  • 2.3.0



231
232
233
# File 'lib/hanami/action/config/formats.rb', line 231

def accept_types
  accepted.map { |format| mapping[format]&.accept_types }.flatten(1).compact
end

#accept_types_for(format) ⇒ Object Also known as: mime_types_for

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:

  • 2.0.0



270
271
272
# File 'lib/hanami/action/config/formats.rb', line 270

def accept_types_for(format)
  mapping[format]&.accept_types || []
end

#accepted_formats(standard_formats = {}) ⇒ Object

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:

  • 2.0.0



96
97
98
99
100
101
102
103
# File 'lib/hanami/action/config/formats.rb', line 96

def accepted_formats(standard_formats = {})
  accepted.to_h { |format|
    [
      format,
      mapping.fetch(format) { standard_formats[format] }
    ]
  }
end

#add(format) ⇒ self #add(format, mime_type) ⇒ self #add(format, mime_types) ⇒ self

Overloads:

  • #add(format) ⇒ self

    Adds and enables a format.

    Examples:

    config.formats.add(:json)
    

    Parameters:

    • format (Symbol)
  • #add(format, mime_type) ⇒ self

    Adds a custom format to MIME type mapping and enables the format. Adds a format mapping to a single MIME type.

    Examples:

    config.formats.add(:json, "application/json")
    

    Parameters:

    • format (Symbol)
    • mime_type (String)
  • #add(format, mime_types) ⇒ self

    Adds a format mapping to multiple MIME types.

    Examples:

    config.formats.add(:json, ["application/json+scim"])
    

    Parameters:

    • format (Symbol)
    • mime_types (Array<String>)

Returns:

  • (self)

Since:

  • 2.0.0



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/hanami/action/config/formats.rb', line 173

def add(format, mime_types)
  msg = "    Hanami::Action `config.formats.add` is deprecated and will be removed in Hanami 2.4.\n\n    Please use `config.formats.register` instead.\n\n    See https://guides.hanamirb.org/v2.3/actions/formats-and-mime-types/ for details.\n  TEXT\n  warn(msg, category: :deprecated)\n\n  mime_type = Array(mime_types).first\n\n  # The old behaviour would have subsequent mime types _replacing_ previous ones\n  mapping.reject! { |_, format| format.media_type == mime_type }\n\n  register(format, Array(mime_types).first, accept_types: mime_types)\n\n  accept(format) unless @accepted.include?(format)\n\n  self\nend\n"

#any?Boolean

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.

Returns:

  • (Boolean)

Since:

  • 2.0.0



203
204
205
# File 'lib/hanami/action/config/formats.rb', line 203

def any?
  @accepted.any?
end

#clearself

Clears any previously added mappings and format values.

Returns:

  • (self)

Since:

  • 2.0.0



219
220
221
222
223
224
225
# File 'lib/hanami/action/config/formats.rb', line 219

def clear
  @accepted = []
  @default = nil
  @mapping = {}

  self
end

#content_types_for(format) ⇒ Object

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:

  • 2.0.0



275
276
277
# File 'lib/hanami/action/config/formats.rb', line 275

def content_types_for(format)
  mapping[format]&.content_types || []
end

#empty?Boolean

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.

Returns:

  • (Boolean)

Since:

  • 2.0.0



197
198
199
# File 'lib/hanami/action/config/formats.rb', line 197

def empty?
  accepted.empty?
end

#format_for(media_type) ⇒ Symbol, NilClass

Retrieve the format name associated with the given media type

Examples:

@config.formats.format_for("application/json") # => :json

Parameters:

  • media_type (String)

    the media Type

Returns:

  • (Symbol, NilClass)

    the associated format name, if any

See Also:

Since:

  • 2.0.0



248
249
250
# File 'lib/hanami/action/config/formats.rb', line 248

def format_for(media_type)
  mapping.values.reverse.find { |format| format.media_type == media_type }&.name
end

#map(&blk) ⇒ Object

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:

  • 2.0.0



209
210
211
# File 'lib/hanami/action/config/formats.rb', line 209

def map(&blk)
  @accepted.map(&blk)
end

#media_type_for(format) ⇒ String? Also known as: mime_type_for

Returns the media type associated with the given format.

Examples:

@config.formats.media_type_for(:json) # => "application/json"

Parameters:

  • format (Symbol)

    the format name

Returns:

  • (String, nil)

    the associated media type, if any

See Also:

Since:

  • 2.3.0



265
266
267
# File 'lib/hanami/action/config/formats.rb', line 265

def media_type_for(format)
  mapping[format]&.media_type
end

#register(format, media_type, accept_types: [media_type], content_types: [media_type]) ⇒ self

Registers a format and its associated media types.

Examples:

config.formats.register(:scim, media_type: "application/json+scim")

config.formats.register(
  :jsonapi,
  "application/vnd.api+json",
  accept_types: ["application/vnd.api+json", "application/json"],
  content_types: ["application/vnd.api+json", "application/json"]
)

Parameters:

  • format (Symbol)

    the format name

  • media_type (String)

    the format’s media type

  • accept_types (Array<String>) (defaults to: [media_type])

    media types to accept in request ‘Accept` headers

  • content_types (Array<String>) (defaults to: [media_type])

    media types to accept in request ‘Content-Type` headers

Returns:

  • (self)

Since:

  • 2.3.0



131
132
133
134
135
136
137
138
139
140
# File 'lib/hanami/action/config/formats.rb', line 131

def register(format, media_type, accept_types: [media_type], content_types: [media_type])
  mapping[format] = Mime::Format.new(
    name: format.to_sym,
    media_type: media_type,
    accept_types: accept_types,
    content_types: content_types
  )

  self
end

#valuesObject

See Also:

Since:

  • 2.0.0



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hanami/action/config/formats.rb', line 34

def values
  msg = "    Hanami::Action `config.formats.values` is deprecated and will be removed in Hanami 2.4.\n\n    Please use `config.formats.accepted` instead.\n\n    See https://guides.hanamirb.org/v2.3/actions/formats-and-mime-types/ for details.\n  TEXT\n  warn(msg, category: :deprecated)\n\n  accepted\nend\n"