-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
318 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
defmodule CodeCorps.Github do | ||
|
||
alias CodeCorps.{User, Repo} | ||
|
||
@api Application.get_env(:code_corps, :github_api) | ||
|
||
@doc """ | ||
Temporary function until the actual behavior is implemented. | ||
Posts code to github to receive an auth token, associates user with that | ||
auth token. | ||
Accepts a third parameter, which is a custom API module, for the purposes of | ||
explicit dependency injection during testing. | ||
Returns one of the following: | ||
- {:ok, %CodeCorps.User{}} | ||
- {:error, %Ecto.Changeset{}} | ||
- {:error, "some_github_error"} | ||
""" | ||
def connect(user, _code), do: {:ok, user} | ||
@spec connect(User.t, String.t, module) :: {:ok, User.t} | {:error, String.t} | ||
def connect(%User{} = user, code, api \\ @api) do | ||
case code |> api.connect do | ||
{:ok, github_auth_token} -> user |> associate(%{github_auth_token: github_auth_token}) | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
@doc """ | ||
Associates user with an auth token | ||
Returns one of the following: | ||
- {:ok, %CodeCorps.User{}} | ||
- {:error, %Ecto.Changeset{}} | ||
""" | ||
@spec associate(User.t, map) :: {:ok, User.t} | {:error, Ecto.Changeset.t} | ||
def associate(user, params) do | ||
user | ||
|> User.github_associate_changeset(params) | ||
|> User.github_association_changeset(params) | ||
|> Repo.update() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule CodeCorps.Github.API do | ||
@moduledoc """ | ||
The boundary module which communicates with the Github API using either | ||
direct requests, or through Tentacat | ||
""" | ||
@behaviour CodeCorps.Github.APIContract | ||
|
||
@client_secret Application.get_env(:code_corps, :github_client_secret) | ||
@client_id Application.get_env(:code_corps, :github_client_id) | ||
|
||
@base_connect_params %{ | ||
client_id: @client_id, | ||
client_secret: @client_secret | ||
} | ||
|
||
@doc """ | ||
Receives a code generated through the client-side github connect process and | ||
posts it to github. | ||
Returns either an {:ok, access_token}, or an {:error, error_message}. | ||
""" | ||
@spec connect(String.t) :: {:ok, String.t} | {:error, String.t} | ||
def connect(code) do | ||
with {:ok, %HTTPoison.Response{body: response}} <- code |> build_connect_params() |> do_connect(), | ||
{:ok, %{"access_token" => access_token}} <- response |> Poison.decode | ||
do | ||
{:ok, access_token} | ||
else | ||
{:ok, %{"error" => error}} -> {:error, error} | ||
end | ||
end | ||
|
||
@connect_url "https://github.com/login/oauth/access_token" | ||
|
||
@spec do_connect(map) :: {:ok, HTTPoison.Response.t | HTTPoison.AsyncResponse.t} | {:error, HTTPoison.Error.t} | ||
defp do_connect(params) do | ||
HTTPoison.post(@connect_url, "", [{"Accept", "application/json"}], [params: params]) | ||
end | ||
|
||
@spec build_connect_params(String.t) :: map | ||
defp build_connect_params(code) do | ||
@base_connect_params |> Map.put(:code, code) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule CodeCorps.Github.APIContract do | ||
@moduledoc """ | ||
Defines a contract for a github API module, listing all functions the module | ||
should implement. | ||
This contract should be specified as behaviour for the default `Github.API` | ||
module, as well as any custom module we inject in tests. | ||
""" | ||
|
||
@doc """ | ||
Receives a code string, created in the client part of the github connect process, | ||
returns either an :ok tupple indicating a successful connect process, where | ||
the second element is the auth token string, or an :error tuple, where the | ||
second element is an error message, or a struct | ||
""" | ||
@callback connect(code :: String.t) :: {:ok, auth_token :: String.t} | {:error, error :: String.t} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule CodeCorps.Github.TestAPI do | ||
@moduledoc """ | ||
The most basic implementation of an API module for testing. All functions | ||
here should return successes. | ||
If we want to test the wrapper module, we can specify custom API modules | ||
during function calls. | ||
""" | ||
@behaviour CodeCorps.Github.APIContract | ||
|
||
@spec connect(String.t) :: {:ok, String.t} | ||
def connect(code), do: send(self(), {:ok, code}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
priv/repo/migrations/20170526095401_add_github_auth_token_to_users.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddGithubAuthTokenToUsers do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:users) do | ||
add :github_auth_token, :string | ||
end | ||
end | ||
end |
Oops, something went wrong.