Skip to content

Instantly share code, notes, and snippets.

@andrepiske
andrepiske / objdiff.rb
Created November 25, 2022 17:26
objdiff Ruby
def arraydiff(ref, other, key=[])
keyname = key.join('.')
desc = []
(other - ref).each do |e|
desc << "#{keyname}: Added '#{e}'"
end
(ref - other).each do |e|
desc << "#{keyname}: Removed '#{e}'"
end
@andrepiske
andrepiske / runpsql
Last active November 29, 2023 15:36
runpsql
#!/usr/bin/env ruby
require 'uri'
want_cmd = false
args = ARGV.select do |a|
if a == '--print'
want_cmd = true
false
else
true
end
@andrepiske
andrepiske / web_utils.rb
Created October 19, 2022 12:26
web_utils
require 'uri'; require 'cgi'
def uri_extract_params(url_str)
u = URI.parse(url_str)
u.query.split('&').map { |x| x.split('=', 2).map { |c| c ? CGI.unescape(c) : nil } }
end
@andrepiske
andrepiske / redis_and_queues.rb
Last active October 17, 2025 15:36
redis_and_queues.rb
r = Redis.new(url: ENV['REDIS_URL'])
def fetch_all_keys(r, batch: 10_000, match: nil)
cursor = '0'
all_keys = []
loop do
cursor, k = r.scan(cursor, count: batch, match: match)
all_keys += k
break if cursor == '0'
end
@andrepiske
andrepiske / postgres.sql
Last active October 20, 2025 20:33
Postgres utils
######################
## Count estimate
SELECT reltuples::bigint AS estimate FROM pg_class WHERE relname='table_name';
######################
## Table size in disk
# source: https://stackoverflow.com/a/2596678/375888
@andrepiske
andrepiske / calculate_signature.rb
Created October 16, 2021 10:56
Calculates sha256 HMAC signature for a given payload
# Calculates sha256 HMAC signature for a given payload
# Useful to check webhook signatures from e.g. github or stripe
def calculate_signature(data, key, algorithm='SHA256')
digest = OpenSSL::Digest.new(algorithm)
hmac = OpenSSL::HMAC.new(key, digest)
hmac << data
hmac.hexdigest
end
@andrepiske
andrepiske / lol.rb
Created September 7, 2021 11:49
Unlolify (ASCII)
def unlolify(n);s=n.to_s(2);s.rjust((s.length/8.0).ceil*8,'0').split('').each_slice(8).to_a.map{|x|x.join.to_i(2).chr}.join;end
def lolify(s);s.split('').map{|x|x.ord.to_s(2).rjust(8,'0')}.join;end
# > unlolify(lolify('Hello, world!').to_i(2))
# => "Hello, world!"