Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor Phoenix.Router.Path.build_url to take a keyword list as it's third arg #161

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/phoenix/router/mapper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ defmodule Phoenix.Router.Mapper do
scheme = if config[:ssl], do: "https", else: "http"

Path.build(unquote(path), params)
|> Path.build_url(host, scheme)
|> Path.build_url(host, [scheme: scheme])
end
end
end
Expand Down
15 changes: 10 additions & 5 deletions lib/phoenix/router/path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,20 @@ defmodule Phoenix.Router.Path do
Builds a URL based on options passed.

# Examples:
iex> Path.build_url("/users", "example.com", "https")
"https://example.com/users"

iex> Path.build_url("/users", "example.com")
"http://example.com/users"

iex> Path.build_url("/users", "example.com", [scheme: "https"])
"https://example.com/users"

iex> Path.build_url("/users", "example.com", [port: 8080])
"http://example.com:8080/users"

"""
def build_url(path, host, scheme \\ "http") do
%URI{scheme: scheme, host: host, path: path} |> to_string
def build_url(path, host, options \\ []) do
scheme = options[:scheme] || "http"
port = options[:port]
%URI{scheme: scheme, host: host, path: path, port: port} |> to_string
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion test/phoenix/router/path_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Phoenix.Router.PathTest do

test "build_url includes the host and scheme" do
path = Path.build("users/:id", id: 1)
assert Path.build_url(path, "example.com", "https") ==
assert Path.build_url(path, "example.com", [scheme: "https"]) ==
"https://example.com/users/1"
end

Expand Down