Packages
github_issues
0.3.9
0.4.71
0.4.70
0.4.69
0.4.68
0.4.66
0.4.65
0.4.64
0.4.63
0.4.62
0.4.61
0.4.60
0.4.59
0.4.58
0.4.56
0.4.55
0.4.54
0.4.53
0.4.52
0.4.51
0.4.50
0.4.48
0.4.47
0.4.46
0.4.45
0.4.44
0.4.43
0.4.42
0.4.41
0.4.40
0.4.39
0.4.38
0.4.37
0.4.36
0.4.35
0.4.34
0.4.33
0.4.32
0.4.31
0.4.30
0.4.29
0.4.28
0.4.27
0.4.26
0.4.25
0.4.24
0.4.23
0.4.22
0.4.21
0.4.20
0.4.19
0.4.18
0.4.17
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.24
0.3.23
0.3.22
0.3.21
0.3.20
0.3.19
0.3.18
0.3.17
0.3.16
0.3.15
0.3.14
0.3.13
0.3.12
0.3.11
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.0
0.2.5
0.2.3
0.2.2
0.2.0
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.4
0.1.2
0.1.1
0.1.0
Writes GitHub Issues to "stdio" in a table with borders and colors.
Current section
Files
Jump to
Current section
Files
lib/github/issues.ex
# ┌───────────────────────────────────────────────────────────┐
# │ Inspired by the book "Programming Elixir" by Dave Thomas. │
# └───────────────────────────────────────────────────────────┘
defmodule GitHub.Issues do
# @moduledoc """
# Fetches a list of issues from a GitHub project.
# """
@moduledoc false
use PersistConfig
alias GitHub.Issues.CLI
require Logger
@typep issue :: map
@url_template Application.get_env(@app, :url_template)
@doc """
Fetches issues from a GitHub `project` of a given `user`.
Returns a tuple of either `{:ok, [issue]}` or `{:error, text}`.
## Parameters
- `user` - GitHub user
- `project` - GitHub project
- `url_template` - URL template
## Examples
alias GitHub.Issues
Issues.fetch("laravel", "elixir")
"""
@spec fetch(CLI.user, CLI.project, String.t) ::
{:ok, [issue]} | {:error, String.t}
def fetch(user, project, url_template \\ @url_template) do
Logger.info("Fetching GitHub Issues from #{user}/#{project}...")
try do
with url <- url(url_template, user, project),
{:ok, %{status_code: 200, body: body}} <- HTTPoison.get(url) do
{:ok, :jsx.decode(body, [:return_maps])}
else
{:ok, %{status_code: 301}} -> {:error, "status code: 301 (not found)"}
{:ok, %{status_code: 404}} -> {:error, "status code: 404 (not found)"}
{:error, %{reason: reason}} -> {:error, "reason: #{inspect reason}"}
end
rescue
error -> {:error, "exception: #{Exception.message(error)}"}
end
end
## Private functions
# @doc """
# Returns a URL based on `user` and `project`.
# ## Parameters
# - `url_template` - URL template
# - `user` - user
# - `project` - project
# ## Examples
# iex> alias GitHub.Issues
# iex> app = Mix.Project.config[:app]
# iex> url_template = Application.get_env app, :url_template
# iex> Issues.url url_template, "laravel", "elixir"
# "https://api.github.com/repos/laravel/elixir/issues"
# iex> alias GitHub.Issues
# iex> url_template = "elixir-lang.org/<project>/{user}/wow"
# iex> Issues.url url_template, "José", "Elixir"
# "elixir-lang.org/Elixir/José/wow"
# """
@spec url(String.t, CLI.user, CLI.project) :: String.t
defp url(url_template, user, project) do
url_template
|> String.replace(~r/{user}|<user>/, user)
|> String.replace(~r/{project}|<project>/, project)
end
end