@@ -31,8 +31,18 @@ defmodule URI do
31
31
32
32
@ doc """
33
33
Returns the default port for a given scheme.
34
+
34
35
If the scheme is unknown to URI, returns `nil`.
35
36
Any scheme may be registered via `default_port/2`.
37
+
38
+ ## Examples
39
+
40
+ iex> URI.default_port("ftp")
41
+ 21
42
+
43
+ iex> URI.default_port("ponzi")
44
+ nil
45
+
36
46
"""
37
47
def default_port ( scheme ) when is_binary ( scheme ) do
38
48
{ :ok , dict } = :application . get_env ( :elixir , :uri )
@@ -48,39 +58,42 @@ defmodule URI do
48
58
end
49
59
50
60
@ doc """
61
+ Encodes an enumerable into a query string.
62
+
51
63
Takes an enumerable (containing a sequence of two-item tuples)
52
- and returns a string of the form "k=v&k2=v2 ..." where keys and values are
53
- URL encoded as per `encode/1`. Keys and values can be any term
54
- that implements the `String.Chars` protocol (i.e. can be converted
64
+ and returns a string of the form "key1=value1&key2=value2 ..." where
65
+ keys and values are URL encoded as per `encode/1`. Keys and values can
66
+ be any term that implements the `String.Chars` protocol (i.e. can be converted
55
67
to a binary).
56
68
57
69
## Examples
58
70
59
71
iex> hd = HashDict.new([{"foo", 1}, {"bar", "2"}])
60
- #HashDict<[{"bar", "2"}, {"foo", 1}]>
61
72
iex> URI.encode_query(hd)
62
73
"bar=2&foo=1"
63
74
64
75
"""
65
76
def encode_query ( l ) , do: Enum . map_join ( l , "&" , & pair / 1 )
66
77
67
78
@ doc """
68
- Given a query string of the form "key1=value1&key=value2...", produces an
79
+ Decodes a query string into an orddict.
80
+
81
+ Given a query string of the form "key1=value1&key2=value2...", produces an
69
82
orddict with one entry for each key-value pair. Each key and value will be a
70
- binary. It also does percent-unescaping of both keys and values .
83
+ binary. Keys and values will be percent-unescaped .
71
84
72
85
Use `query_decoder/1` if you want to iterate over each value manually.
73
86
74
87
## Examples
75
88
76
- iex> URI.decode_query("foo=1&bar=2")
77
- #HashDict< [{"bar", "2"}, {"foo", "1"}]>
89
+ iex> URI.decode_query("foo=1&bar=2") |> Dict.to_list
90
+ [{"bar", "2"}, {"foo", "1"}]
78
91
79
- iex > hd = HashDict.new()
92
+ > hd = HashDict.new()
80
93
#HashDict<[]>
81
- iex > URI.decode_query("foo=1&bar=2", hd) |> HashDict.keys
94
+ > URI.decode_query("foo=1&bar=2", hd) |> HashDict.keys
82
95
["bar", "foo"]
83
- iex > URI.decode_query("foo=1&bar=2", hd) |> HashDict.values
96
+ > URI.decode_query("foo=1&bar=2", hd) |> HashDict.values
84
97
["2", "1"]
85
98
86
99
"""
@@ -201,7 +214,9 @@ defmodule URI do
201
214
## Examples
202
215
203
216
iex> URI.parse("http://elixir-lang.org/")
204
- URI.Info[scheme: "http", path: "/", query: nil, fragment: nil, authority: "elixir-lang.org", userinfo: nil, host: "elixir-lang.org", port: 80]
217
+ URI.Info[scheme: "http", path: "/", query: nil, fragment: nil,
218
+ authority: "elixir-lang.org", userinfo: nil,
219
+ host: "elixir-lang.org", port: 80]
205
220
206
221
"""
207
222
def parse ( s ) when is_binary ( s ) do
0 commit comments