Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
@henrik
henrik / config__initializers__exceptions.rb
Last active October 27, 2023 11:29
Sidekiq "silence_errors_during_retries".
# Intended for silent Sidekiq retries: https://www.mikeperham.com/2017/09/29/retries-and-exceptions/
class Exception
attr_accessor :ignore_in_error_reporting
end
@henrik
henrik / app__lib__temporary_network_errors.rb
Created October 27, 2023 11:26
Ruby `TemporaryNetworkErrors.all` – sloppier superset of https://github.com/barsoom/net_http_timeout_errors.
require "net/ssh/proxy/errors"
module TemporaryNetworkErrors
def self.all
[
*NetHttpTimeoutErrors.all,
Errno::EBADF,
IOError,
OpenSSL::SSL::SSLError,
@henrik
henrik / vname.rb
Created May 28, 2010 20:36
Ruby script/command to rename TV series episodes by pattern, e.g. for Plex.
#! /usr/bin/env ruby -r ftools
# "vname" by Henrik Nyh <http://henrik.nyh.se> on 2010-05-28 under the MIT license.
#
# Usage:
#
# vname PATTERN
#
# Prompts to rename files in the working directory matching the PATTERN into this pattern:
#
# X-Files - S01E03 - Pilot.avi
@henrik
henrik / sothebys.rb
Created December 23, 2010 15:24
Sothebys full-size image ripper.
#!/usr/bin/env ruby
# Sothebys full-size image ripper.
# Gets the full-size images from the lots listed after __END__.
# By Henrik Nyh <http://henrik.nyh.se> 2010-12-23 under the MIT license.
require "open-uri"
# We get an error page if we do not specify a user agent.
USER_AGENT = "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)"
@henrik
henrik / rspec_stub_env.rb
Last active July 26, 2023 14:48
Stub ENVs in RSpec.
def stub_env(key, value)
is_set_up_flag = "__stub_env_is_set_up"
unless ENV[is_set_up_flag]
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:[]).with(is_set_up_flag).and_return(true)
end
@henrik
henrik / 1-info.md
Last active May 17, 2023 14:49
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 / .bashrc
Created December 3, 2008 17:56
Git branch and dirty state in Bash prompt.
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
# username@Machine ~/dev/dir[master]$ # clean working directory
# username@Machine ~/dev/dir[master*]$ # dirty working directory
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
@henrik
henrik / el_capitan.md
Last active May 1, 2023 18:24
El Capitan update: Ruby, Rails, homebrew, VirtualBox/Vagrant, Elixir etc.

My notes from updating to El Capitan, as a Ruby/Rails developer working in VirtualBox/Vagrant.

VirtualBox and Vagrant

Our dev environment currently requires us to use older versions of VirtualBox (4.3) and Vagrant (1.7.1).

It seems the new "SIP" protection messes with that version of VirtualBox.

I tried upgrading VirtualBox (to 5.0.6) and Vagrant (to 1.7.4). Those seemed to run fine on El Capitan, but we rely on Chef v. 10 and Vagrant doesn't seem to get along with it (error: "invalid option: --force-formatter").

@henrik
henrik / hash_deep_diff.rb
Created July 14, 2009 08:38
Recursively diff two Ruby hashes.
# Recursively diff two hashes, showing only the differing values.
# By Henrik Nyh <http://henrik.nyh.se> 2009-07-14 under the MIT license.
#
# Example:
#
# a = {
# "same" => "same",
# "diff" => "a",
# "only a" => "a",
# "nest" => {