Skip to content

Instantly share code, notes, and snippets.

View michaeltelford's full-sized avatar

Michael Telford michaeltelford

View GitHub Profile
@michaeltelford
michaeltelford / caBundle.sh
Last active January 26, 2024 12:14
HTTPS (TLS) CA and Server Cert Generation
#!/bin/bash
WEBHOOK_YAML_FILE="./path_to_webhook_yaml"
WEBHOOK_KEY="TODO_SET_CA_BUNDLE"
echo "Generating base64 encoded myCA.pem and setting in webhook YAML"
# Check $WEBHOOK_YAML_FILE file exists
if [ ! -e $WEBHOOK_YAML_FILE ]; then
echo "Unable to find ${WEBHOOK_YAML_FILE} file, has it been moved or renamed?"
@michaeltelford
michaeltelford / fizz_buzz.cr
Created December 20, 2022 10:50
FizzBuzz solution written in Crystal
def multiple_of?(multiple : Int32, i : Int32) : Bool
(i % multiple) == 0
end
def get_fizz_buzz_str(i : Int32) : String
if multiple_of?(3, i) && multiple_of?(5, i)
"FizzBuzz"
elsif multiple_of?(3, i)
"Fizz"
elsif multiple_of?(5, i)
@michaeltelford
michaeltelford / main.go
Created September 30, 2022 12:39
Goroutine with timeout (using a context and channel)
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx := context.Background()
@michaeltelford
michaeltelford / extract.rb
Last active March 21, 2024 13:33
Ruby script using Wgit to extract the meaningful content from a webpage (without the crap e.g. cookie banners)
require "wgit"
# Remove the default extractors since we won't be using them.
Wgit::Document.remove_extractors
# The default name of the output file containing the clean HTML.
def default_file_name
"webpage.html"
end
@michaeltelford
michaeltelford / script.sh
Last active July 11, 2022 14:02
Example bash script to awk and loop over STDOUT
#!/bin/bash
#
# Example bash script to capture, awk (filter) and loop over STDOUT.
#
key="some_key"
new_value="some_value"
echo "Updating crons to use new env key/value: $key=$new_value"
echo ""
@michaeltelford
michaeltelford / fib.rb
Created May 16, 2022 16:07
Fibonacci sequence algorithm solution
# Use like: ruby fib.rb 5
# Returns the Fibonacci numbers up to n
# e.g. fib(5) => [0, 1, 1, 2, 3, 5]
def fib(n, nums = nil)
nums = [0, 1] unless nums
raise "nums is invalid" unless nums.is_a?(Enumerable) && nums.any?
next_number = nums[-1] + nums[-2]
return nums if next_number > n
@michaeltelford
michaeltelford / main.go
Last active December 9, 2021 11:56
Golang html/template example using `block/define`
package main
import (
"bytes"
"fmt"
"html/template"
"log"
)
var templates *template.Template
@michaeltelford
michaeltelford / Guardfile
Created December 6, 2021 13:38
Guardfile Ruby example for executing a command on file changes
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
@michaeltelford
michaeltelford / benchmark.rb
Last active October 28, 2021 15:09
Ruby benchmark script for hitting a URL several times and recording the time taken
require 'net/http'
require 'benchmark'
def get_url(url, n)
n.times { Net::HTTP.get_response(URI(url)) }
end
def bench(url, n)
puts "Benchmark: #{n}x GET #{url}"
puts Benchmark.measure { get_url url, n }
@michaeltelford
michaeltelford / README.md
Created June 29, 2021 13:35
React - Trace the cause of render

A given component will re-render if its (or its parents) props or state changes. It can be useful to know when and why this render occurs. Put this componentDidUpdate in your component to log the changes that caused the render.

Note that if the values from and to are the same in the logs, then it's probably because a new object is being generated with the same value, causing unnecessary re-rendering, this should be fixed.