Skip to content

Instantly share code, notes, and snippets.

View synth's full-sized avatar

Peter Philips synth

View GitHub Profile
@synth
synth / flag_registration.rb
Created February 14, 2024 06:33
Feature flag code detection
module Flipper
module FlagRegistration
# These functions are all memoized because they should be static for the
# lifetime of a deployment (albeit they are really static to a Ruby process)
def self.registered_flags
@registered_flags ||= YAML.load_file("config/feature_flags.yml")
end
def self.flags_in_code
@flags_in_code ||= begin
@synth
synth / DbMaxLengthValidator.rb
Last active October 19, 2022 13:51
Custom Validator for Max Length based on database adapter
module DbMaxLengthValidatorConcern
extend ActiveSupport::Concern
included do
cattr_accessor :column_varchar_max_lengths
self.column_varchar_max_lengths = {}
end
module ClassMethods
@synth
synth / truncation.rb
Created September 4, 2022 01:18
Ruby port of truncation.js
# Ported from a node script that seemed to do truncation well
# https://github.com/huang47/nodejs-html-truncate/blob/master/lib/truncate.js
class Trunc
def self.trunc(str, max_length, options = {})
new(str, max_length, options).trunc
end
attr_reader :str
EMPTY_OBJECT = {}
@synth
synth / openapi_converter.rb
Created August 26, 2021 05:14
Convert Swagger2 to OpenApi3
require 'restclient'
class OpenapiConverter
CONVERTER_URL = "https://converter.swagger.io/api/convert"
CONVERTED_FILE_PATH = File.join(Rails.root, "/public/api/docs")
CONVERTED_FILENAME = "swagger-spec-v3.json"
LOGGER = Rails.logger
attr_accessor :url
Rails.application.eager_load!
exclude_tables = ["sessions"]
row_limit_per_table = nil
tables = []
ApplicationRecord.descendants.each do |model|
next if exclude_tables.any?{ |skip_table| skip_table == model.table_name }
next if tables.include?(model.table_name) # This covers STI
puts "Exporting: #{model.table_name}"
@synth
synth / csrf-token-comparison.rb
Created March 24, 2020 23:41
Compare Encoded and Masked CSRF Token with CSRF in the Database
# Note: This is abstracted from Rails 5.0.x
encoded_masked_token = "" # Enter masked csrf that is rendered as meta tag or form parameter
csrf_token = ActiveRecord::SessionStore::Session.last.data["_csrf_token"] # Enter csrf token that is saved in database
AUTHENTICITY_TOKEN_LENGTH=32
def unmask_token(masked_token)
# Split the token into the one-time pad and the encrypted
# value and decrypt it
object.singleton_class.instance_eval do
attr_accessor :label
end
@synth
synth / application_helper.rb
Last active March 29, 2016 02:12
Ad hoc simple form calendar input
module ApplicationHelper
def mmddyyyy_date_input_tag(scope, attribute, value)
object = Struct.new(attribute, :class).new(value, Hashie::Mash.new(model_name: "Date"))
builder = SimpleForm::FormBuilder.new(scope, object, self, {}, nil)
input = DateMmddyyyyInput.new(builder, attribute, nil, nil)
input.input
end
end
@synth
synth / ecmabara.rb
Created February 12, 2016 01:26
Ecmabara - A foolish attempt to reimplement capybara-webkit in pure js
module Ecmabara
def self.included(base_class)
base_class.class_eval do
def self.new
super.extend Page
end
end
end
@synth
synth / gist:d1045e129a5f35d87b69
Last active May 25, 2021 19:44
High quality gifs on OSX with ffmpeg
brew install ffmpeg ImageMagic mplayer gifsicle
# Option 1
# http://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
ffmpeg -y -i yourmovie.mov -vf fps=10,scale=800:-1:flags=lanczos,palettegen palette.png
ffmpeg -i yourmovie.mov -i palette.png -filter_complex "fps=10,scale=800:-1:flags=lanczos[x];[x][1:v]paletteuse" yourgif.gif
# Option 2
# https://gist.github.com/dergachev/4627207
ffmpeg -i in.mov -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > out.gif