Module: Hanami::Action::Mime Private

Defined in:
lib/hanami/action/mime.rb,
lib/hanami/action/mime/request_mime_weight.rb

Overview

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

Since:

  • 0.1.0

Defined Under Namespace

Classes: RequestMimeWeight

Constant Summary collapse

TYPES =

Most commom media types used for responses

Since:

  • 1.0.0

{
  atom: "application/atom+xml",
  avi: "video/x-msvideo",
  bmp: "image/bmp",
  bz2: "application/x-bzip2",
  bz: "application/x-bzip",
  chm: "application/vnd.ms-htmlhelp",
  css: "text/css",
  csv: "text/csv",
  flv: "video/x-flv",
  form: "application/x-www-form-urlencoded",
  gif: "image/gif",
  gz: "application/x-gzip",
  h264: "video/h264",
  html: "text/html",
  ico: "image/vnd.microsoft.icon",
  ics: "text/calendar",
  jpg: "image/jpeg",
  js: "application/javascript",
  json: "application/json",
  manifest: "text/cache-manifest",
  mov: "video/quicktime",
  mp3: "audio/mpeg",
  mp4: "video/mp4",
  mp4a: "audio/mp4",
  mpg: "video/mpeg",
  multipart: "multipart/form-data",
  oga: "audio/ogg",
  ogg: "application/ogg",
  ogv: "video/ogg",
  pdf: "application/pdf",
  pgp: "application/pgp-encrypted",
  png: "image/png",
  psd: "image/vnd.adobe.photoshop",
  rss: "application/rss+xml",
  rtf: "application/rtf",
  sh: "application/x-sh",
  svg: "image/svg+xml",
  swf: "application/x-shockwave-flash",
  tar: "application/x-tar",
  torrent: "application/x-bittorrent",
  tsv: "text/tab-separated-values",
  txt: "text/plain",
  uri: "text/uri-list",
  vcs: "text/x-vcalendar",
  wav: "audio/x-wav",
  webm: "video/webm",
  wmv: "video/x-ms-wmv",
  woff2: "application/font-woff2",
  woff: "application/font-woff",
  wsdl: "application/wsdl+xml",
  xhtml: "application/xhtml+xml",
  xml: "application/xml",
  xslt: "application/xslt+xml",
  yml: "text/yaml",
  zip: "application/zip"
}.freeze
ANY_TYPE =

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.

Since:

  • 0.1.0

"*/*"
Format =

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.

Since:

  • 0.1.0

Data.define(:name, :media_type, :accept_types, :content_types) do
  def initialize(name:, media_type:, accept_types: [media_type], content_types: [media_type])
    super
  end
end

Class Method Summary collapse

Class Method Details

.best_q_match(q_value_header, available_mimes) ⇒ 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.

Patched version of Rack::Utils.best_q_match.



262
263
264
265
266
267
268
269
# File 'lib/hanami/action/mime.rb', line 262

def best_q_match(q_value_header, available_mimes)
  ::Rack::Utils.q_values(q_value_header).each_with_index.map { |(req_mime, quality), index|
    match = available_mimes.find { |am| ::Rack::Mime.match?(am, req_mime) }
    next unless match

    RequestMimeWeight.new(req_mime, quality, index, match)
  }.compact.max&.format
end

.content_type_with_charset(content_type, charset) ⇒ String

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 string combining the given content type and charset, intended for setting as a ‘Content-Type` header.

Examples:

Mime.content_type_with_charset("application/json", "utf-8")
# => "application/json; charset=utf-8"

Parameters:

  • content_type (String)
  • charset (String)

Returns:

  • (String)

Since:

  • 0.1.0



249
250
251
# File 'lib/hanami/action/mime.rb', line 249

def content_type_with_charset(content_type, charset)
  "#{content_type}; charset=#{charset}"
end

.enforce_accept(request, config) ⇒ 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.

Yields if an action is configured with ‘formats`, the request has an `Accept` header, and none of the Accept types matches the accepted formats. The given block is expected to halt the request handling.

If any of these conditions are not met, then the request is acceptable and the method returns without yielding.

See Also:

Since:

  • 0.1.0



127
128
129
130
131
132
133
134
# File 'lib/hanami/action/mime.rb', line 127

def enforce_accept(request, config)
  return unless request.accept_header?

  accept_types = ::Rack::Utils.q_values(request.accept).map(&:first)
  return if accept_types.any? { |type| accepted_type?(type, config) }

  yield
end

.enforce_content_type(request, config) ⇒ 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.

Yields if an action is configured with ‘formats`, the request has a `Content-Type` header, and the content type does not match the accepted formats. The given block is expected to halt the request handling.

If any of these conditions are not met, then the request is acceptable and the method returns without yielding.

See Also:

Since:

  • 0.1.0



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/hanami/action/mime.rb', line 147

def enforce_content_type(request, config)
  # Compare media type (without parameters) instead of full Content-Type header to avoid
  # false negatives (e.g., multipart/form-data; boundary=...)
  media_type = request.media_type

  return if media_type.nil?

  return if accepted_content_type?(media_type, config)

  yield
end

.format_and_media_type(value, config) ⇒ Array<(Symbol, String)>

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 format name and content type pair for a given format name or content type string.

Examples:

format_and_media_type(:json, config)
# => [:json, "application/json"]

format_and_media_type("application/json", config)
# => [:json, "application/json"]

Unknown format name

format_and_media_type(:unknown, config)
# raises Hanami::Action::UnknownFormatError

Unknown content type

format_and_media_type("application/unknown", config)
# => [nil, "application/unknown"]

Returns:

  • (Array<(Symbol, String)>)

Raises:

Since:

  • 0.1.0



225
226
227
228
229
230
231
232
233
234
# File 'lib/hanami/action/mime.rb', line 225

def format_and_media_type(value, config)
  case value
  when Symbol
    [value, format_to_media_type(value, config)]
  when String
    [format_from_media_type(value, config), value]
  else
    raise UnknownFormatError.new(value)
  end
end

.format_from_media_type(media_type, config) ⇒ Symbol?

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 format name for the given content type.

The format name will come from the configured formats, if such a format is configured there, or instead from the default list of formats in ‘Mime::TYPES`.

Returns nil if no matching format can be found.

This is used to return the format name a Response.

Examples:

format_from_media_type("application/json;charset=utf-8", config) # => :json

Returns:

  • (Symbol, nil)

See Also:

Since:

  • 0.1.0



195
196
197
198
199
200
# File 'lib/hanami/action/mime.rb', line 195

def format_from_media_type(media_type, config)
  return if media_type.nil?

  mt = media_type.split(";").first
  config.formats.format_for(mt) || MEDIA_TYPES_TO_FORMATS[mt]&.name
end

.response_content_type_with_charset(request, config) ⇒ String

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 string combining a media type and charset, intended for setting as the ‘Content-Type` header for the response to the given request.

This uses the request’s ‘Accept` header (if present) along with the configured formats to determine the best content type to return.

Returns:

  • (String)

See Also:

Since:

  • 0.1.0



170
171
172
173
174
175
# File 'lib/hanami/action/mime.rb', line 170

def response_content_type_with_charset(request, config)
  content_type_with_charset(
    response_content_type(request, config),
    config.default_charset || Action::DEFAULT_CHARSET
  )
end