Skip to content

Instantly share code, notes, and snippets.

View peterc's full-sized avatar
🏠
Working from home

Peter Cooper peterc

🏠
Working from home
View GitHub Profile
@peterc
peterc / s4d.rb
Created July 13, 2021 14:20
Convert abbreviated text like i18n and l11n to full text
text = ARGV.first || "y1u a5t p1l? j2t w4d to s1y hi"
times = (ARGV[1] || 1).to_i
words = File.readlines('/usr/share/dict/words')
.map(&:downcase)
.map(&:chomp)
times.times do
puts text.gsub(/\w+/) { |word|
words.select { |realword|

Ten seconds to ponder if a thread is worth it

recording

A userstyle that makes you wait ten seconds before entering a Hacker News thread. I use stylus to manage mine.

.subtext {
  display: inline-block;
 background: linear-gradient(to left, transparent 50%, #f60 50%) right;
@peterc
peterc / aoc13-part1.rb
Last active February 3, 2021 15:38
Advent of Code 2020 Day 13 in Ruby annotated
$<.to_a.tap {|e,rs|p rs.split(',').map(&:to_i).reject(&:zero?).map{|r|[r,r-(e.to_i%r)]}.min_by(&:last).inject(:*)}
@peterc
peterc / dnsimple_ssl_to_linode_object_storage.rb
Created December 12, 2020 01:52
Copy a TLS certificate and private key from DNSimple to Linode Object Storage
# dnsimple_ssl_to_linode_object_storage.rb
# Copy a certificate stored on DNSimple to Linode Object Storage
#
# Depends on 'http' gem
#
# Requires environment settings as such:
# LINODE_TOKEN=[API token]
# LINODE_REGION=[region of your object storage bucket]
# DNSIMPLE_TOKEN=[API token]
# DNSIMPLE_DOMAIN=example.com
@peterc
peterc / aoc9.rb
Created December 9, 2020 11:22
Advent of Code 2020 Day 9 in Ruby
nums = $<.readlines.map(&:to_i)
w = 25
w.upto(nums.length - 1) do |i|
p nums[i] if nums[i-w, w].combination(2)
.map(&:sum)
.all? { |x| x != nums[i] }
end
w.times do |cw|
@peterc
peterc / github_rate.rb
Created December 5, 2020 16:23
See GitHub API rate limit information in a useful way
# Return GitHub API rate information in JSON format
# GitHub Personal Access Token expected in ~/.githubtoken
# I'd pipe this into jq for nicer viewing
require 'http'
require 'json'
TOKEN = File.read(ENV['HOME'] + "/.githubtoken").strip
res = HTTP["Authorization" => "token #{TOKEN}"].get("https://api.github.com/rate_limit")
@peterc
peterc / pg.txt
Created November 19, 2020 23:43
Ruby pg (Postgres) library notes
# gem 'pg'
# https://github.com/ged/ruby-pg
# http://deveiate.org/code/pg
#
# I always find it a nightmare to look up how it works because
# it's not intuitive at all. So here are some notes.
# CONNECTION
# ==========
# No connection URL support, so..
@peterc
peterc / zapier.js
Created November 2, 2020 23:27
zapier function to take gmail input and determine inbox type
// a is the 'from name'
// b is the 'labels'
// you set these up in zapier after having gmail as a source
const slackURL = "https://hooks.slack.com/services/...";
let mb = inputData.b;
let folder = "Unknown";
if (mb.includes('CATEGORY_FORUMS')) {
@peterc
peterc / futbin-checker.js
Last active October 27, 2020 22:20
Check something on a remote site using AWS Lambda, Puppeteer, and SNS
// See https://github.com/shelfio/chrome-aws-lambda-layer for more info
// on the layer needed to make chrome-aws-lambda work
const AWS = require('aws-sdk');
const chromium = require('chrome-aws-lambda');
exports.handler = async (event, context) => {
let sns = new AWS.SNS();
let browser = await chromium.puppeteer.launch({
@peterc
peterc / subreply.css
Created July 13, 2020 15:48
Format subreply more nicely - use with an extension like Stylish
body {
font-family: "Menlo", Monaco, Consolas, monospace;
font-size: 16px;
line-height: 24px;
}
.container {
grid-template-columns: 2fr minmax(200px, 1fr) minmax(600px, 3fr) 3fr;
}