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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media_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



34
35
36
# File 'lib/hanami/middleware/body_parser/parser.rb', line 34

def initialize(media_types: [])
  @media_types = self.class.media_types + media_types
end

Instance Attribute Details

#media_typesArray<String> (readonly) Also known as: mime_types

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 media types

Examples:

require "hanami/middleware/body_parser"

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

Returns:

  • (Array<String>)

    supported media types

Since:

  • 2.0.0



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

def media_types
  @media_types
end

Class Method Details

.media_typesObject Also known as: mime_types

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



11
# File 'lib/hanami/middleware/body_parser/parser.rb', line 11

def media_types = []

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



62
63
64
# File 'lib/hanami/middleware/body_parser/parser.rb', line 62

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