Bamboo Elastic Email Adapter v1.2.0 Bamboo.ElasticEmail.Utilities View Source

Utilities for working with the Elastic Email API.

The decode_query/{1,2} and encode_query/{1,2} functions are based heavily on Plug.Conn.Query, but the Elastic Email API accepts repeated values for list values instead of requiring [] be appended to the key name. Nested key names are not supported.

Link to this section Summary

Functions

Decode an Elastic Email API query string. Because the decoded query is returned as a map, a list is always returned.

Encodes an Elastic Email API query string. Maps can be encoded

Link to this section Functions

Link to this function

decode_query(query, initial \\ %{})

View Source

Decode an Elastic Email API query string. Because the decoded query is returned as a map, a list is always returned.

iex> decode_query("foo=bar")["foo"]
["bar"]

If a value is given more than once, a list is returned:

iex> decode_query("foo=bar&foo=baz")["foo"]
["bar", "baz"]

Decoding an empty string returns an empty map.

iex> decode_query("")
%{}
Link to this function

encode_query(kv, encoder \\ &to_string/1)

View Source

Encodes an Elastic Email API query string. Maps can be encoded:

iex> encode_query(%{foo: "bar", baz: "bat"})
"baz=bat&foo=bar"

Encoding keyword lists preserves the order of the fields:

iex> encode_query([foo: "bar", baz: "bat"])
"foo=bar&baz=bat"

When encoding keyword lists with duplicate keys, the keys are repeated:

iex> encode_query([foo: "bar", foo: "bat"])
"foo=bar&foo=bat"

Encoding maps or keys with simple lists will have the same result as repeated keys in a keyword list:

iex> encode_query(%{foo: ["bar", "bat"]})
"foo=bar&foo=bat"

Encoding a list of maps works the same way:

iex> encode_query([%{foo: "bar"}, %{foo: "bat"}])
"foo=bar&foo=bat"

Nested maps and keyword lists are not supported and raise exceptions:

iex> encode_query(%{foo: %{bar: "baz"}})
** (ArgumentError) cannot encode nested structures for foo

iex> encode_query(%{foo: [bar: "baz"]})
** (ArgumentError) cannot encode nested structures for foo

Structs work as well as maps:

iex> encode_query(%Point{x: 1, y: 1})
"x=1&y=1"

Other structures raise an exception:

iex> encode_query(3)
** (ArgumentError) can only encode maps, keyword lists, or lists of maps, got: 3