Skip to content

Instantly share code, notes, and snippets.

🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆 🦆 🦆 🦆
Block or report user

Report or block henrik

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
henrik / sureflap.rb
Last active Dec 16, 2019
SureFlap API Ruby example.
View sureflap.rb
# SureFlap API code example.
# By Henrik Nyh <> 2019-12-16 under the MIT license.
# Heavily based on the PHP code by Alex Toft.
# Has no dependencies outside the Ruby standard library (uses Net::HTTP directly and painfully).
require "net/http"
require "json"
require "pp"
henrik /
Last active Dec 26, 2019
Automatically launch Cuphead (from Steam) on a Mac when a Nimbus Bluetooth game controller is connected.

The only game I play on the Mac Mini connected to our TV is Cuphead.

I wanted the game to launch automatically when I turn on one of our Nimbus game controllers, which connect via Bluetooth.

This is how:

  • Save the .scpt file below as e.g. ~/Library/Scripts/LaunchCupheadIfControllerIsConnected.scpt
    • In the .scpt, change "Nimbus" if your Bluetooth game controllers have some other name.
  • Save the .plist below as e.g. ~/Library/LaunchAgents/henrik.launch_cuphead_if_controller_is_connected.plist
    • In the .plist, change media to whatever your username is.
henrik /
Created Aug 15, 2019
Wetherspoon Bubblegin recipe

Written down for the benefit of googlers, since I've tried to figure it out to make it at home.

This is my best guess from overhearing things and glancing behind the bar:

  • One part Zymurgorium "Realm of the Unicorn" gin
  • One part Funkin Pro bubblegum syrup
  • Add lemonade
  • Add ice and strawberries cut in half

I've tried the Imaginaria gin and I've tried the Monin bubblegum syrup, but they don't taste the same.

henrik / ignore_exceptions_for_a_while.rb
Last active Jul 12, 2019
Ruby class using Redis to ignore exceptions inside a block unless they've consistently happened for the past X amount of time. Useful e.g. in recurring background workers:
View ignore_exceptions_for_a_while.rb
class IgnoreExceptionsForAWhile
REDIS_KEY = "ignore_exceptions_for_a_while"
def, name:, raise_after:, &block)
exceptions = Array(exception_or_exceptions)
key = "#{REDIS_KEY}:#{name}"
# If the block did not fail, reset this.
henrik / spanish_organization_number.rb
Created Jun 27, 2019
Validate Spanish NIFs ("CIFs") in Ruby.
View spanish_organization_number.rb
# By "organization number" we mean an organization NIF (Spanish tax identification number). We don't mean personal NIFs such as DNI/NIE (see `SpanishIdentityNumber` for that).
# This class also supports personal NIFs starting with the letters K, L or M, though it's inaccurate to call these "organization numbers". More about these:
# It's easier to support them than not to, and means that any such users can provide this number, even though it's represented a bit inaccurately in our system. They're likely to be very rare. Feel free to think up a way to represent it more accurately. (Renaming `spanish_organization_number` to something like `non_dni_or_nie_spanish_tax_identification_number`?)
class SpanishOrganizationNumber
# NIFs do not have the letter prefixes "I" and "O" since those can be confused for the digits "1" and "0".
# Don't know why "T" is not a p
henrik / 1-exhaustive_case_exact_match.rb
Last active Apr 15, 2019
Experimental Ruby exhaustive conditionals that raise if no branch matches. A bit like in e.g. Elm.
View 1-exhaustive_case_exact_match.rb
# This replaces conditions like: `case …; when …; when …; else raise "Nothing matched!"` if an exact match is all you need.
def cond(input, hash)
hash.fetch(input) { raise "No branch for #{input.inspect}!" }.call
input = :good
good: -> { puts "So good!" },
bad: -> { puts "So bad!" },
View gist:42ed93465e7d6299de54aef8636f2a84
<!DOCTYPE html>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
henrik / tz_identifiers_to_rails_identifiers.rb
Created Aug 23, 2018 — forked from jpmckinney/tz_identifiers_to_rails_identifiers.rb
Maps tz database time zone identifiers to Rails time zone identifiers
View tz_identifiers_to_rails_identifiers.rb
# blog post:
"Australia/Adelaide" => "Adelaide",
"Australia/Broken_Hill" => "Adelaide",
"America/Anchorage" => "Alaska",
"America/Juneau" => "Alaska",
"America/Nome" => "Alaska",
"America/Yakutat" => "Alaska",
"Pacific/Gambier" => "Alaska",
"Asia/Almaty" => "Almaty",
henrik / post-commit
Last active Nov 4, 2018
Git hook to run Rubocop on changed files after committing, without locking up the terminal. Bit of a WIP.
View post-commit
#!/usr/bin/env ruby
# diff-filter=AM = only show added and modified, not removed
changed_files = `git diff-tree --no-commit-id --name-only --diff-filter=AM -r HEAD`
unless changed_files.empty? # E.g. an "--allow-empty" commit.
bg_process = fork do
# This will report offenses in the entirety of the updated files, not just the changed lines. Not sure if we could easily get Rubocop to check changed-lines only, but this may be good enough.
rubocop_results = `bundle exec rubocop --color #{changed_files.join(" ")}`
henrik / lgtv_turn_on.rb
Last active Nov 25, 2018
Ruby script to turn on a LGTV running WebOS 3, using "wake on LAN".
View lgtv_turn_on.rb
# Based on: and
# Idea from:
# In order for this to work, first go to the TV "General" settings and turn on "Mobile TV On".
# Change this to the actual value.
# First find the TV IP, e.g. from your router's list of connected devices.
# Then run "arp -a" on a computer and you'll see the MAC by its IP.
# Maybe you need to "ssh <the ip>" before it will turn up in arp. Not sure.
TV_MAC_ADDRESS = "xx:xx:xx:xx:xx:xx"
You can’t perform that action at this time.