Skip to content

Instantly share code, notes, and snippets.

View ivorpad's full-sized avatar
🇪🇸
Working from Spain

Ivor ivorpad

🇪🇸
Working from Spain
View GitHub Profile
# config/initializers/method_source_locator.rb
# Utility to find the source location of a method based on its receiver and name
def source_location(receiver, method_name)
method_object = if receiver.singleton_class.method_defined?(method_name) || receiver.singleton_class.private_method_defined?(method_name)
# Class method
receiver.method(method_name)
elsif receiver.method_defined?(method_name) || receiver.private_method_defined?(method_name)
# Instance method
receiver.instance_method(method_name)
@ivorpad
ivorpad / .js
Created July 18, 2023 07:05
Convert AWS IAM credentials to AWS SMTP credentials
const crypto = require("crypto");
const secretAccessKey = ""; // AWS_ACCESS_KEY_SECRET
const regionName = "us-east-1";
// Assuming you've already set up your SES Policy on your IAM User:
// {
// "Version": "2012-10-17",
// "Statement": [
// {
@ivorpad
ivorpad / .sql
Created June 20, 2023 07:53
Database Introspection
-- Generates multiple CREATE TABLE statements (without INDEXES)
SELECT string_agg(create_table_stmt, E'\n\n' ORDER BY tablename)
FROM (
SELECT
tablename,
'CREATE TABLE ' || tablename || ' (' || column_defs || ');' as create_table_stmt
FROM (
SELECT
@ivorpad
ivorpad / mitmproxy.md
Created May 8, 2023 15:35
Testing an API Replacement with mitmproxy and Chrome

Redirect API calls from a live URL to localhost when path signatures are different

Introduction

When developing a new version of an API that's meant to replace an existing one, you may need to test the new API using the live URL to ensure proper functionality. However, if the path signatures of the new API are different from the existing one, using /etc/hosts to redirect the calls won't be sufficient. In this article, we will demonstrate how to use mitmproxy to redirect API calls from the live URL to your localhost, allowing you to test the new API with different path signatures.

Prerequisites

Before we begin, make sure you have the following software installed:

#!/usr/bin/env bash
# wait for a program to start listening on a PORT ($1)
wait_for_port() {
local PORT=$1
echo "waiting for $PORT"
for i in `seq 1 60`;
do
nc -z localhost $PORT && echo "port $PORT is ready" && return
echo -n .
@ivorpad
ivorpad / scheduler.rb
Created August 31, 2022 11:05
Start Job in sidekiq-scheduler dynamically
Sidekiq.schedule = {"hello_world"=>{"dynamic"=>true, "cron"=>"* * * * * *", "class"=>"HelloJob", "enabled"=>false}}
Sidekiq::Scheduler.enabled = true
Sidekiq::Scheduler.reload_schedule!
# or
SidekiqScheduler::RedisManager.get_job_state("hello_world") # "{\"enabled\":false}"
state = {"enabled" => true}
SidekiqScheduler::RedisManager.set_job_state("hello_world", state)
@ivorpad
ivorpad / .sh
Created April 8, 2021 21:34
Install Node with n in Apple Silicon
n --arch x64 10.16.0
@ivorpad
ivorpad / machine.js
Created April 1, 2021 14:41
Generated by XState Viz: https://xstate.js.org/viz
let count = 0;
const functionThatCanFail = async () => {
const urls = [
`https://api.github.com/users/mojombo`,
`https://api.github.com/users/ivorpad`,
`https://api.github.com/users/failtofetch`,
`https://api.github.com/users/davidkpiano`
];
const user = await fetch(urls[count]);
@ivorpad
ivorpad / machine.js
Created April 1, 2021 11:44
Generated by XState Viz: https://xstate.js.org/viz
const functionThatCanFail = async () => {
const urls = [
`https://api.github.com/users/failtofetch`,
`https://api.github.com/users/mojombo`
];
return await fetch(urls[Math.floor(Math.random() * 2)]);
};
const healthCheck = () => {
return new Promise((resolve, reject) => {
@ivorpad
ivorpad / machine.js
Created January 28, 2021 05:54
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions