module Crest::ParamsEncoder
Extended Modules
Defined in:
crest/params_encoder.crConstant Summary
-
ARRAY_REGEX =
/[\[\]]+\Z/
-
SUBKEYS_REGEX =
/[^\[\]]+(?:\]?\[\])?/
Instance Method Summary
-
#decode(query : String) : Hash
Converts the given URI querystring into a hash.
-
#encode(params : Hash) : String
Converts the given params into a URI querystring.
-
#flatten_params(object : Hash, parent_key = nil)
Transform deeply nested params containers into a flat hash of
key => value
. -
#flatten_params(object : Array, parent_key = nil)
Transform deeply nested params containers into a flat hash of
key => value
.
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"}
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'
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"}]
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"}]