|
1 | 1 | defmodule CodeCorps.Github do |
2 | | - |
3 | 2 | alias CodeCorps.{User, Repo} |
4 | 3 |
|
| 4 | + @api Application.get_env(:code_corps, :github_api) |
| 5 | + |
5 | 6 | @doc """ |
6 | | - Temporary function until the actual behavior is implemented. |
| 7 | + Posts code to github to receive an auth token, associates user with that |
| 8 | + auth token. |
| 9 | +
|
| 10 | + Accepts a third parameter, which is a custom API module, for the purposes of |
| 11 | + explicit dependency injection during testing. |
| 12 | +
|
| 13 | + Returns one of the following: |
| 14 | +
|
| 15 | + - {:ok, %CodeCorps.User{}} |
| 16 | + - {:error, %Ecto.Changeset{}} |
| 17 | + - {:error, "some_github_error"} |
7 | 18 | """ |
8 | | - def connect(user, _code), do: {:ok, user} |
| 19 | + @spec connect(User.t, String.t, module) :: {:ok, User.t} | {:error, String.t} |
| 20 | + def connect(%User{} = user, code, api \\ @api) do |
| 21 | + case code |> api.connect do |
| 22 | + {:ok, github_auth_token} -> user |> associate(%{github_auth_token: github_auth_token}) |
| 23 | + {:error, error} -> {:error, error} |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + @doc """ |
| 28 | + Associates user with an auth token |
9 | 29 |
|
| 30 | + Returns one of the following: |
| 31 | +
|
| 32 | + - {:ok, %CodeCorps.User{}} |
| 33 | + - {:error, %Ecto.Changeset{}} |
| 34 | + """ |
| 35 | + @spec associate(User.t, map) :: {:ok, User.t} | {:error, Ecto.Changeset.t} |
10 | 36 | def associate(user, params) do |
11 | 37 | user |
12 | | - |> User.github_associate_changeset(params) |
| 38 | + |> User.github_association_changeset(params) |
13 | 39 | |> Repo.update() |
14 | 40 | end |
15 | 41 | end |
0 commit comments