Skip to content

Instantly share code, notes, and snippets.

View SophieDeBenedetto's full-sized avatar

Sophie DeBenedetto SophieDeBenedetto

  • GitHub
  • New York, NY
View GitHub Profile
set +e
echo "Preparing to run migrations"
while true; do
nodetool ping
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "Application is up!"
break
# rel/config.exs
use Mix.Releases.Config,
 default_release: :default,
 default_environment: Mix.env()
environment :prod do
 set include_erts: true
 set include_src: false
 set post_start_hooks: "rel/hooks/post_start"
 set cookie: :"~AP{<;FKd_!~gObmS]h.HBYU4z&[1p(>tvCXo`I1qo*ZDVM)f0c>Slw~`R.47O]C"
defmodule GithubClient do
@base_url "https://api.github.com/v3"
def create_repo(params) do
HTTPoison.post(@base_url <> "/repos", params, headers)
|> handle_response
end
defp handle_response(resp) do
case resp do
defmodule GithubClient.Test do
use ExUnit.Case, async: false
import GithubRepo
import Mock
@create_repo_params %{name: "my-new-repo"}
test_with_mock "create_repo when success", HTTPoison,
[post: fn("repos", @create_repo_params, _headers) ->
%HTTPoison.Response{body: %{name: "my-new-repo", id: 1}, headers: _list, request_url: _url, status_code: 200} end] do
defp deps do
[
{:dialyxir, "~> 0.5", only: [:dev], runtime: false},
{:httpoison, "~> 1.0"},
{:plug_cowboy, "~> 2.0"}
]
end
# mix.exs
def application do
[
extra_applications: [:logger],
mod: {GithubClient.Application, [env: Mix.env]},
applications: applications(Mix.env)
]
end
defp applications(:test), do: applications(:default) ++ [:cowboy, :plug]
# lib/github_client/mock_server.ex
defmodule GithubClient.MockServer do
use GenServer
alias GithubClient.MockServer.GithubApiMockController
def init(args) do
{:ok, args}
end
# lib/github_client/mock_server.ex
defmodule GithubClient.MockServer do
use Plug.Router
end
# lib/github_client/application.ex
...
def start(_type, args) do
children = case args do
[env: :prod] -> []
[env: :test] -> [{Plug.Cowboy, scheme: :http, plug: GithubClient.MockServer, options: [port: 8081]}]
[env: :dev] -> []
[_] -> []
end
defmodule GithubClient.MockServer do
use Plug.Router
plug Plug.Parsers, parsers: [:json],
pass: ["text/*"],
json_decoder: Poison
plug :match
plug :dispatch
end