-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
085ac5f
commit e3f3ea8
Showing
8 changed files
with
192 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
defmodule Phamello.TrelloClient do | ||
alias Phamello.Picture | ||
|
||
@api_url "https://api.trello.com" | ||
@boards_path "/1/members/me" | ||
@cards_path "/1/cards" | ||
|
||
def create_card(%Picture{} = picture) do | ||
case get_list_id(config[:board_name], config[:list_name]) do | ||
{:ok, list_id} -> do_create_card(list_id, picture) | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
def do_create_card(list_id, picture) do | ||
@cards_path | ||
|> full_url(card_params(list_id, picture)) | ||
|> HTTPoison.post!("") | ||
|> parse_response | ||
end | ||
|
||
def get_list_id(board, list) do | ||
case fetch_boards do | ||
{:ok, boards} -> {:ok, do_get_list_id(boards, board, list)} | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
def do_get_list_id(boards, board, list) do | ||
boards | ||
|> map_boards | ||
|> select_board(board) | ||
|> select_board_list(list) | ||
end | ||
|
||
defp select_board(boards, board) do | ||
case Map.get(boards, board, nil) do | ||
nil -> raise "Board not found" | ||
board -> board | ||
end | ||
end | ||
|
||
defp select_board_list(board, list) do | ||
case Map.get(board, list, nil) do | ||
nil -> raise "List not found" | ||
list -> list | ||
end | ||
end | ||
|
||
defp fetch_boards do | ||
@boards_path | ||
|> full_url(boards_params) | ||
|> HTTPoison.get! | ||
|> parse_response | ||
end | ||
|
||
defp map_boards(%{"boards" => boards}) do | ||
boards | ||
|> Enum.reduce(%{}, fn(%{"name" => name, "lists" => lists}, acc) -> | ||
Map.put(acc, name, map_lists(lists)) | ||
end) | ||
end | ||
|
||
defp map_lists(lists) do | ||
lists | ||
|> Enum.reduce(%{}, fn(%{"id" => id, "name" => name}, acc) -> | ||
Map.put(acc, name, id) | ||
end) | ||
end | ||
|
||
defp parse_response(%HTTPoison.Response{status_code: 200, body: body}), do: | ||
{:ok, Poison.decode!(body)} | ||
|
||
defp parse_response(%HTTPoison.Response{status_code: _, body: body}), do: | ||
{:error, body} | ||
|
||
defp full_url(path, params) do | ||
query = URI.encode_query(params) | ||
|
||
@api_url | ||
|> URI.merge(path) | ||
|> URI.merge("?#{query}") | ||
|> URI.to_string | ||
end | ||
|
||
defp config, do: Application.get_env(:phamello, __MODULE__) | ||
|
||
defp auth_params do | ||
[key: config[:api_key], token: config[:api_token]] | ||
|> Enum.into(%{}) | ||
end | ||
|
||
defp boards_params do | ||
auth_params | ||
|> Map.merge(%{ | ||
"boards" => "all", | ||
"board_lists" => "all" | ||
}) | ||
end | ||
|
||
defp card_params(list_id, picture) do | ||
auth_params | ||
|> Map.merge(%{ | ||
"idList" => list_id, | ||
"name" => picture.name, | ||
"due" => picture.updated_at, | ||
"desc" => "#{picture.user.username}\n#{picture.remote_url}" | ||
}) | ||
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,18 @@ | ||
defmodule Phamello.TrelloTasks do | ||
alias Phamello.{Picture, TrelloClient} | ||
|
||
def push_to_board(pid, %Picture{} = picture) do | ||
case TrelloClient.create_card(picture) do | ||
{:ok, %{"url" => url}} -> confirm_trello_push(pid, picture.id, url) | ||
{:error, error} -> bail_trello_push(pid, picture.id, error) | ||
end | ||
end | ||
|
||
defp confirm_trello_push(pid, picture_id, url) do | ||
GenServer.cast(pid, {:trello_notify_complete, picture_id, url}) | ||
end | ||
|
||
defp bail_trello_push(pid, picture_id, error) do | ||
GenServer.cast(pid, {:trello_notify_error, picture_id, error}) | ||
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
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
11 changes: 11 additions & 0 deletions
11
priv/repo/migrations/20160822230741_add_picture_trello_url.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,11 @@ | ||
defmodule Phamello.Repo.Migrations.AddPictureTrelloUrl do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:pictures) do | ||
add :trello_url, :string | ||
end | ||
|
||
create index(:pictures, [:trello_url]) | ||
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
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