Module: Linzer::HTTP::StructuredField

Defined in:
lib/linzer/http/structured_field.rb

Overview

Utilities for serializing HTTP Structured Fields as defined in RFC 8941.

This module currently provides helpers for serializing HTTP Message Signature parameters as used by RFC 9421.

See Also:

Constant Summary collapse

InnerList =
Starry::InnerList
Item =
Starry::Item

Class Method Summary collapse

Class Method Details

.parse_dictionary(str, field_name: nil) ⇒ Hash<String, Object>

Parses an RFC 8941 Structured Field Dictionary.

Parameters:

  • str (String)

    the serialized dictionary value

  • field_name (String, nil) (defaults to: nil)

    optional field name for contextual errors

Returns:

  • (Hash<String, Object>)

    parsed structured field dictionary

Raises:

See Also:



24
25
26
27
28
29
30
31
32
33
# File 'lib/linzer/http/structured_field.rb', line 24

def self.parse_dictionary(str, field_name: nil)
  Starry.parse_dictionary(str)
# rescue Starry::ParseError => ex
# https://github.com/takemar/starry/pull/4
rescue => ex
  cannot_parse = "Cannot parse %sfield!"
  raise Error,
        cannot_parse % [field_name ? "\"#{field_name}\" " : nil],
        cause: ex
end

.parse_item(item) ⇒ Starry::Item

Parses an RFC 8941 Structured Field Item.

Parameters:

  • item (String)

    serialized structured field item

Returns:

  • (Starry::Item)

    parsed structured field item

Raises:

See Also:



42
43
44
45
46
47
48
# File 'lib/linzer/http/structured_field.rb', line 42

def self.parse_item(item)
  Starry.parse_item(item)
# rescue Starry::ParseError => ex
# https://github.com/takemar/starry/pull/4
rescue => ex
  raise Error, "Invalid/unparseable HTTP field item", cause: ex
end

.parse_list(list) ⇒ Array<Object>

Parses an RFC 8941 Structured Field List.

Parameters:

  • list (String)

    serialized structured field list

Returns:

  • (Array<Object>)

    parsed structured field list members

Raises:

See Also:



57
58
59
60
61
62
63
# File 'lib/linzer/http/structured_field.rb', line 57

def self.parse_list(list)
  Starry.parse_list(list)
# rescue Starry::ParseError => ex
# https://github.com/takemar/starry/pull/4
rescue => ex
  raise Error, "Invalid/unparseable HTTP field list", cause: ex
end

.serialize(obj) ⇒ String

Serializes an object into RFC 8941 Structured Field format.

Parameters:

  • obj (Object)

    structured field value

Returns:

  • (String)

    serialized structured field value

Raises:

See Also:



102
103
104
105
106
# File 'lib/linzer/http/structured_field.rb', line 102

def self.serialize(obj)
  Starry.serialize(obj)
rescue Starry::SerializeError => ex
  raise Error, ex.message, cause: ex
end

.serialize_dictionary(hsh) ⇒ String

Serializes a dictionary into RFC 8941 Structured Field format.

Parameters:

  • hsh (Hash)

    structured field dictionary

Returns:

  • (String)

    serialized structured field dictionary

Raises:

See Also:



72
73
74
75
76
77
78
# File 'lib/linzer/http/structured_field.rb', line 72

def self.serialize_dictionary(hsh)
  Starry.serialize_dictionary(hsh)
# rescue Starry::SerializeError => ex
# https://github.com/takemar/starry/pull/4
rescue => ex
  raise Error, ex.message, cause: ex
end

.serialize_item(item) ⇒ String

Serializes a Structured Field Item into RFC 8941 format.

This helper serializes a single Structured Field Item, such as a string, integer, token, boolean, date, byte sequence, or an already constructed Starry::Item.

Parameters:

  • item (Object)

    structured field item value

Returns:

  • (String)

    serialized structured field item

Raises:

See Also:



119
120
121
122
123
# File 'lib/linzer/http/structured_field.rb', line 119

def self.serialize_item(item)
  Starry.serialize_item(item)
rescue Starry::SerializeError => ex
  raise Error, ex.message, cause: ex
end

.serialize_list(arr) ⇒ String

Serializes a list into RFC 8941 Structured Field format.

Parameters:

  • arr (Array)

    structured field list members

Returns:

  • (String)

    serialized structured field list

Raises:

See Also:



87
88
89
90
91
92
93
# File 'lib/linzer/http/structured_field.rb', line 87

def self.serialize_list(arr)
  Starry.serialize_list(arr)
# rescue Starry::SerializeError => ex
# https://github.com/takemar/starry/pull/4
rescue => ex
  raise Error, ex.message, cause: ex
end

.serialize_parameters(parameters) ⇒ String

Serializes Structured Field Parameters into RFC 8941 format.

Parameters are serialized according to the Structured Fields parameter syntax defined in RFC 8941 Section 3.1.2.

Parameters:

  • parameters (Hash{String,Symbol => Object})

    parameter names and values

Returns:

  • (String)

    serialized structured field parameters

Raises:

See Also:



136
137
138
139
140
141
142
# File 'lib/linzer/http/structured_field.rb', line 136

def self.serialize_parameters(parameters)
  Starry.serialize_parameters(parameters)
# rescue Starry::SerializeError => ex
# https://github.com/takemar/starry/pull/4
rescue => ex
  raise Error, ex.message, cause: ex
end