Add download, json-parsing
This commit is contained in:
parent
9423d7ea71
commit
ae41d2799d
|
@ -0,0 +1,3 @@
|
|||
use Mix.Config
|
||||
|
||||
config :issues, github_url: "https://api.github.com"
|
|
@ -7,7 +7,9 @@ defmodule Issues.CLI do
|
|||
"""
|
||||
|
||||
def run(argv) do
|
||||
parse_args(argv)
|
||||
argv
|
||||
|> parse_args
|
||||
|> process
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
@ -38,4 +40,22 @@ defmodule Issues.CLI do
|
|||
def args_to_internal_representation(_) do # either bad arg or --help
|
||||
:help
|
||||
end
|
||||
|
||||
def process(:help) do
|
||||
IO.puts """
|
||||
usage: issues <user> <project> [count | #{@default_count}]
|
||||
"""
|
||||
System.halt(0)
|
||||
end
|
||||
|
||||
def process({user, project, count}) do
|
||||
Issues.GithubIssues.fetch(user, project)
|
||||
|> decode_response()
|
||||
end
|
||||
|
||||
def decode_response({:ok, body}), do: body
|
||||
def decode_response({:error, error}) do
|
||||
IO.puts "Error fetching from Github: #{error["message"]}"
|
||||
System.halt(2)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
defmodule Issues.GithubIssues do
|
||||
|
||||
@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
|
||||
issues_url(user, project)
|
||||
|> HTTPoison.get(@user_agent)
|
||||
|> handle_response
|
||||
end
|
||||
|
||||
def issues_url(user, project) do
|
||||
"#{@github_url}/repos/#{user}/#{project}/issues"
|
||||
end
|
||||
|
||||
def handle_response( {:ok, %{status_code: status_code, body: body}} ) do
|
||||
{
|
||||
status_code |> check_for_error(),
|
||||
body |> Poison.Parser.parse!()
|
||||
}
|
||||
end
|
||||
|
||||
def handle_response( {_, %{status_code: _, body: body}} ) do
|
||||
{ :error, body }
|
||||
end
|
||||
|
||||
defp check_for_error(200), do: :ok
|
||||
defp check_for_error(_), do: :error
|
||||
end
|
|
@ -21,6 +21,8 @@ defmodule Issues.MixProject do
|
|||
# Run "mix help deps" to learn about dependencies.
|
||||
defp deps do
|
||||
[
|
||||
{:httpoison, "~> 1.8.0"},
|
||||
{:poison, "~> 5.0"},
|
||||
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue