Class: Hanami::Action::Config::Formats Private
- Inherits:
-
Object
- Object
- Hanami::Action::Config::Formats
- 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.
Instance Attribute Summary collapse
-
#accepted ⇒ Object
The array of formats to accept requests by.
-
#body_parsers ⇒ Hash{String => #call}
readonly
The registered body parsers, as a hash mapping content types to callable parsers.
-
#default ⇒ Symbol?
Returns the default format name.
- #mapping ⇒ Object readonly private
Instance Method Summary collapse
- #accept(*formats) ⇒ Object private
-
#accept_types ⇒ Object
Returns an array of all accepted media types.
- #accept_types_for(format) ⇒ Object (also: #mime_types_for) private
- #accepted_formats(standard_formats = {}) ⇒ Object private
- #any? ⇒ Boolean private
-
#body_parser_for(content_type) ⇒ #call?
private
Finds the parser for a content type.
-
#clear ⇒ self
Clears any previously added mappings and format values.
- #content_types_for(format) ⇒ Object private
- #empty? ⇒ Boolean private
-
#format_for(media_type) ⇒ Symbol, NilClass
Retrieve the format name associated with the given media type.
-
#initialize(accepted: [], default: nil, mapping: {}) ⇒ Formats
constructor
private
A new instance of Formats.
- #map(&blk) ⇒ Object private
-
#media_type_for(format) ⇒ String?
(also: #mime_type_for)
Returns the media type associated with the given format.
-
#register(format, media_type, accept_types: [media_type], content_types: [media_type], parser: nil) ⇒ self
Registers a format and its associated media types.
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.
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
#accepted ⇒ Object
The array of formats to accept requests by.
28 29 30 |
# File 'lib/hanami/action/config/formats.rb', line 28 def accepted @accepted end |
#body_parsers ⇒ Hash{String => #call} (readonly)
The registered body parsers, as a hash mapping content types to callable parsers.
36 37 38 |
# File 'lib/hanami/action/config/formats.rb', line 36 def body_parsers @body_parsers end |
#default ⇒ Symbol?
Returns the default format name.
When a request is received that cannot
49 50 51 |
# File 'lib/hanami/action/config/formats.rb', line 49 def default @default end |
#mapping ⇒ Object (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.
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.
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_types ⇒ Object
Returns an array of all accepted media types.
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.
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.
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.
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.
239 240 241 |
# File 'lib/hanami/action/config/formats.rb', line 239 def body_parser_for(content_type) @body_parsers[content_type&.downcase] end |
#clear ⇒ self
Clears any previously added mappings and format values.
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.
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.
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
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.
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.
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.
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 |