Skip to content

Instantly share code, notes, and snippets.

View troelskn's full-sized avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
@troelskn
troelskn / combine_gif.rb
Created April 25, 2024 18:34
Combine multiple files into an animated gif with ruby-vips
require 'vips'
images = Dir["input-*.png"].map { |file| Vips::Image.new_from_file(file) }
combined = Vips::Image.arrayjoin(images, across: 1)
buffer = combined.gifsave_buffer(page_height: images.first.height)
gif_image = Vips::Image.new_from_buffer(buffer, "", n: images.length)
gif_image = gif_image.mutate do |y|
y.set!("delay", images.length.times.map { 1000 }) # time is in ms
-- Warcry Wargaming Aura Script adapted by Ziggy and Hood
-- Call functions
function revertNameDesc()
self.setName(og_name)
self.setDescription(og_desc)
end
function setWounds(value)
@troelskn
troelskn / db_fixtures_dump.rake
Last active January 21, 2024 10:26 — forked from ecleel/db_fixtures_dump.rake
Rails 5: Dump Rails db to fixtures
# Original from http://snippets.dzone.com/posts/show/4468 by MichaelBoutros
#
# Optimized version which uses to_yaml for content creation and checks
# that models are ActiveRecord::Base models before trying to fetch
# them from database.
namespace :db do
namespace :fixtures do
desc 'Dumps all models into fixtures.'
task dump: :environment do
models = Dir.glob(Rails.root + 'app/models/**.rb').map do |s|
### Keybase proof
I hereby claim:
* I am troelskn on github.
* I am troelsknkarnov (https://keybase.io/troelsknkarnov) on keybase.
* I have a public key ASDynHl2_xhXgI0QWFmIGy8bfqZ9Ac3iEOXeJ1toyrQvzgo
To claim this, I am signing this object:
### Keybase proof
I hereby claim:
* I am troelskn on github.
* I am troelsknkarnov (https://keybase.io/troelsknkarnov) on keybase.
* I have a public key ASCeF7wPMbEzOllgpHKzqNsMuawATDzUCu-KRCGNxk3aCgo
To claim this, I am signing this object:
@troelskn
troelskn / bjorklund.c
Created January 14, 2023 22:41
Björklund's algorithm for calculating euclidean rhythms in pure C
#define ARRAY_TERMINATE 0
#define TRIGGER 'x'
#define REST '.'
// Will accomodate up to 64 long patterns
// If you want to handle larger sets, you probably need to rewrite this code
// to use dynamic arrays instead
int bjorklun_buffer[64][65];
int zarray_count(int arr[]) {
@troelskn
troelskn / featherwing_midi.ino
Created January 12, 2023 17:22
Basic example of using Adafruit Featherwing MIDI with a Feather RP2040 board
#include <MIDI.h>
#include <HardwareSerial.h>
struct MIDISettings : public midi::DefaultSettings
{
static const long BaudRate = 31250;
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, MIDI, MIDISettings);
void setup()
{
#!/usr/bin/env ruby
require 'nokogiri'
document = Nokogiri::XML.parse(STDIN.read) { |c| c.noent.nonet.strict }
xsl =<<XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:copy-of select="." />
</xsl:template>
module Mojibake
# UTF-8 content, interpreted as latin1
class Utf8
CANARY = "æøåÆØÅ".chars.map { |c| c.encode(Encoding::UTF_8).force_encoding(Encoding::ISO_8859_1).encode(Encoding::UTF_8) }.freeze
def detect?(content)
CANARY.any? { |c| content.include?(c) }
end
def repair(content)
@troelskn
troelskn / smtp_email_validate.rb
Created September 30, 2020 10:09
Validate email via smtp
class EmailValidation
SMTP_PORT = 25
CONNECTION_TIMEOUT = 3
READ_TIMEOUT = 3
REGEX_SMTP_ERROR_BODY_PATTERN = /(?=.*550)(?=.*(user|account|customer|mailbox)).*/i.freeze
def initialize(verifier_domain:, verifier_email:)
@verifier_domain, @verifier_email = verifier_domain, verifier_email
end