Skip to content

Instantly share code, notes, and snippets.

mudge / new.html.erb
Last active November 9, 2023 14:51
Using a custom direct upload controller with Rails Active Storage to limit file size before any data is uploaded
<%= form_with model: @post, data: { controller: 'upload', action: 'direct-upload:error@window->upload#error' } do |f| %>
<%= f.file_field(:image, data: { 'direct-upload-url' => uploads_url }) %>
<%= f.submit %>
<% end %>
mudge / webcam-share-ended
Last active October 30, 2023 11:49
A Tuple Trigger to turn an Elgato Key light on and off when your webcam is shared
if [[ "$TUPLE_TRIGGER_IS_SELF" == "true" ]]
curl --silent --request PUT --json '{"lights":[{"on":0}]}' http://elgato-key-light-air-2282.local:9123/elgato/lights
mudge / production.rb
Last active November 21, 2023 14:06
How to configure Rails and Rack::Attack to use the real client IP when running behind Cloudflare
Rails.application.configure do
# Add Cloudflare's IPs to the trusted proxy list so they are ignored when
# determining the true client IP.
# See and
config.action_dispatch.trusted_proxies = ActionDispatch::RemoteIp::TRUSTED_PROXIES + %w[
mudge / api_controller.rb
Last active March 24, 2023 05:27
Instruct Rails to respect the Accept header even if it contains a wildcard
class ApiController < ApplicationController
before_action :only_respect_accept_header
# By default, Rails will ignore the Accept header if it contains a wildcard
# and assume the client wants HTML (or JS if using XMLHttpRequest). See
# If you don't expect your clients to be browsers, we want to override this
mudge / pdftk.rb
Created April 23, 2021 13:16
Homebrew Cask for PDFtk
cask "pdftk" do
version "2.02"
sha256 "c33cf95151e477953cd57c1ea9c99ebdc29d75f4c9af0d5f947b385995750b0c"
url "{version}-mac_osx-10.11-setup.pkg"
name "PDFtk"
desc "Tool for doing everyday things with PDF documents"
homepage ""
pkg "pdftk_server-2.02-mac_osx-10.11-setup.pkg"
mudge / data_uri.rb
Last active December 16, 2022 12:10
A Ruby regular expression to parse data URIs based on RFC 2397.
require 'base64'
class DataUri
REGEXP = %r{
(?<mimetype> .+? / .+? )?
(?<parameters> (?: ; .+? = .+? )* )
mudge /
Last active April 9, 2024 16:41
A Bash script to generate an Unbound configuration to block all domains on The Firebog's "The Big Blocklist Collection" and allow all domains on Anudeep's list
set -euo pipefail
# Download all ticked blocklists from The Firebog's "The Big Blocklist
# Collection" [0] and block access to them with Unbound by redirecting traffic
# to
# [0]:
mudge / pythagorean_means.rb
Last active May 20, 2021 10:55
A Ruby refinement to add methods to Enumerable for calculating the three Pythagorean means.
# A refinement to add methods to Enumerables for calculating the three
# Pythagorean means.
# See
module PythagoreanMeans
# Note that due to a bug refining modules in Ruby 2.7 [1], we can't `refine
# Enumerable` so we `refine Array` instead.
# See also
mudge /
Created August 26, 2019 09:18
Auto-switch Node.js versions with chnode by reading .node-version files
# Based off chruby's
function chnode_auto() {
local dir="$PWD/" version
until [[ -z "$dir" ]]; do
if { read -r version <"$dir/.node-version"; } 2>/dev/null || [[ -n "$version" ]]; then
mudge / use-debounce.js
Last active September 22, 2022 10:14
A custom React Hook for a debounced click handler with a given callback and delay.
* Inspired by Dan Abramov's "Making setInterval Declarative with React Hooks",
* this is a custom hook for debouncing a callback (e.g. for click handlers) such
* that a callback will not be fired until some delay has passed since the last click.
* The callback will automatically be updated with the latest props and state on every
* render meaning that users don't need to worry about stale information being used.
* See for the
* original inspiration.