Skip to content

Commit 34c2c50

Browse files
author
José Valim
committed
Merge pull request #1916 from augiedb/master
Add examples to URI
2 parents f62c26b + 6f219ad commit 34c2c50

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lib/elixir/lib/uri.ex

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ defmodule URI do
5353
URL encoded as per `encode/1`. Keys and values can be any term
5454
that implements the `String.Chars` protocol (i.e. can be converted
5555
to a binary).
56+
57+
## Examples
58+
59+
iex> hd = HashDict.new([{"foo", 1}, {"bar", "2"}])
60+
#HashDict<[{"bar", "2"}, {"foo", 1}]>
61+
iex> URI.encode_query(hd)
62+
"bar=2&foo=1"
63+
5664
"""
5765
def encode_query(l), do: Enum.map_join(l, "&", &pair/1)
5866

@@ -62,6 +70,19 @@ defmodule URI do
6270
binary. It also does percent-unescaping of both keys and values.
6371
6472
Use `query_decoder/1` if you want to iterate over each value manually.
73+
74+
## Examples
75+
76+
iex> URI.decode_query("foo=1&bar=2")
77+
#HashDict<[{"bar", "2"}, {"foo", "1"}]>
78+
79+
iex> hd = HashDict.new()
80+
#HashDict<[]>
81+
iex> URI.decode_query("foo=1&bar=2", hd) |> HashDict.keys
82+
["bar", "foo"]
83+
iex> URI.decode_query("foo=1&bar=2", hd) |> HashDict.values
84+
["2", "1"]
85+
6586
"""
6687
def decode_query(q, dict // HashDict.new) when is_binary(q) do
6788
Enum.reduce query_decoder(q), dict, fn({ k, v }, acc) -> Dict.put(acc, k, v) end
@@ -70,6 +91,12 @@ defmodule URI do
7091
@doc """
7192
Returns an iterator function over the query string that decodes
7293
the query string in steps.
94+
95+
## Examples
96+
97+
iex> URI.query_decoder("foo=1&bar=2") |> Enum.map &(&1)
98+
[{"foo", "1"}, {"bar", "2"}]
99+
73100
"""
74101
def query_decoder(q) when is_binary(q) do
75102
fn(acc, fun) ->
@@ -103,6 +130,12 @@ defmodule URI do
103130

104131
@doc """
105132
Percent-escape a URI.
133+
134+
## Example
135+
136+
iex> URI.encode("http://elixir-lang.com/getting_started/2.html")
137+
"http%3A%2F%2Felixir-lang.com%2Fgetting_started%2F2.html"
138+
106139
"""
107140
def encode(s), do: bc(<<c>> inbits s, do: <<percent(c) :: binary>>)
108141

@@ -125,6 +158,12 @@ defmodule URI do
125158

126159
@doc """
127160
Percent-unescape a URI.
161+
162+
## Examples
163+
164+
iex> URI.decode("http%3A%2F%2Felixir-lang.com")
165+
"http://elixir-lang.com"
166+
128167
"""
129168
def decode(<<?%, hex1, hex2, tail :: binary >>) do
130169
<< bsl(hex2dec(hex1), 4) + hex2dec(hex2) >> <> decode(tail)
@@ -160,6 +199,12 @@ defmodule URI do
160199
which takes no arguments and returns the default port
161200
for that particular scheme. Take a look at `URI.HTTPS` for an
162201
example of one of these extension modules.
202+
203+
## Examples
204+
205+
iex> URI.parse("http://elixir-lang.org/")
206+
URI.Info[scheme: "http", path: "/", query: nil, fragment: nil, authority: "elixir-lang.org", userinfo: nil, host: "elixir-lang.org", port: 80]
207+
163208
"""
164209
def parse(s) when is_binary(s) do
165210
# From http://tools.ietf.org/html/rfc3986#appendix-B

0 commit comments

Comments
 (0)