Skip to content

Instantly share code, notes, and snippets.

Avatar
🦆
🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆
🦆 🦆 🦆 🦆
View GitHub Profile
@henrik
henrik / example.jxa
Created Feb 5, 2020
JXA script (JavaScript AppleScript) to set Photos.app titles ("names") from filenames, but changing the first letter to uppercase.
View example.jxa
var Photos = Application("Photos")
var currentSelection = Photos.selection()
for (var item of currentSelection) {
const oldFilename = item.filename()
const newFilename = oldFilename.charAt(0).toUpperCase() + oldFilename.slice(1)
item.name = newFilename
}
View factory_bot_extensions.rb
# 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 Dec 16, 2019
SureFlap API Ruby example.
View sureflap.rb
# 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 Apr 22, 2020
Automatically launch Cuphead (from Steam) on a Mac when a Nimbus Bluetooth game controller is connected.
View 1-info.md

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 Aug 15, 2019
Wetherspoon Bubblegin recipe
View bubblegin_recipe.md

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 Jun 10, 2020
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.
View ignore_exceptions_for_a_while.rb
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 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: 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 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
end
input = :good
cond(input,
good: -> { puts "So good!" },
bad: -> { puts "So bad!" },
View gist:42ed93465e7d6299de54aef8636f2a84
<!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 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: 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",