@@ -53,6 +53,14 @@ defmodule URI do
53
53
URL encoded as per `encode/1`. Keys and values can be any term
54
54
that implements the `String.Chars` protocol (i.e. can be converted
55
55
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
+
56
64
"""
57
65
def encode_query ( l ) , do: Enum . map_join ( l , "&" , & pair / 1 )
58
66
@@ -62,6 +70,19 @@ defmodule URI do
62
70
binary. It also does percent-unescaping of both keys and values.
63
71
64
72
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
+
65
86
"""
66
87
def decode_query ( q , dict // HashDict . new ) when is_binary ( q ) do
67
88
Enum . reduce query_decoder ( q ) , dict , fn ( { k , v } , acc ) -> Dict . put ( acc , k , v ) end
@@ -70,6 +91,12 @@ defmodule URI do
70
91
@ doc """
71
92
Returns an iterator function over the query string that decodes
72
93
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
+
73
100
"""
74
101
def query_decoder ( q ) when is_binary ( q ) do
75
102
fn ( acc , fun ) ->
@@ -103,6 +130,12 @@ defmodule URI do
103
130
104
131
@ doc """
105
132
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
+
106
139
"""
107
140
def encode ( s ) , do: bc ( << c >> in bits s , do: << percent ( c ) :: binary >> )
108
141
@@ -125,6 +158,12 @@ defmodule URI do
125
158
126
159
@ doc """
127
160
Percent-unescape a URI.
161
+
162
+ ## Examples
163
+
164
+ iex> URI.decode("http%3A%2F%2Felixir-lang.com")
165
+ "http://elixir-lang.com"
166
+
128
167
"""
129
168
def decode ( << ?% , hex1 , hex2 , tail :: binary >> ) do
130
169
<< bsl ( hex2dec ( hex1 ) , 4 ) + hex2dec ( hex2 ) >> <> decode ( tail )
@@ -160,6 +199,12 @@ defmodule URI do
160
199
which takes no arguments and returns the default port
161
200
for that particular scheme. Take a look at `URI.HTTPS` for an
162
201
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
+
163
208
"""
164
209
def parse ( s ) when is_binary ( s ) do
165
210
# From http://tools.ietf.org/html/rfc3986#appendix-B
0 commit comments