Skip to content

Instantly share code, notes, and snippets.

@erez-rabih
erez-rabih / useless.rb
Created April 10, 2019 14:57
useless.rb
def doit(x)
if x < 1
10 / x
else
x + 1
end
end
app_1 | connecting to rabbitmq (attempt 1)
app_1 | connecting to rabbitmq (attempt 2)
app_1 | connecting to rabbitmq (attempt 3)
app_1 | connected!
app_1 | 2018-06-14 13:47:42 +0000 received message: hello | redelivered: false | first try, rejecting with requeue=true
app_1 | 2018-06-14 13:47:42 +0000 received message: hello | redelivered: true | already retried, rejecting with retry=false
app_1 | 2018-06-14 13:47:47 +0000 Bye
@erez-rabih
erez-rabih / s3_epoch.sh
Created June 12, 2018 10:42
Get S3 object last modified time as unix timestamp
aws s3 ls $S3_FILE_URL | awk '{print $1 " " $2}' | xargs -I {} gdate --date="{}" +%s
defmodule Plug.Validator do
def init(opts), do: opts
def call(conn, opts) do
case conn.private[:validate] do
nil -> conn
validations -> validate(Conn.fetch_query_params(conn), validations, opts[:on_error])
end
end
defmodule Plug.Validator do
def init(opts), do: opts
def call(conn, opts), do: conn
end
@erez-rabih
erez-rabih / validator_text.exs
Last active March 1, 2018 13:12
test/plug/validator_test.exs
defmodule Plug.ValidatorTest do
use ExUnit.Case
use Plug.Test
@subject Plug.Support.Router
@opts @subject.init([])
def assert_json_response(request_url, expected_status, expected_body) do
conn = conn(:get, request_url)
Code.load_file("test/support/validators.exs")
Code.load_file("test/support/router.exs")
ExUnit.start()
defmodule Plug.Support.Validators do
import Plug.Conn
def validate_integer(v) do
case Integer.parse(v) do
:error -> {:error, "could not parse #{v} as integer"}
other -> other
end
end
defmodule Plug.Support.Router do
import Plug.Conn
use Plug.Router
import Plug.Support.Validators,
only: [validate_integer: 1, validate_boolean: 1, on_error_fn: 2, json_resp: 3]
plug :match
plug Plug.Validator, on_error: &on_error_fn/2
plug :dispatch
get “/users/:id”,
private: %{validate: %{id: &validate_integer/1, active: &validate_boolean/1}} do
# Controller logic goes here
end