Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 1.29 KB

13.7.md

File metadata and controls

61 lines (47 loc) · 1.29 KB

poisonを使ってレスポンスを加工する

poison: JSONライブラリ https://github.com/devinus/poison

goh@goh% mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
Unchanged:
  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 1.1.1
  unicode_util_compat 0.3.1
New:
  poison 3.1.0
* Getting poison (Hex package)

レスポンスに応じてdecodeする

  • {:ok, body}
    bodyを返す
  • {:error, body}
    bodyからエラー情報を抽出して、処理を終了する

データを扱いやすくするため、キーワードリスト(キーと値のtupleのリスト)のリストからマップのリストに変換する

アプリケーションの設定

config.config.exs に設定する

設定の定義

config :アプリケーション名, キー: 値

環境ファイルから設定を読み込む 環境変数 MIX_ENVの値を元に読み込むファイルを切り替える

  • dev.exs
  • test.exs
  • prod.exs

ref. https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html#environments

import_config "#{Mix.env}.exs"

アプリケーションから設定を参照する

Application.get_env(:アプリケーション名, :キー)