Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
@henrik
henrik / factory_bot_extensions.rb
Created January 31, 2020 16:11
FactoryBot extensions
# Do this in a factory to get this method:
# require "factory_bot_extensions"
# We should just load it once somewhere, but have yet to figure out where because FactoryBot is loaded in some fancy way.
#
# Example usage:
#
# factory :item do
# transient do
# company {
# if passed_in?(:contract)
@henrik
henrik / sureflap.rb
Last active December 16, 2019 20:36
SureFlap API Ruby example.
# SureFlap API code example.
#
# By Henrik Nyh <https://henrik.nyh.se> 2019-12-16 under the MIT license.
# Heavily based on the https://github.com/alextoft/sureflap 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
henrik / 1-info.md
Last active September 25, 2024 08:05
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
henrik / bubblegin_recipe.md
Created August 15, 2019 09:26
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
henrik / ignore_exceptions_for_a_while.rb
Last active June 10, 2020 08:51
Ruby/Redis code to ignore exceptions unless they've consistently happened for the past X amount of time. Useful e.g. in recurring background workers: https://twitter.com/henrik/status/1149595443992485896 And outside workers, too: https://twitter.com/henrik/status/1270638270901366785 Goes well with https://github.com/barsoom/net_http_timeout_errors.
class IgnoreExceptionsForAWhile
REDIS_KEY = "ignore_exceptions_for_a_while"
IGNORED_EXCEPTION_RETURN_VALUE = :_ignored_exception
# This exception should be ignored in our exception logger (e.g. Honeybadger) config, so devs won't be notified about it.
#
# This can be used to let a UI show some user-facing error without notifying devs until after the `raise_after` timeout.
# Either by just relying on e.g. Ajax request error callbacks to show a helpful error message, or by rescuing this error and showing a message.
#
# You can call `#cause` on this exception to get the original exception.
@henrik
henrik / spanish_organization_number.rb
Created June 27, 2019 10:03
Validate Spanish NIFs ("CIFs") in Ruby.
# 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: https://translate.google.co.uk/translate?sl=auto&tl=en&u=https%3A%2F%2Fes.wikipedia.org%2Fwiki%2FN%25C3%25BAmero_de_identificaci%25C3%25B3n_fiscal
#
# 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
henrik / 1-exhaustive_case_exact_match.rb
Last active April 15, 2019 09:54
Experimental Ruby exhaustive conditionals that raise if no branch matches. A bit like in e.g. Elm.
# 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
end
input = :good
cond(input,
good: -> { puts "So good!" },
bad: -> { puts "So bad!" },
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.3.0.js"></script>
</head>
<body>
<script type="text/javascript">
dymo.label.framework.checkEnvironment();
</script>
@henrik
henrik / tz_identifiers_to_rails_identifiers.rb
Created August 23, 2018 13:15 — forked from jpmckinney/tz_identifiers_to_rails_identifiers.rb
Maps tz database time zone identifiers to Rails time zone identifiers
# blog post: http://blog.slashpoundbang.com/post/2613268281/changing-from-tz-database-identifiers-to-rails-friendly
{
"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
henrik / post-commit
Last active August 3, 2022 20:04
Git hook to run Rubocop on changed files after committing, without locking up the terminal. Bit of a WIP.
#!/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`.lines.map(&:chomp)
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(" ")}`