Sending tags to an ad server
Your glimr tags will live very happily in your ad server. To get there, they need to be encoded in a compliant manner. This library provides some tools to make that easy.
The biggest hurdle is how to encode your tags. Since they might come back as an array of strings, or a dictionary of arrays, we have tools to encode both into HTTP spec compliant query strings.
.objectToQuery
string Glimr.objectToQuery( object value )
Take an object/dictionary and convert to a query string. It will not modify array keys (postfix them with []), that is up to the implementer to make sure the keys are postfix'd.
.arrayToQuery
string Glimr.arrayToQuery( array value, string key )
Take an array and convert to a query string. The second argument is a string of which will become key for the values.
It will not modify keys like other frameworks might (i.e postfix them with []), that is up to the implementer to make sure the keys are postfix'd.
var tags = ["a", "bcd", "ef"];
var queryString = Glimr.arrayToQuery(tags, "my_key");
// my_key=a&my_key=bcd&my_key=ef
.queryToObject
object Glimr.queryToObject( string )
Parse a query string into an object. Since multiple entries are supported per key, it always creates an array key. For example:
var queryString = "foo=bar&foo=baz&hello=world";
var object = Glimr.queryToObject("foo=bar&foo=baz&hello=world");
/*
{
foo: ["bar", "baz"],
hello: ["world"] // note that hello is also an array
}
*/
.escapeStringForQuery
string Glimr.escapeStringForQuery( string value )
This is more of a helper that might be useful for very custom stuff. It's what Glimr.objectToQuery
and Glimr.arrayToQuery
use to encode the values.
Don't call this manually
... Unless you really need it. Escaping and unescaping is done behind the scenes by
arrayToQuery
/objectToQuery
Usage is easy:
var escapedString = Glimr.escapeStringForQuery("hello world");
// hello%20world
.unescapeStringForQuery
string Glimr.unescapeStringForQuery( string value )
Does the opposite of Glimr.escapeStringForQuery
:
var string = Glimr.unescapeStringForQuery("hello%20world");
// hello world
Updated less than a minute ago