Last active
August 29, 2015 14:16
-
-
Save tmlbl/80cee11b1309deb03749 to your computer and use it in GitHub Desktop.
Basic query string encoder in Julia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using HttpCommon | |
using Dates | |
# Creating a query string that will encode unix datetimes | |
# given Julia DateTimes. Methods to handle more types could | |
# easily be added. | |
function qstring(kv::Tuple...) | |
encodeval(v::String) = encodeURI(v) | |
encodeval(dt::DateTime) = string(int64(datetime2unix(dt))) | |
encodepair(p::Tuple) = string(encodeval(p[1]), "=", encodeval(p[2]), "&") | |
q = string("?", join(map(encodepair, kv))) | |
q[1:length(q) - 1] | |
end | |
# Let's test it! | |
@assert qstring(("a", "b"), ("c", "d")) == "?a=b&c=d" | |
@assert qstring(("date", unix2datetime(1424888079))) == "?date=1424888079" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment