Skip to content

Latest commit

 

History

History
197 lines (166 loc) · 10.3 KB

13.6.md

File metadata and controls

197 lines (166 loc) · 10.3 KB

ライブラリの探し方

  1. Elixirの組み込みライブラリ
    Elixirのドキュメント
    http://elixir-lang.org/docs/

  2. Erlangの標準ライブラリ
    以下のサイトの「Application Groups」
    http://erlang.org/doc/

外部ライブラリを探す

  1. hexでパッケージを探す
    hex:
    Elixirのパッケージマネージャ
    http://hex.pm

    FYI

    • RubyGems: Ruby
    • pip: Python
    • npm: Node.js
  2. Google、GitHubで探す

今回は hex.pm にある「HTTPoison」を使う

ライブラリをプロジェクトに追加する

以下のファイルを編集してライブラリを管理する プロジェクトのルートディレクトリ/mix.exs

バージョンの指定の仕方はiexで確認できる

> h Version

Hexのインストール mix deps 実行時に Hex のインストールを求められた

goh@goh% mix deps
Could not find Hex, which is needed to build dependency :httpoison
Shall I install Hex? (if running non-interactively, use "mix local.hex --force") [Yn] Y
* creating /Users/goh/.mix/archives/hex-0.17.7
* httpoison (Hex package)
  the dependency is not available, run "mix deps.get"

依存関係の確認

$ mix deps
goh@goh% mix deps
* httpoison (Hex package)
  the dependency is not available, run "mix deps.get"

ライブラリのインストール 依存関係を解決した上で必要なライブラリをインストールする

$ mix deps.get
goh@goh% mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
New:
  certifi 2.3.1
  hackney 1.12.1
  httpoison 1.1.1
  idna 5.1.1
  metrics 1.0.1
  mimerl 1.0.2
  parse_trans 3.2.0
  ssl_verify_fun 0.1.1
  unicode_util_compat 0.3.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)

インストール後の確認 the dependency build is outdated, please run "mix deps.compile" インストールされているが、まだコンパイルされていない ライブラリは deps ディレクトリに格納される

goh@goh% mix deps
* parse_trans (Hex package) (rebar3)
  locked at 3.2.0 (parse_trans) 2adfa4da
  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.3.1 (unicode_util_compat) a1f612a7
  the dependency build is outdated, please run "mix deps.compile"
* idna (Hex package) (rebar3)
  locked at 5.1.1 (idna) cbc3b2fa
  the dependency build is outdated, please run "mix deps.compile"
* ssl_verify_fun (Hex package) (rebar)
  locked at 1.1.1 (ssl_verify_fun) 28a4d65b
  the dependency build is outdated, please run "mix deps.compile"
* certifi (Hex package) (rebar3)
  locked at 2.3.1 (certifi) d0f42423
  the dependency build is outdated, please run "mix deps.compile"
* hackney (Hex package) (rebar3)
  locked at 1.12.1 (hackney) 8bf2d0e1
  the dependency build is outdated, please run "mix deps.compile"
* httpoison (Hex package) (mix)
  locked at 1.1.1 (httpoison) 96ed7ab7
  the dependency build is outdated, please run "mix deps.compile"

変換に戻る

lib/issues/github_issues.exIssues.GithubIssues.fetch を実装

HTTPoison.start を呼び出すのではなく、OTPを使ってHTTPoisonを実行しておき、Issuesアプリケーションから HTTPoison.get を呼び出す どのアプリケーションを起動するかは mix.exs で指定する

OTP: 実行しているアプリケーションを管理するフレームワーク 詳しくは第Ⅱ部参照

iexでアプリケーションを実行する 初回はコンパイルされている

$ iex -S mix
goh@goh% iex -S mix
Erlang/OTP 20 [erts-9.3.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Could not find "rebar3", which is needed to build dependency :parse_trans
I can install a local copy which is just used by Mix
Shall I install rebar3? (if running non-interactively, use "mix local.rebar --force") [Yn] Y
* creating /Users/goh/.mix/rebar
* creating /Users/goh/.mix/rebar3
===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Package unicode_util_compat-0.3.1 not found. Fetching registry updates and trying again...
===> Updating package registry...
===> Writing registry to /Users/goh/.cache/rebar3/hex/default/registry
===> Generating package index...
===> [appsignal:1.6.2], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.0], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.0-beta.1], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.3], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.1], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> [appsignal:1.6.0-alpha.1], Bad dependency version for httpoison: ~> 0.11 or ~> 1.0.
===> Writing index to /Users/goh/.cache/rebar3/hex/default/packages.idx
===> Compiling idna
==> ssl_verify_fun (compile)
Compiled src/ssl_verify_util.erl
Compiled src/ssl_verify_fingerprint.erl
Compiled src/ssl_verify_pk.erl
Compiled src/ssl_verify_hostname.erl
===> Compiling certifi
===> Compiling hackney
==> httpoison
Compiling 2 files (.ex)
Generated httpoison app
==> issues
Compiling 3 files (.ex)
warning: variable "count" is unused
  lib/issues/cli.ex:44

