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



53
54
55
56
57
58
59
60
# File 'lib/hanami/action/config/formats.rb', line 53

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

  register_default_body_parsers
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

#body_parsersHash{String => #call} (readonly)

The registered body parsers, as a hash mapping content types to callable parsers.

Returns:

  • (Hash{String => #call})

Since:

  • x.x.x



36
37
38
# File 'lib/hanami/action/config/formats.rb', line 36

def body_parsers
  @body_parsers
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



49
50
51
# File 'lib/hanami/action/config/formats.rb', line 49

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



80
81
82
83
# File 'lib/hanami/action/config/formats.rb', line 80

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



174
175
176
# File 'lib/hanami/action/config/formats.rb', line 174

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



213
214
215
# File 'lib/hanami/action/config/formats.rb', line 213

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



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

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

#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



146
147
148
# File 'lib/hanami/action/config/formats.rb', line 146

def any?
  @accepted.any?
end

#body_parser_for(content_type) ⇒ #call?

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.

Finds the parser for a content type.

Parameters:

  • content_type (String)

    the content type

Returns:

  • (#call, nil)

    the parser callable, if registered

Since:

  • 2.0.0



239
240
241
# File 'lib/hanami/action/config/formats.rb', line 239

def body_parser_for(content_type)
  @body_parsers[content_type&.downcase]
end

#clearself

Clears any previously added mappings and format values.

Returns:

  • (self)

Since:

  • 2.0.0



162
163
164
165
166
167
168
# File 'lib/hanami/action/config/formats.rb', line 162

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



218
219
220
# File 'lib/hanami/action/config/formats.rb', line 218

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



140
141
142
# File 'lib/hanami/action/config/formats.rb', line 140

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



191
192
193
# File 'lib/hanami/action/config/formats.rb', line 191

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



152
153
154
# File 'lib/hanami/action/config/formats.rb', line 152

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



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

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

#register(format, media_type, accept_types: [media_type], content_types: [media_type], parser: nil) ⇒ 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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hanami/action/config/formats.rb', line 121

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

  if parser
    Array(content_types).each do |ct|
      @body_parsers[ct.downcase] = parser
    end
  end

  self
end