Skip to content

Instantly share code, notes, and snippets.

Avatar
🦆
🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆
🦆 🦆 🦆 🦆
View GitHub Profile
@henrik
henrik / example.ex
Created Aug 29, 2020
Elixir "@foo bar" macro example
View example.ex
defmodule MyMacro do
defmacro @{name, _meta, [arg]} do
IO.inspect "Your #{name} is a #{arg}"
end
defmacro __using__(_) do
quote do
import Kernel, except: [@: 1]
import unquote(__MODULE__)
end
@henrik
henrik / app.js
Last active May 23, 2020
Phoenix LiveView hook that makes links with a "phx-click" still trigger the default navigation event, and also prevents clicks on buttons inside these links from doing the same.
View app.js
// Fixes two issues:
// - Clicking a link with a `phx-click` attribute did not cause the link default (navigation) to trigger.
// - Clicking a button inside the link *would* cause the link default to trigger.
Hooks.AllowLinkDefaultAndPreventNestedDefault = {
mounted() {
this.el.addEventListener("click", (e) => {
// `closest` in case we click an element inside a button, e.g. an icon.
if (e.target.closest("button")) {
e.preventDefault()
} else {
@henrik
henrik / Z_payloads.md
Last active May 26, 2020
LiveView rendering lab
View Z_payloads.md

Lab (LiveView 0.13.0, Phoenix 1.5.3)

A - straight for loop

First render, 8233 bytes

["4","4","lv:phx-FhF6gwiU2cjuXjoB","phx_reply",{"response":{"rendered":{"0":"<a class="tabs__tab " data-phx-link="patch" data-phx-link-state="push" href="/"><i class="fas fa-eye mr-1">\nCommits\n","1":"<a class="tabs__tab " data-phx-link="patch" data-phx-link-state="push" href="/comments"><i class="fas fa-comments mr-1">\nComments\n","2":"<a class="tabs__tab " data-phx-link="patch" data-phx-link-state="push" href="/settings"><i class="fas fa-cog mr-1">\nSettings\n","3":"","4":"","5":{"0":{"d":[["1","Item 1","1"],["2","Item 2","2"],["3","Item 3","3"],["4","Item 4","4"],["5","Item 5","5"],["6","Item 6","6"],["7","Item 7","7"],["8","Item 8","8"],["9","Item 9","9"],["10","Item 10","10"],["11","Item 11","11"],["12","Item 12","12"],["13","Item 13","13"],["14","Item 14","14"],["15","Item 15","15"],["16","Item 16","16"],["17","Item 17","17"],["18","Item 18","18"],["19

@henrik
henrik / consignor_portal_data_ws_example.rb
Created Mar 12, 2020
Using the Consignor Portal Data WS (web service) with Savon in Ruby.
View consignor_portal_data_ws_example.rb
require "savon"
client = Savon.client(
pretty_print_xml: true,
log: true,
wsdl: "https://customer-api.consignorportal.com/PortalData/PortalData.svc?singleWsdl",
namespace_identifier: "edis",
)
#pp client.operations
@henrik
henrik / gemdiffs.rb
Last active Apr 20, 2020
Ruby script (gemdiffs.rb) to generate Coditsu gem diffing URLs from a Gemfile.lock diff, to help catch hijacked gems, or just to keep on top of changes. Also supports gems sourced straight from GitHub. There's also updategems.rb which updates gems and calls gemdiffs.rb to pre-fill the commit message.
View gemdiffs.rb
#!/usr/bin/env ruby
# Usage example (in a Terminal):
#
# bundle update
# script/gemdiffs.rb
# Rubygems version diffs.
puts `git diff Gemfile.lock`.lines.
select { |line| line.match?(/^[+-] \w/) }.
@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.

You can’t perform that action at this time.