Skip to content

Commit

Permalink
Allow scheme and host to be configured application wide (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Mrsny authored and Jason S committed Jul 12, 2017
1 parent 42a4454 commit 1bbe4de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/jsonapi/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ defmodule JSONAPI.View do
end

def url_for(data, %Plug.Conn{}=conn) when is_list(data) do
"#{Atom.to_string(conn.scheme)}://#{conn.host}#{@namespace}/#{type()}"
"#{scheme(conn)}://#{host(conn)}#{@namespace}/#{type()}"
end

def url_for(data, %Plug.Conn{}=conn) do
"#{Atom.to_string(conn.scheme)}://#{conn.host}#{@namespace}/#{type()}/#{id(data)}"
"#{scheme(conn)}://#{host(conn)}#{@namespace}/#{type()}/#{id(data)}"
end

def url_for_rel(data, rel_type, conn) do
Expand Down Expand Up @@ -155,6 +155,10 @@ defmodule JSONAPI.View do
|> String.to_atom
end

defp host(conn), do: Application.get_env(:jsonapi, :host, conn.host)

defp scheme(conn), do: Application.get_env(:jsonapi, :scheme, to_string(conn.scheme))

defoverridable attributes: 2,
fields: 0,
id: 1,
Expand Down
14 changes: 13 additions & 1 deletion test/jsonapi/view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,26 @@ defmodule JSONAPI.ViewTest do
assert PostView.type == "posts"
end

test "url_for/2 with namespace" do
test "url_for/2" do
assert PostView.url_for(nil, nil) == "/api/posts"
assert PostView.url_for([], nil) == "/api/posts"
assert PostView.url_for(%{id: 1}, nil) == "/api/posts/1"
assert PostView.url_for([], %Plug.Conn{}) == "http://www.example.com/api/posts"
assert PostView.url_for(%{id: 1}, %Plug.Conn{}) == "http://www.example.com/api/posts/1"
assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == "http://www.example.com/api/posts/relationships/comments"
assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == "http://www.example.com/api/posts/1/relationships/comments"

Application.put_env(:jsonapi, :host, "www.otherhost.com")
assert PostView.url_for([], %Plug.Conn{}) == "http://www.otherhost.com/api/posts"
assert PostView.url_for(%{id: 1}, %Plug.Conn{}) == "http://www.otherhost.com/api/posts/1"
assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == "http://www.otherhost.com/api/posts/relationships/comments"
assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == "http://www.otherhost.com/api/posts/1/relationships/comments"

Application.put_env(:jsonapi, :scheme, "ftp")
assert PostView.url_for([], %Plug.Conn{}) == "ftp://www.otherhost.com/api/posts"
assert PostView.url_for(%{id: 1}, %Plug.Conn{}) == "ftp://www.otherhost.com/api/posts/1"
assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == "ftp://www.otherhost.com/api/posts/relationships/comments"
assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == "ftp://www.otherhost.com/api/posts/1/relationships/comments"
end

@tag :compile_phoenix
Expand Down

0 comments on commit 1bbe4de

Please sign in to comment.