class Crest::NestedParamsEncoder

Defined in:

crest/params_encoders/nested_params_encoder.cr

Constant Summary

ARRAY_REGEX = /[\[\]]+\Z/
SUBKEYS_REGEX = /[^\[\]]+(?:\]?\[\])?/

Class Method Summary

Instance Method Summary

Instance methods inherited from class Crest::ParamsEncoder

decode(query : String) : Hash decode, encode(params : Hash) : String encode

Class methods inherited from class Crest::ParamsEncoder

decode(query : String) : Hash decode, encode(params : Hash) : String encode

Class Method Detail

def self.flatten_params(object : Hash, parent_key = nil) : Array(Tuple(String, Crest::ParamsValue)) #

Transform deeply nested params containers into a flat array of {key, value}.

parent_key — Should not be passed (used for recursion)

Crest::NestedParamsEncoder.flatten_params({:key1 => {:key2 => "123"}})
# => [{"key1[key2]", "123"}]

[View source]
def self.flatten_params(object : Array, parent_key = nil) : Array(Tuple(String, Crest::ParamsValue)) #

Transform deeply nested params containers into a flat array of {key, value}.

parent_key — Should not be passed (used for recursion)

Crest::NestedParamsEncoder.flatten_params({:key1 => {:arr => ["1", "2", "3"]}})
# => [{"key1[arr]", "1"}, {"key1[arr]", "2"}, {"key1[arr]", "3"}]

[View source]

Instance Method Detail

def decode(query : String) : Hash #

Converts the given URI query string into a hash.

Crest::NestedParamsEncoder.decode("a[]=one&a[]=two&a[]=three&b=true&c=C&d=1")
# => {"a" => ["one", "two", "three"], "b" => "true", "c" => "C", "d" => "1"}

Crest::NestedParamsEncoder.decode("a=one&a=two&a=three&b=true&c=C&d=1")
# => {"a" => ["one", "two", "three"], "b" => "true", "c" => "C", "d" => "1"}

[View source]
def encode(params : Hash) : String #

Converts the given params into a URI query string. Keys and values will converted to strings and appropriately escaped for the URI.

Crest::NestedParamsEncoder.encode({"a" => ["one", "two", "three"], "b" => true, "c" => "C", "d" => 1})
# => 'a=one&a=two&a=three&b=true&c=C&d=1'

[View source]