Class: Net::HTTP::StructuredFieldValues::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/net/http/structured_field_values/parser.rb

Overview

RFC8941 compliant parser which parses HTTP fields into Ruby objects.

Defined Under Namespace

Classes: ParseError

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • input (String)

    input bytes to be parsed



26
27
28
29
30
31
# File 'lib/net/http/structured_field_values/parser.rb', line 26

def initialize(input)
  @scanner = StringScanner.new(input.encode(Encoding::ASCII))
  remove_leading_spaces
rescue Encoding::UndefinedConversionError
  raise ParseError, 'Unexpected input'
end

Instance Method Details

#parse_as(type) ⇒ Object

Parameters:

  • type (String)

    type of the field to be parsed, must be one of ‘list’, ‘dictionary’ or ‘item’

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
# File 'lib/net/http/structured_field_values/parser.rb', line 35

def parse_as(type)
  raise ArgumentError, "Invalid type: #{type}" unless TOP_LEVEL_TYPES.include?(type)

  send(:"parse_as_#{type}").tap do
    remove_leading_spaces
    raise ParseError, 'Unexpected input' unless scanner.eos?
  end
end