Current section
7 Versions
Jump to
Current section
7 Versions
Compare versions
6
files changed
+120
additions
-73
deletions
| @@ -18,7 +18,7 @@ config :trello, | |
| 18 18 | ### Methods |
| 19 19 | ***Secret is from authorization via trello. You can use generate_auth_url to generate the url*** |
| 20 20 | |
| 21 | - - `generate_auth_url(config)` - {scope: "account,write,read", exipiry: "never", response_type: "token"} |
| 21 | + - `generate_auth_url(config)` - %{scope: "account,write,read", exipiry: "never", response_type: "token"} |
| 22 22 | |
| 23 23 | - `get(url, secret)` |
| 24 24 | - `get!(url, secret)` |
| @@ -51,8 +51,11 @@ config :trello, | |
| 51 51 | - `get_list(list_id, secret)` |
| 52 52 | - `get_list!(list_id, secret)` |
| 53 53 | |
| 54 | - - `get_list_cards(list_id, secret)` |
| 54 | + - `add_comment_to_card( card_id, comment, secret )` |
| 55 | + - `add_comment_to_card!( card_id, comment, secret )` |
| 55 56 | |
| 57 | + - `get_list_cards(list_id, secret)` |
| 56 58 | - `get_list_cards!(list_id, secret)` |
| 59 | + |
| 57 60 | - `get_member(member_id, secret)` |
| 58 61 | - `get_member!(member_id, secret)` |
| @@ -3,19 +3,20 @@ | |
| 3 3 | {<<"description">>,<<"Trello wrapper for elixir api">>}. |
| 4 4 | {<<"elixir">>,<<"~> 1.2">>}. |
| 5 5 | {<<"files">>, |
| 6 | - [<<"lib/trello.ex">>,<<"lib/trello_http.ex">>,<<"README.md">>,<<"mix.exs">>, |
| 7 | - <<"LICENSE">>]}. |
| 6 | + [<<"lib">>,<<"lib/config.ex">>,<<"lib/trello.ex">>,<<"lib/trello_http.ex">>, |
| 7 | + <<"README.md">>,<<"mix.exs">>,<<"LICENSE">>]}. |
| 8 8 | {<<"licenses">>,[<<"MIT">>]}. |
| 9 9 | {<<"links">>,[{<<"GitHub">>,<<"https://github.com/mikaak/trello-elixir">>}]}. |
| 10 | - {<<"maintainers">>,[<<"Mika Kalathil">>]}. |
| 11 10 | {<<"name">>,<<"trello">>}. |
| 12 11 | {<<"requirements">>, |
| 13 | - [{<<"httpoison">>, |
| 14 | - [{<<"app">>,<<"httpoison">>}, |
| 15 | - {<<"optional">>,false}, |
| 16 | - {<<"requirement">>,<<">= 0.8.2">>}]}, |
| 17 | - {<<"poison">>, |
| 18 | - [{<<"app">>,<<"poison">>}, |
| 19 | - {<<"optional">>,false}, |
| 20 | - {<<"requirement">>,<<">= 1.5.0">>}]}]}. |
| 21 | - {<<"version">>,<<"1.3.0">>}. |
| 12 | + [[{<<"app">>,<<"poison">>}, |
| 13 | + {<<"name">>,<<"poison">>}, |
| 14 | + {<<"optional">>,false}, |
| 15 | + {<<"repository">>,<<"hexpm">>}, |
| 16 | + {<<"requirement">>,<<">= 1.5.0">>}], |
| 17 | + [{<<"app">>,<<"httpoison">>}, |
| 18 | + {<<"name">>,<<"httpoison">>}, |
| 19 | + {<<"optional">>,false}, |
| 20 | + {<<"repository">>,<<"hexpm">>}, |
| 21 | + {<<"requirement">>,<<">= 0.8.2">>}]]}. |
| 22 | + {<<"version">>,<<"1.4.0">>}. |
| @@ -0,0 +1,26 @@ | |
| 1 | + defmodule Trello.Config do |
| 2 | + alias Trello.Http |
| 3 | + |
| 4 | + def generate_auth_url(config) do |
| 5 | + query = URI.encode_query(query_params(config)) |
| 6 | + Http.process_request_url("authorize?" <> query) |
| 7 | + end |
| 8 | + |
| 9 | + def query_params(config) do |
| 10 | + query_params = Map.put(config, :key, key()) |
| 11 | + |
| 12 | + case name_configured?(config) do |
| 13 | + true -> query_params |
| 14 | + false -> Map.put(query_params, :name, trello_app_name()) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + defp name_configured?(config), do: Map.has_key?(config, :name) |
| 19 | + |
| 20 | + defp trello_app_key, do: Application.fetch_env!(:trello, :app_key) |
| 21 | + defp trello_app_name, do: Application.fetch_env!(:trello, :name) |
| 22 | + |
| 23 | + def key, do: key(trello_app_key()) |
| 24 | + def key({:system, key}), do: System.get_env(key) |
| 25 | + def key(value), do: value |
| 26 | + end |
| @@ -1,24 +1,24 @@ | |
| 1 1 | defmodule Trello do |
| 2 2 | alias Trello.Http |
| 3 | - |
| 4 | - def generate_auth_url(config) do |
| 5 | - query_params = Map.put(config, :key, trello_app_key) |
| 6 | - |
| 7 | - query_params = if (!Map.has_key?(config, :name)) do |
| 8 | - Map.put(query_params, :name, trello_app_name) |
| 9 | - else |
| 10 | - query_params |
| 11 | - end |
| 12 | - |
| 13 | - "authorize?" <> URI.encode_query(query_params) |
| 14 | - |> Http.process_url |
| 15 | - end |
| 3 | + alias Trello.Config |
| 16 4 | |
| 17 5 | def get(url, secret), do: Http.get(create_url(url, secret)) |> unwrap_http |
| 18 6 | def get!(url, secret), do: Http.get!(create_url(url, secret)) |> unwrap_http |
| 19 7 | |
| 20 | - def post(url, body, secret), do: Http.post(create_url(url, secret), body) |> unwrap_http |
| 21 | - def post!(url, body, secret), do: Http.post!(create_url(url, secret), body) |> unwrap_http |
| 8 | + def post(url, body, secret), do: Http.post(create_url(url, secret), body, %{ "Content-Type": "application/json; charset=utf-8"}) |> unwrap_http |
| 9 | + def post!(url, body, secret), do: Http.post!(create_url(url, secret), body, %{ "Content-Type": "application/json; charset=utf-8"}) |> unwrap_http |
| 10 | + |
| 11 | + @doc """ |
| 12 | + post via multipart/form-data |
| 13 | + requried for attachments |
| 14 | + body: [{"name", "value"}, {:file, path_to_file}] |
| 15 | + """ |
| 16 | + def post_multipart(url, body, secret) do |
| 17 | + Http.post(create_url(url, secret), {:multipart, body}, %{"Content-Type": "multipart/form-data"}) |> unwrap_http |
| 18 | + end |
| 19 | + def post_multipart!(url, body, secret) do |
| 20 | + Http.post!(create_url(url, secret), {:multipart, body}, %{"Content-Type": "multipart/form-data"}) |> unwrap_http |
| 21 | + end |
| 22 22 | |
| 23 23 | def put(url, body, secret), do: Http.put(create_url(url, secret), body) |> unwrap_http |
| 24 24 | def put!(url, body, secret), do: Http.put!(create_url(url, secret), body) |> unwrap_http |
| @@ -44,6 +44,29 @@ defmodule Trello do | |
| 44 44 | def get_list(list_id, secret), do: get "/lists/#{list_id}", secret |
| 45 45 | def get_list!(list_id, secret), do: get! "/lists/#{list_id}", secret |
| 46 46 | |
| 47 | + def add_comment_to_card(card_id, comment, secret), do: post "/cards/#{card_id}/actions/comments", %{text: comment}, secret |
| 48 | + def add_comment_to_card!(card_id, comment, secret), do: post! "/cards/#{card_id}/actions/comments", %{text: comment}, secret |
| 49 | + |
| 50 | + @doc """ |
| 51 | + https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-attachments |
| 52 | + [{"name", "value"}, {"mimeType", "value"}, {:file, path_to_file}] |
| 53 | + opts |
| 54 | + file (optional) Valid Values: A file |
| 55 | + url (optional) Valid Values: A URL starting with http:// or https:// or null |
| 56 | + name (optional) Valid Values: a string with a length from 0 to 256 |
| 57 | + mimeType (optional) Valid Values: a string with a length from 0 to 256 |
| 58 | + """ |
| 59 | + @valid_attachment_opts [:file, "name", "mimeType", "url"] |
| 60 | + def add_attachment_to_card(card_id, opts, secret), do: add_attachment_to_card(validate_attachment_opts(opts), card_id, opts, secret) |
| 61 | + def add_attachment_to_card!(card_id, opts, secret), do: add_attachment_to_card!(validate_attachment_opts(opts), card_id, opts, secret) |
| 62 | + |
| 63 | + def add_attachment_to_card(true, card_id, opts, secret), do: post_multipart("cards/#{card_id}/attachments", opts, secret) |
| 64 | + def add_attachment_to_card(false, _card_id, opts, _secret), do: raise "only following keys allowed #{inspect @valid_attachment_opts} \n --> #{inspect opts}" |
| 65 | + |
| 66 | + def add_attachment_to_card!(true, card_id, opts, secret), do: post_multipart!("cards/#{card_id}/attachments", opts, secret) |
| 67 | + def add_attachment_to_card!(false, _card_id, opts, _secret), do: raise "only following keys allowed #{inspect @valid_attachment_opts} \n --> #{inspect opts}" |
| 68 | + defp validate_attachment_opts(opts), do: Enum.all?(opts, fn({k,_v})-> k in [:file, "name", "mimeType", "url"] end) |
| 69 | + |
| 47 70 | def get_list_cards(list_id, secret), do: get "/lists/#{list_id}/cards", secret |
| 48 71 | def get_list_cards!(list_id, secret), do: get! "/lists/#{list_id}/cards", secret |
| 49 72 | |
| @@ -57,40 +80,29 @@ defmodule Trello do | |
| 57 80 | def get_current_member!(secret), do: get!("/members/me", secret) |
| 58 81 | |
| 59 82 | def get_full_board(board_id, secret) do |
| 60 | - with {:ok, board} <- get_board(board_id, secret), |
| 61 | - {:ok, cards} <- get_board_cards(board_id, secret), |
| 83 | + with {:ok, board} <- get_board(board_id, secret), |
| 84 | + {:ok, cards} <- get_board_cards(board_id, secret), |
| 62 85 | {:ok, labels} <- get_board_labels(board_id, secret), |
| 63 | - {:ok, lists} <- get_board_lists(board_id, secret) do |
| 86 | + {:ok, lists} <- get_board_lists(board_id, secret) do |
| 64 87 | |
| 65 88 | lists = Enum.map(lists, fn(list) -> |
| 66 89 | Map.put list, :cards, get_lists_with_id(list[:id], "idList", cards) |
| 67 90 | end) |
| 68 91 | |
| 69 | - board = Map.put(board, :cards, cards) |
| 92 | + board = board |
| 93 | + |> Map.put(:cards, cards) |
| 70 94 | |> Map.put(:labels, labels) |
| 71 | - |> Map.put(:lists, lists) |
| 95 | + |> Map.put(:lists, lists) |
| 72 96 | |
| 73 97 | {:ok, board} |
| 74 98 | end |
| 75 99 | end |
| 76 100 | |
| 77 | - def process_error(error), do: error |
| 101 | + def process_error(error), do: error |
| 78 102 | def process_success(body), do: body |
| 79 103 | |
| 80 | - defp trello_app_key, do: Application.fetch_env!(:trello, :app_key) |
| 81 | - defp trello_app_name, do: Application.fetch_env!(:trello, :name) |
| 82 104 | defp has_query_params?(url), do: Regex.match?(~r/\?/, url) |
| 83 105 | |
| 84 | - defp key do |
| 85 | - if (is_tuple trello_app_key) do |
| 86 | - {:system, key} = trello_app_key |
| 87 | - |
| 88 | - System.get_env(key) |
| 89 | - else |
| 90 | - trello_app_key |
| 91 | - end |
| 92 | - end |
| 93 | - |
| 94 106 | defp get_lists_with_id(id, idName, list) do |
| 95 107 | Enum.filter list, fn(item) -> |
| 96 108 | Map.get(item, String.to_atom(idName)) == id |
| @@ -98,7 +110,7 @@ defmodule Trello do | |
| 98 110 | end |
| 99 111 | |
| 100 112 | defp create_url(url, secret) do |
| 101 | - params = "token=#{secret}&key=#{key}" |
| 113 | + params = "token=#{secret}&key=#{Config.key}" |
| 102 114 | seperator = if has_query_params?(url), do: "&", else: "?" |
| 103 115 | |
| 104 116 | url <> seperator <> params |
| @@ -1,23 +1,31 @@ | |
| 1 1 | defmodule Trello.Http do |
| 2 2 | use HTTPoison.Base |
| 3 3 | |
| 4 | - def process_url(url), do: "https://trello.com/1/" <> url |
| 4 | + def process_request_url(url), do: "https://trello.com/1/#{url}" |
| 5 | + |
| 6 | + def process_request_body(body) do |
| 7 | + if (is_map(body)), do: Poison.encode!(body), else: body |
| 8 | + end |
| 5 9 | |
| 6 10 | def process_response_body(body) when is_bitstring(body) do |
| 7 11 | if (is_json(body)), do: decode_body(body), else: body |
| 8 12 | end |
| 9 13 | |
| 10 14 | defp decode_body(body) do |
| 11 | - with {:ok, data} <- Poison.decode(body), do: snake_keys(data) |> atomize_keys |
| 15 | + with {:ok, data} <- Poison.decode(body) do |
| 16 | + data |> snake_keys |> atomize_keys |
| 17 | + end |
| 12 18 | end |
| 13 19 | |
| 14 20 | defp snake_keys(data) when is_map(data) do |
| 15 21 | for {key, val} <- data, into: %{} do |
| 16 | - key_name = Macro.underscore key |
| 17 | - |
| 18 | - {key_name, snake_keys(val)} |
| 19 | - end |
| 22 | + {Macro.underscore(key), snake_keys(val)} |
| 23 | + end |
| 20 24 | end |
| 25 | + defp snake_keys(data) when is_list(data) do |
| 26 | + for item <- data, into: [], do: snake_keys(item) |
| 27 | + end |
| 28 | + defp snake_keys(data), do: data |
| 21 29 | |
| 22 30 | defp atomize_keys(data) when is_map(data) do |
| 23 31 | for {key, val} <- data, into: %{} do |
| @@ -26,16 +34,10 @@ defmodule Trello.Http do | |
| 26 34 | {key_name, atomize_keys(val)} |
| 27 35 | end |
| 28 36 | end |
| 29 | - |
| 30 | - defp snake_keys(data) when is_list(data) do |
| 31 | - for item <- data, into: [], do: snake_keys(item) |
| 32 | - end |
| 33 | - |
| 34 37 | defp atomize_keys(data) when is_list(data) do |
| 35 38 | for item <- data, into: [], do: atomize_keys(item) |
| 36 39 | end |
| 37 | - |
| 38 40 | defp atomize_keys(data), do: data |
| 39 | - defp snake_keys(data), do: data |
| 41 | + |
| 40 42 | defp is_json(string), do: Regex.match?(~r/^({|\[).*(}|\])$/, string) |
| 41 43 | end |
Loading more files…