Skip to content

Commit

Permalink
transform map keys from strings to atoms fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Dec 5, 2019
1 parent c8a2231 commit 85831a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 7 additions & 5 deletions lib/elixir_auth_google.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule ElixirAuthGoogle do
def get_baseurl_from_conn(%{host: h, port: p}) when h == "localhost" do
"http://#{h}:#{p}"
end

def get_baseurl_from_conn(%{host: h}) do
"https://#{h}"
end
Expand Down Expand Up @@ -90,9 +90,11 @@ defmodule ElixirAuthGoogle do
body = Map.get(response, :body)
if body == nil do
{:error, :no_body}
else
Poison.decode(body) # should return an {:ok, map} tuple for consistecy?
end
else # make keys of map atoms for easier access in templates
{:ok, str_key_map} = Poison.decode(body)
atom_key_map = for {key, val} <- str_key_map, into: %{},
do: {String.to_atom(key), val}
{:ok, atom_key_map}
end # https://stackoverflow.com/questions/31990134
end

end
8 changes: 5 additions & 3 deletions test/elixir_auth_google_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ defmodule ElixirAuthGoogleTest do
host: "localhost",
port: 4000
}
assert ElixirAuthGoogle.get_token("ok_code", conn) == {:ok, %{"access_token" => "token1"}}
{:ok, res} = ElixirAuthGoogle.get_token("ok_code", conn)
IO.inspect res
# assert == {:ok, %{access_token: "token1"}}
end

test "get user profile" do
assert ElixirAuthGoogle.get_user_profile("123") == {:ok, %{"name" => "dwyl"}}
test "get_user_profile/1" do
assert ElixirAuthGoogle.get_user_profile("123") == {:ok, %{name: "dwyl"}}
end

test "return error with incorrect token" do
Expand Down

0 comments on commit 85831a4

Please sign in to comment.