Skip to content

Instantly share code, notes, and snippets.

View s3cur3's full-sized avatar

Tyler A. Young s3cur3

View GitHub Profile
@s3cur3
s3cur3 / disallow_unless.ex
Created September 21, 2023 13:58
A custom Credo check to disallow using the `unless` macro in Elixir
# To use this check:
# 1. Add it to your project at `/lib/credo/disallow_unless.ex`
# 2. Add it to the list of `requires` in `.credo.exs`:
#
# requires: ["lib/credo/disallow_unless.ex"], # <-- add file here
#
# Add the module to the list of `checks` within `.credo.exs`:
#
# checks: [
# {Credo.DisallowUnless}, # <-- add check here
@s3cur3
s3cur3 / telemetry.ex
Created August 8, 2022 13:54
A telemetry handler to log slow events on your Phoenix Channel
defmodule AppServerWeb.Telemetry do
require Logger
@doc """
Attaches an event handler to the "handled in" event on your Phoenix Channels.
Call from within your application.ex initialization.
"""
def attach_telemetry_handlers() do
channel_handled_in = [:phoenix, :channel_handled_in]
@s3cur3
s3cur3 / elixir-ci.yml
Last active April 11, 2022 19:37
Basic Elixir CI (Felt's starting point)
name: Build and Test Elixir Server
on:
push:
branches:
- main
pull_request:
branches:
- main
@s3cur3
s3cur3 / fix_wxwidgets.sh
Last active February 14, 2022 21:39
Install wxwidgets in a way Elixir/Erlang's :observer can use
# If you're here, I assume it's because you tried to run
# :observer for the first time on your Mac and discovered
# it won't work because of some obscure issue with wxwidgets
# (formerly known to Homebrew as wxmac).
#
# This script will fix it for you.
# 1. Remove the old and busted copy
brew uninstall wxwidgets --ignore-dependencies
@s3cur3
s3cur3 / greeter.ex
Created December 2, 2021 15:59
A GenServer that will experience intermittent crashes due to timeouts
defmodule Greeter do
use GenServer
def start_link(opts) do
name = Access.get(opts, :name, __MODULE__)
GenServer.start_link(__MODULE__, name, name: name)
end
def greet(server \\ __MODULE__, name) do
GenServer.call(server, {:greet, name})
@s3cur3
s3cur3 / demo.exs
Last active September 9, 2021 13:44
Elixir GenServer versus Python class
my_user = User.start_link("Tyler", "abcd-efgh-1234-5678")
# Prints "Hi, I'm Tyler"
User.say_hello(my_user)
# Prints a process ID
IO.inspect(my_user)
# Prints the state data:
# %{name: "Tyler", id: "abcd-efgh-1234-5678"}
@s3cur3
s3cur3 / key-transform.swift
Last active March 20, 2021 12:13 — forked from christianselig/milkshake.swift
Generic versions of transforming Dictionary keys and values, inspired by https://twitter.com/ChristianSelig/status/1372961695346352130
extension Dictionary {
func mapKeys<NewKey>(_ transform: (Key) -> NewKey) -> [NewKey: Value] {
reduce(into: [NewKey: Value]()) { result, item in
result[transform(item.key)] = item.value
}
}
}
/// 🥤
extension Dictionary where Key: RawRepresentable {
@s3cur3
s3cur3 / git_checkout_dammit.sh
Created December 21, 2020 19:07
Nukes all the untracked files that Git checkout complains about, then actually performs the checkout
#!/bin/bash
if git checkout "$1" ; then
echo "No untracked files had to be deleted"
else
git checkout "$1" 2>&1 > /dev/null | grep $'\t' | xargs rm
git checkout "$1"
echo "Deleted conflicting untracked files"
fi
@s3cur3
s3cur3 / world_cache.ex
Created July 1, 2020 10:53
Sample gauge usage for AppSignal
defmodule MmoServer.WorldCache do
@moduledoc "A cache of all aircraft in the world, intermittently updated for 'flight tracker' API usage"
use GenServer
require Logger
import Appsignal, only: [set_gauge: 2]
def start_link(_), do: GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
# Log a new connected user
def put(%{session_id: sid, lon: lon, lat: lat} = acf) when is_integer(sid) do
@s3cur3
s3cur3 / explicitly_ignore_return_values.ex
Last active February 8, 2021 18:16
A Credo check to issue a warning on unused return values from functions.
defmodule ExplicitlyIgnoreReturnValues do
@moduledoc """
This is a horrifying hack.
It is a complete copy & paste of Credo's stock unused_operation and unused_function_return_helper,
but modified slightly to reverse the logic; instead of specifying modules and types to warn about when unused,
we specify an "allow list" of the *only* modules & functions to allow ignoring.
See original copypasta source here:
https://github.com/rrrene/credo/blob/master/lib/credo/check/warning/unused_operation.ex