Skip to content

Instantly share code, notes, and snippets.

@gaynetdinov
Last active June 8, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaynetdinov/353dab9a444e9b42440c091537a3c3fa to your computer and use it in GitHub Desktop.
Save gaynetdinov/353dab9a444e9b42440c091537a3c3fa to your computer and use it in GitHub Desktop.
defp check_access(conn) do
parsed_body = with {:ok, response} <- HTTPoison.get(url, header_with_authorization(conn)),
200 <- response.status_code,
do: Poison.Parser.parse(response.body)
case parsed_body do
{:ok, %{"super_admin" => _} = body} ->
{:ok, body}
_ ->
{:error, "User not authorized"}
end
end
@gaynetdinov
Copy link
Author

gaynetdinov commented Jun 8, 2016

With Elixir 1.3 this code would look like this:

defp check_access(conn) do
  with {:ok, %{status_code: 200} = response} <- HTTPoison.get(url, header_with_authorization(conn)),
       {:ok, %{"super_admin" => _} = body} <- Poison.Parser.parse(response.body) do
    {:ok, body}
  else
    _ -> {:error, "User not authorized"}
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment