Compare commits
4 Commits
455c472635
...
a82059d8dd
Author | SHA1 | Date |
---|---|---|
Nathan Mattes | a82059d8dd | |
Nathan Mattes | 4042f8893f | |
Nathan Mattes | a391104a28 | |
Nathan Mattes | d1bc22f745 |
|
@ -1,3 +1,5 @@
|
|||
use Mix.Config
|
||||
|
||||
config :issues, github_url: "https://api.github.com"
|
||||
|
||||
config :logger, compile_time_purge_level: :info
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
defmodule Issues.CLI do
|
||||
|
||||
import Issues.TableFormatter, only: [print_table_for_columns: 2]
|
||||
|
||||
@default_count 4
|
||||
|
||||
@moduledoc """
|
||||
|
@ -54,7 +57,7 @@ defmodule Issues.CLI do
|
|||
|> decode_response()
|
||||
|> sort_into_descending_order
|
||||
|> last(count)
|
||||
|> show_issues_as_table
|
||||
|> show_issues_as_table # alternative: print_table_for_columns(["id", "created_at", "title"])
|
||||
end
|
||||
|
||||
def decode_response({:ok, body}), do: body
|
||||
|
@ -100,12 +103,12 @@ defmodule Issues.CLI do
|
|||
|
||||
# print the title
|
||||
IO.puts(
|
||||
"#{String.pad_trailing("#id", id_column_width)} | #{String.pad_trailing("created_at", creation_date_width)} | #{String.pad_trailing("title", title_length)} "
|
||||
"#{String.pad_trailing("#id", id_column_width)} | #{String.pad_trailing("created_at", creation_date_width)} | #{String.pad_trailing("title", title_length)}"
|
||||
)
|
||||
|
||||
# print seperator
|
||||
IO.puts(
|
||||
"#{String.pad_trailing("-", id_column_width, "-")}-+-#{String.pad_trailing("-", creation_date_width, "-")}-+-#{String.pad_trailing("-", title_length, "-")}-"
|
||||
"#{String.duplicate("-", id_column_width)}-+-#{String.duplicate("-", creation_date_width)}-+-#{String.duplicate("-", title_length)}"
|
||||
)
|
||||
|
||||
# print each issue
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
defmodule Issues.GithubIssues do
|
||||
|
||||
require Logger
|
||||
|
||||
@user_agent [ { "User-agent", "Elixir dave@pragprog.com" } ]
|
||||
# use a module attribute to fetch the value at compile time
|
||||
@github_url Application.get_env(:issues, :github_url)
|
||||
|
||||
def fetch(user, project) do
|
||||
Logger.info("Fetching #{user}'s project #{project}")
|
||||
|
||||
issues_url(user, project)
|
||||
|> HTTPoison.get(@user_agent)
|
||||
|> handle_response
|
||||
|
@ -15,6 +19,8 @@ defmodule Issues.GithubIssues do
|
|||
end
|
||||
|
||||
def handle_response( {:ok, %{status_code: status_code, body: body}} ) do
|
||||
Logger.info("Got response: status code=#{status_code}")
|
||||
Logger.debug(fn -> inspect(body) end)
|
||||
{
|
||||
status_code |> check_for_error(),
|
||||
body |> Poison.Parser.parse!()
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
defmodule Issues.TableFormatter do
|
||||
import Enum, only: [each: 2, map: 2, map_join: 3, max: 1]
|
||||
|
||||
def print_table_for_columns(rows, headers) do
|
||||
with data_by_columns = split_into_columns(rows, headers),
|
||||
column_widths = widths_of(data_by_columns),
|
||||
format = format_for(column_widths) do
|
||||
puts_one_line_in_columns(headers, format)
|
||||
IO.puts(separator(column_widths))
|
||||
puts_in_columns(data_by_columns, format)
|
||||
end
|
||||
end
|
||||
|
||||
def split_into_columns(rows, headers) do
|
||||
for header <- headers do
|
||||
for row <- rows, do: printable(row[header])
|
||||
end
|
||||
end
|
||||
|
||||
def printable(str) when is_binary(str), do: str
|
||||
def printable(str), do: to_string(str)
|
||||
|
||||
def widths_of(columns) do
|
||||
for column <- columns, do: column |> map(&String.length/1) |> max
|
||||
end
|
||||
|
||||
def format_for(columns_width) do
|
||||
map_join(columns_width, " | ", fn width -> "~-#{width}s" end) <> "~n"
|
||||
end
|
||||
|
||||
def separator(column_widths) do
|
||||
map_join(column_widths, "-+-", fn width -> List.duplicate("-", width) end)
|
||||
end
|
||||
|
||||
def puts_in_columns(data_by_columns, format) do
|
||||
data_by_columns
|
||||
|> List.zip()
|
||||
|> map(&Tuple.to_list/1)
|
||||
|> each(&puts_one_line_in_columns(&1, format))
|
||||
end
|
||||
|
||||
def puts_one_line_in_columns(fields, format) do
|
||||
:io.format(format, fields)
|
||||
end
|
||||
end
|
|
@ -4,10 +4,13 @@ defmodule Issues.MixProject do
|
|||
def project do
|
||||
[
|
||||
app: :issues,
|
||||
escript: escript_config(),
|
||||
version: "0.1.0",
|
||||
elixir: "~> 1.12",
|
||||
start_permanent: Mix.env() == :prod,
|
||||
deps: deps()
|
||||
deps: deps(),
|
||||
name: "Issues",
|
||||
source_url: "https://git.zeitschlag.net/zeitschlag/elixir-book",
|
||||
]
|
||||
end
|
||||
|
||||
|
@ -23,8 +26,17 @@ defmodule Issues.MixProject do
|
|||
[
|
||||
{:httpoison, "~> 1.8.0"},
|
||||
{:poison, "~> 5.0"},
|
||||
{:ex_doc, "~> 0.28"},
|
||||
{:earmark, "~> 1.4.18"},
|
||||
|
||||
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
]
|
||||
end
|
||||
|
||||
defp escript_config do
|
||||
[
|
||||
main_module: Issues.CLI
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue