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 / form-to-fetch.js
Last active December 15, 2020 21:11
Basic implementation of turning forms into an Ajax form using Fetch API
document.querySelectorAll("FORM[method='post']").forEach(form => {
form.onsubmit = function(e) {
e.preventDefault();
let formData = new FormData(e.target);
let output = e.target.querySelector('button');
fetch(e.target.action, { method: 'post', body: formData })
.then( res => {
console.log("Happy days");
})
.catch( err => {
@peterc
peterc / Gemfile
Created June 25, 2020 15:55 — forked from dhh/Gemfile
HEY's Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
@peterc
peterc / hn_new.py
Created June 15, 2020 22:42
Get a heads up when a new item hits the Hacker News front page
# Get a heads up when a new item hits the Hacker News front page
import requests
import sys
from time import sleep
from random import randint
def fetchItems():
res = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json")
assert(res.status_code == 200)
@peterc
peterc / rss-to-s3.rb
Created May 23, 2020 15:56
RSS to S3 Ruby Lambda Function
require 'json'
require 'aws-sdk-s3'
require 'open-uri'
# Ideally put these in environment variables
# but since this is just for us, who cares.
BUCKET = "RETRACTED"
REGION = "RETRACTED"
def do_newsletters
@peterc
peterc / pick_a_winner.rb
Created May 2, 2020 15:42
Given a list of names, pick a winner at the terminal in a visually appealing way
require 'curses'
include Curses
init_screen
curs_set(0)
users = %w{
put twitter handles of people who enter here
}
@peterc
peterc / cdown.rb
Last active April 29, 2020 16:24
Basic Countdown numbers solver in Ruby
# Basic Countdown numbers solver
# Principally ported from Jake Archibald's JS solution
# at https://jsbin.com/yimejin/4/edit?js,console
def solve(nums, target)
bs = nil
nums.sort.reverse.combination(2).each do |i, j|
%I{+ - * /}.each do |op|
next if op == :* && (i == 1 || j == 1)
next if op == :/ && (j == 1 || i % j != 0)
@peterc
peterc / zapier-gmail.js
Created April 10, 2020 12:59
Using Zapier to check emails on Gmail and then notify us on Slack
// Using Zapier to check emails on Gmail and then notify us on Slack
const slackURL = "https://hooks.slack.com/services/XXXXXXXXXXXXX";
let mb = inputData.b;
let folder = "Unknown";
if (mb.includes('CATEGORY_FORUMS')) {
folder = "Inbox (Forums)";
}
if (mb.includes('CATEGORY_PERSONAL')) {
@peterc
peterc / hn-title-checker.rb
Created November 24, 2019 17:52
Monitors Hacker News for changing titles
require 'aws-sdk'
require 'open-uri'
require 'json'
require 'pg'
require 'dotenv'
Dotenv.load
# .env to contain:
# POSTGRES_URL
@peterc
peterc / stallman.txt
Created September 19, 2019 12:54
Stallman's "info pack"
Here's the info packet about my speeches. This information is
essential for planning my visit and speech. Please forward
it to anyone who is interested in organizing a speech for me.
Please discuss with me what the topic of this speech should be.
We need to decide it together.
My talks are not technical. The topics of free software, copyright vs
@peterc
peterc / block-aws.rb
Created August 19, 2019 11:54
Create UFW rules to block AWS regions and services
require 'json'
require 'open-uri'
URL = "https://ip-ranges.amazonaws.com/ip-ranges.json"
REGION = "us-east"
SERVICE = "EC2"
j = JSON.parse(open(URL).read)
j['prefixes'].select { |k| k['region'].start_with?(REGION) && k['service'] == SERVICE }.each do |l|