Class: Hanami::Middleware::BodyParser::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/middleware/body_parser/parser.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.

Body parser abstract class

Since:

  • 2.0.0

Direct Known Subclasses

FormParser, JsonParser

Constant Summary collapse

DEFAULT_MIME_TYPES =

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:

  • 2.0.0

[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_types: DEFAULT_MIME_TYPES) ⇒ Parser

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 Parser.

Since:

  • 2.0.0



30
31
32
# File 'lib/hanami/middleware/body_parser/parser.rb', line 30

def initialize(mime_types: DEFAULT_MIME_TYPES)
  @mime_types = self.class.mime_types + mime_types
end

Instance Attribute Details

#mime_typesArray<String> (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.

This method is abstract.

Return supported mime types

Examples:

require "hanami/middleware/body_parser"

class XMLParser < Hanami::Middleware::BodyParser::Parser
  def self.mime_types
    ["application/xml", "text/xml"]
  end
end

Returns:

  • (Array<String>)

    supported MIME types

Since:

  • 2.0.0



27
28
29
# File 'lib/hanami/middleware/body_parser/parser.rb', line 27

def mime_types
  @mime_types
end

Instance Method Details

#parse(body, env = {}) ⇒ Hash

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.

This method is abstract.

Parse raw HTTP request body

Examples:

require "hanami/middleware/body_parser"

class XMLParser < Hanami::Middleware::BodyParser::Parser
  def parse(body)
    # XML parsing
    # ...
  rescue => exception
    raise Hanami::Middleware::BodyParser::BodyParsingError.new(exception.message)
  end
end

Parameters:

  • body (String)

    HTTP request body

  • env (Hash) (defaults to: {})

    Rack env

Returns:

  • (Hash)

    the result of the parsing

Raises:

Since:

  • 2.0.0



58
59
60
# File 'lib/hanami/middleware/body_parser/parser.rb', line 58

def parse(body, env = {}) # rubocop:disable Lint/UnusedMethodArgument
  raise NoMethodError
end