Skip to content

Instantly share code, notes, and snippets.

View sescobb27's full-sized avatar
🇨🇴
sisas parce

Simon Escobar Benitez sescobb27

🇨🇴
sisas parce
View GitHub Profile
@sescobb27
sescobb27 / my_app.ex
Created February 6, 2019 18:11 — forked from alanpeabody/my_app.ex
Websockets in Elixir with Cowboy and Plug
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
Plug.Adapters.Cowboy.child_spec(:http, MyApp.Router, [], [
dispatch: dispatch
])
defmodule Topological do
def sort(library) do
g = :digraph.new
Enum.each(library, fn {l,deps} ->
:digraph.add_vertex(g,l) # noop if library already added
Enum.each(deps, fn d -> add_dependency(g,l,d) end)
end)
if t = :digraph_utils.topsort(g) do
print_path(t)
else
defmodule Test.Services.ReleasePollerTest do
use Test.DataCase, async: true
import Mimic
import Test.Factory
alias Test.Services.RPC
setup :verify_on_exit!
test "successfully started polling repository - no error" do
iex(37)> :hackney_trace.disable()                                                                                                                                                
:ok                                                                                                                                                                              
iex(38)> :hackney_trace.enable(:max, :io)
@sescobb27
sescobb27 / docker_parser.ex
Last active September 12, 2018 18:20
simple Dockerfile parser, doesn't support complex RUN expressions with continuation (\)
defmodule DockerApi.DockerfileParser do
@comment ~r/^\s*#/
@continuation ~r/^.*\\\s*$/
@instruction ~r/^\s*(\w+)\s+(.*)$/
def parse(path) do
File.stream!(path)
|> Stream.map(fn line ->
parse_line(line)
end)
defmodule DockerApi do
@version "v1.37"
@endpoint URI.encode_www_form("/var/run/docker.sock")
@protocol "http+unix"
def commit(container_id, payload) do
# commit image
{:ok, %{body: body}} =
HTTPoison.post(
"#{@protocol}://#{@endpoint}/#{@version}/commit?container=#{container_id}",
@sescobb27
sescobb27 / throw_vs_raise
Last active August 29, 2018 15:35
throw vs raise elixir
creating | handling with | where y is
-----------------------------------------------------
raise x | rescue y | %RuntimeError{message: x}
error(x) | rescue y | %ErlangError{original: x}
throw x | catch y | x
exit(x) | catch :exit, y | x
creating | handling with | where y is | and z is
defmodule MyList
def delete_all(list, el) do
_delete_all(list, el, [])
|> Enum.reverse()
end
def _delete_all([el | tail], el, new_list) do
_delete_all(tail, el, new_list)
end
defmodule ReqWithBackoff do
# hakney :connect_timeout - timeout used when establishing a connection, in milliseconds
# hakney :recv_timeout - timeout used when receiving from a connection, in milliseconds
# poison :timeout - timeout to establish a connection, in milliseconds
# :backoff_max - maximum backoff time, in milliseconds
# :backoff_factor - a backoff factor to apply between attempts, in milliseconds
defp fetch(uri) do
options = [
follow_redirect: true,
recv_timeout: Application.get_env(:arc, :recv_timeout, 5_000),