Generated issues app
Interactive Elixir (1.6.5) - press Ctrl+C to exit (type h() ENTER for help)

elixir-lang ユーザーの elixir プロジェクトからissueの一覧を取得

> Issues.GithubIssues.fetch("elixir-lang", "elixir")
{:ok,
 "[{\"url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7681\",\"repository_url\":\"https://api.github.com/repos/elixir-lang/elixir\",\"labels_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7681/labels{/name}\",\"comments_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7681/comments\",\"events_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7681/events\",\"html_url\":\"https://github.com/elixir-lang/elixir/pull/7681\",\"id\":322551140,\"number\":7681,\"title\":\"Add `@impl true` to Exception callbacks\",\"user\":{\"login\":\"wojtekmach\",\"id\":76071,\"avatar_url\":\"https://avatars0.githubusercontent.com/u/76071?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/wojtekmach\",\"html_url\":\"https://github.com/wojtekmach\",\"followers_url\":\"https://api.github.com/users/wojtekmach/followers\",\"following_url\":\"https://api.github.com/users/wojtekmach/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/wojtekmach/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/wojtekmach/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/wojtekmach/subscriptions\",\"organizations_url\":\"https://api.github.com/users/wojtekmach/orgs\",\"repos_url\":\"https://api.github.com/users/wojtekmach/repos\",\"events_url\":\"https://api.github.com/users/wojtekmach/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/wojtekmach/received_events\",\"type\":\"User\",\"site_admin\":false},\"labels\":[],\"state\":\"open\",\"locked\":false,\"assignee\":null,\"assignees\":[],\"milestone\":null,\"comments\":0,\"created_at\":\"2018-05-12T23:43:43Z\",\"updated_at\":\"2018-05-13T20:59:54Z\",\"closed_at\":null,\"author_association\":\"MEMBER\",\"pull_request\":{\"url\":\"https://api.github.com/repos/elixir-lang/elixir/pulls/7681\",\"html_url\":\"https://github.com/elixir-lang/elixir/pull/7681\",\"diff_url\":\"https://github.com/elixir-lang/elixir/pull/7681.diff\",\"patch_url\":\"https://github.com/elixir-lang/elixir/pull/7681.patch\"},\"body\":\"\"},{\"url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7680\",\"repository_url\":\"https://api.github.com/repos/elixir-lang/elixir\",\"labels_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7680/labels{/name}\",\"comments_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7680/comments\",\"events_url\":\"https://api.github.com/repos/elixir-lang/elixir/issues/7680/events\",\"html_url\":\"https://github.com/elixir-lang/elixir/pull/7680\",\"id\":322514873,\"number\":7680,\"title\":\"Added performant impl for string upcase/downcase `:ascii` mode.\",\"user\":{\"login\":\"tckb\",\"id\":939542,\"avatar_url\":\"https://avatars2.githubusercontent.com/u/939542?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/tckb\",\"html_url\":\"https://github.com/tckb\",\"followers_url\":\"https://api.github.com/users/tckb/followers\",\"following_url\":\"https://api.github.com/users/tckb/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/tckb/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/tckb/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/tckb/subscriptions\",\"organizations_url\":\"https://api.github.com/users/tckb/orgs\",\"repos_url\":\"https://api.github.com/users/tckb/repos\",\"events_url\":\"https://api.github.com/users/tckb/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/tckb/received_events\",\"type\":\"User\",\"site_admin\":false},\"labels\":[],\"state\":\"open\",\"locked\":false,\"assignee\":null,\"assignees\":[],\"milestone\":null,\"comments\":10,\"created_at\":\"2018-05-12T14:16:41Z\",\"updated_at\":\"2018-05-14T19:52:31Z\",\"closed_at\":null,\"author_association\":\"NONE\",\"pull_request\":{\"url\":\"https://api.github.com/repos/elixir-lang/elixir/pulls/7680\",\"html_url\":\"https://github.com/elixir-lang/elixir/pull/7680\",\"diff_url\":\"https://github.com/elixir-lang/elixir/pull/7680.diff\",\"patch_url\":\"https://github.com/elixir-lang/elixir/pull/7680.patch\"},\"body\":\"the existing implementation with binary comprehensions turned out to be\\r\\n_far_ slower than the other modes.\\r\\n\\r\\nThe current implementation is >= 2.5X faster than the earlier implementation\\r\\n\\r\\n---\\r\\nthis PR is a consequence of the discussions i" <> ...}