13.6 タスク: ライブラリを使う、から再開。
mix.exs
メタな方でもハマッたソレです。httpoison 導入らしく。
{ :httpoison, "~> 0.8"},
これで 0.8 以上の HTTPoison にマッチとのこと。mix deps
な出力が以下。
$ mix deps
* httpoison (Hex package)
the dependency is not available, run "mix deps.get"
してみます。
$ mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
New:
certifi 2.4.2
hackney 1.15.0
httpoison 0.13.0
idna 6.0.0
metrics 1.0.1
mimerl 1.0.2
parse_trans 3.3.0
ssl_verify_fun 1.1.4
unicode_util_compat 0.4.1
* Getting httpoison (Hex package)
* Getting hackney (Hex package)
* Getting certifi (Hex package)
* Getting idna (Hex package)
* Getting metrics (Hex package)
* Getting mimerl (Hex package)
* Getting ssl_verify_fun (Hex package)
* Getting unicode_util_compat (Hex package)
* Getting parse_trans (Hex package)
再度 mix deps
してみます。
$ mix deps
* parse_trans (Hex package) (rebar3)
locked at 3.3.0 (parse_trans) 09765507
the dependency build is outdated, please run "mix deps.compile"
* mimerl (Hex package) (rebar3)
locked at 1.0.2 (mimerl) 993f9b0e
the dependency build is outdated, please run "mix deps.compile"
* metrics (Hex package) (rebar3)
locked at 1.0.1 (metrics) 25f094de
the dependency build is outdated, please run "mix deps.compile"
* unicode_util_compat (Hex package) (rebar3)
locked at 0.4.1 (unicode_util_compat) d869e4c6
the dependency build is outdated, please run "mix deps.compile"
* idna (Hex package) (rebar3)
locked at 6.0.0 (idna) 689c46cb
the dependency build is outdated, please run "mix deps.compile"
* ssl_verify_fun (Hex package) (mix)
locked at 1.1.4 (ssl_verify_fun) f0eafff8
the dependency build is outdated, please run "mix deps.compile"
* certifi (Hex package) (rebar3)
locked at 2.4.2 (certifi) 75424ff0
the dependency build is outdated, please run "mix deps.compile"
* hackney (Hex package) (rebar3)
locked at 1.15.0 (hackney) 287a5d23
the dependency build is outdated, please run "mix deps.compile"
* httpoison (Hex package) (mix)
locked at 0.13.0 (httpoison) bfaf44d9
the dependency build is outdated, please run "mix deps.compile"
なんというか、不穏な出力だな。これはこれで大丈夫な模様。ここまでが OrganizingAProject-2 になるのかどうか。
GithubIssuesFetch という手続き
ユーザ名とプロジェクト名から issue を格納するデータ構造に変換する手続きとのこと。HTTPoison なリポジトリに手がかりあり、と記載がありますね。Issues.GithubIssues というモジュール作成とのことで以下を投入。
issues/lib/issues/github_issues.ex
defmodule Issues.GithubIssues do
@user_agent [ {"User-agent", "Elixir dave@"}]
def fetch(user, project) do
issues_url(user, project)
|> HTTPoison.get(@user_agent)
|> handle_response
end
def issues_url(user, project) do
"https://github.com/repos/#{user}/#{project}/issues"
end
def handle_response({ :ok, ${status_code: 200, body: body}}) do
{ :ok, body}
end
def handle_response({ ___, %{status_code: ___, body: body}}) do
{ :error, body}
end
end
fetch の書き方とか見易くて良いですよね。
もう一つ
HTTPoison はメインプロセスの外で別なアプリケーションとして実行されるとのこと。これを起動するには mix.exs の application という手続きから、が良い模様。
issues/mix.exs
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :httpoison]
]
end
テキストと微妙に記述が違うな。とりあえずこの状態で iex で実行してみます。
$ iex -S mix
Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]
===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> httpoison
Compiling 2 files (.ex)
Generated httpoison app
==> issues
Compiling 3 files (.ex)
== Compilation error in file lib/issues/github_issues.ex ==
** (SyntaxError) lib/issues/github_issues.ex:14: unexpected token: "$" (column 30, codepoint U+0024)
(elixir) lib/kernel/parallel_compiler.ex:206: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
ここが typo でした。
def handle_response({ :ok, ${status_code: 200, body: body}}) do
{ :ok, body}
end
$ を % にしてリトライ。
$ iex -S mix
Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]
Compiling 3 files (.ex)
warning: the underscored variable "___" appears more than once in a match. This means the pattern will only match if all "___" bind to the same value. If this is the intended behaviour, please remove the leading underscore from the variable name, otherwise give the variables different names
lib/issues/github_issues.ex:18
Generated issues app
Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
で、
iex> Issues.GithubIssues.fetch("elixir-lang", "elixir")
ってしてみたのですが 404 が戻るな。おかしいなと思ってたら url が違うorz
def issues_url(user, project) do
"https://api.github.com/repos/#{user}/#{project}/issues"
end
でしたorz
iex(1)> Issues.GithubIssues.fetch("elixir-lang", "elixir")
{:ok,
いやはや。