module Crest::ParamsEncoder

Extended Modules

Defined in:

crest/params_encoder.cr

Constant Summary

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

Instance Method Summary

Instance Method Detail

def decode(query : String) : Hash #

Converts the given URI querystring into a hash.

Crest::ParamsEncoder.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 querystring. Keys and values will converted to strings and appropriately escaped for the URI.

Crest::ParamsEncoder.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]
def flatten_params(object : Hash, parent_key = nil) #

Transform deeply nested params containers into a flat hash of key => value.

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

[View source]
def flatten_params(object : Array, parent_key = nil) #

Transform deeply nested params containers into a flat hash of key => value.

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

[View source]