Skip to content

Instantly share code, notes, and snippets.

View karlosmid's full-sized avatar

Karlo Smid karlosmid

View GitHub Profile
@karlosmid
karlosmid / birthdays.rb
Last active February 11, 2022 11:41
birthdays manipulation
def reverse_birthdays birthdays
result = {}
birthdays.map { |day,names| names.map { |name| result[name] = day } }
result
end
actual = {"2019-01-01"=>["karlo01", "karlo02"], "2019-01-02"=>["ben01", "ben02"]}
expected = {"karlo01"=>"2019-01-01", "karlo02"=>"2019-01-01", "ben01"=>"2019-01-02", "ben02"=>"2019-01-02"}
expected == reverse_birthdays(actual)
@karlosmid
karlosmid / error.ex
Last active August 14, 2021 14:32
Cowboy error for HTTP 431
[error] [early_error_time: -576460745611033000, partial_req: %{headers: %{"accept" => "*/*", "host" => "localhost:5000", "user-agent" => "curl/7.64.1"}, method: "GET", path: "/healthcheck", peer: {{127, 0, 0, 1}, 51940}, qs: "", ref: YourAppName.Endpoint.HTTP, version: :"HTTP/1.1"}, pid: #PID<0.718.0>, reason: {:connection_error, :limit_reached, :"A header value is larger than configuration allows. (RFC7230 3.2.5, RFC6585 5)"}, ref: YourAppName.Endpoint.HTTP, resp_headers: %{"connection" => "close", "content-length" => "0"}, resp_status: 431, streamid: 1]
@karlosmid
karlosmid / 431_error.ex
Created August 14, 2021 14:28
HTTP 431 logged by Cowboy HTTP server
Cowboy returned 431 because it was unable to parse the request headers. This may happen because there are no headers, or there are too many headers or the header name or value are too large (such as a large cookie). You can customize those values when configuring your http/https server. The configuration option and default values are shown below: protocol_options: [ max_header_name_length: 64, max_header_value_length: 4096, max_headers: 100 ]
@karlosmid
karlosmid / telemetry.ex
Created August 14, 2021 14:22
Handle the broken HTTP request
defmodule YourAppName.LogIncompleteRequestHandler do
require Logger
def handle_event([:cowboy, :request, :early_error], _response, request, nil) do
Logger.error(IO.inspect(request))
end
end
@karlosmid
karlosmid / application.ex
Created August 14, 2021 14:17
how to log broken HTTP request in Phoenix
:ok = :telemetry.attach(
"cowboy-request-handler",
[:cowboy, :request, :early_error],
&YourAppName.LogIncompleteRequestHandler.handle_event/4,
nil
)
@karlosmid
karlosmid / function_guards.ex
Created February 23, 2021 20:52
Example of function guards
defmodule Vat do
@moduledoc """
Elixir In Action book example for function overloading
"""
def price(%{country: :germany, price: price}), do: calc_total_price(1.19, price)
def price(%{country: :greece, price: price}), do: calc_total_price(1.24, price)
def price(%{country: :croatia, price: price}), do: calc_total_price(1.25, price)
def price(unknown), do: {:unknown_country_data, unknown}
defp calc_total_price(vat, price) when vat > 1.20, do: "Expensive country: " <> "#{vat * price}"
@karlosmid
karlosmid / forbid_add_column.html
Created February 19, 2021 14:21
Forbid adding columns to jExcel
<html>
<script src="https://bossanova.uk/jspreadsheet/v3/jexcel.js"></script>
<link rel="stylesheet" href="https://bossanova.uk/jspreadsheet/v3/jexcel.css" type="text/css" />
<script src="https://jsuites.net/v3/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v3/jsuites.css" type="text/css" />
<div id='my-spreadsheet'></div>
<script>
data = [
defmodule Vat do
@moduledoc """
Elixir In Action book example for function overloading
"""
def price(%{country: :germany, price: price}), do: 1.19 * price
def price(%{country: :greece, price: price}), do: 1.24 * price
def price(%{country: :croatia, price: price}), do: 1.25 * price
def price(unknown), do: {:unknown_country_data, unknown}
end
@karlosmid
karlosmid / function_overload.ex
Created February 12, 2021 13:56
Example for Elixir function overloading
defmodule Vat do
@moduledoc """
Elixir In Action book example for function overloading
"""
def price(%{country: :germany, price: price}), do: 1.19 * price
def price(%{country: :greece, price: price}), do: 1.24 * price
def price(%{country: :croatia, price: price}), do: 1.25 * price
end
@karlosmid
karlosmid / test_helpers.exs
Created February 7, 2021 16:46
test helpers file for Mox
Mox.defmock(ExKeyCDN.MockZone, for: ExKeyCDN.ZoneBehaviour)
Application.put_env(:exkeycdn, :zone, ExKeyCDN.MockZone)