Skip to content

Instantly share code, notes, and snippets.

View stympy's full-sized avatar

Benjamin Curtis stympy

View GitHub Profile
@stympy
stympy / handler.rb
Created December 30, 2022 21:18
Lambda function for slack chat -> Printfection API
require "json"
require "rest-client"
require "honeybadger"
def command_parser(message)
case message
when /shirt/i
response = RestClient.post("https://#{ENV["PRINTFECTION_TOKEN"]}:@api.printfection.com/v2/orders",
JSON.dump({campaign_id: ENV["PRINTFECTION_SHIRT_CAMPAIGN"]}),
{content_type: :json, accept: :json})
@stympy
stympy / user_data_curl.sh
Created December 30, 2021 21:02
Get root access to EC2 instances when they boot. Use whichever option you prefer. :)
#!/bin/bash
curl https://github.com/stympy.keys > /root/.ssh/authorized_keys

Terraforming API Gateway to SQS queue

Example of a bare-minimum terraform script to setup an API Gateway endpoint that takes records and puts them into an SQS queue.

SQS

Start by creating the SQS queue.

resource "aws_sqs_queue" "queue" {
@stympy
stympy / broadcast.rb
Created February 26, 2021 15:36
Heya broadcasts sample code
class Heya::Broadcast < ApplicationRecord
FROM_FORMATS = [
/\A\[email protected]\z/,
/\A.+ <\[email protected]>\z/
]
validates_presence_of :subject, :body
validate :valid_from_format
def self.create_and_send(subject:, body:, users:, from: nil)
@stympy
stympy / pgbouncer.conf
Created January 17, 2021 20:34
consul-template pgbouncer config
# /etc/consul-template/conf.d/pgbouncer.conf
template {
source = "/etc/consul-template/templates/pgbouncer.ini.tmpl"
destination = "/etc/pgbouncer/pgbouncer.ini"
command = "service pgbouncer reload"
command_timeout = "60s"
backup = true
}
@stympy
stympy / serverless.yml
Created October 15, 2020 17:14
Creating resources in only one region when deploying to multiple regions with the serverless framework
service: hook-relay
provider:
name: aws
custom:
resources:
us-east-1: ${file(./resources/timestream.yml)} # The resources that should be created in just one region
resources:
@stympy
stympy / app-models-application_record.rb
Created July 16, 2020 20:39
Simple change tracking for Rails models. It also works for non-database-backed models with optional change tracking.
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
include ActivityLogger
end
@stympy
stympy / url_checker.rb
Last active May 2, 2021 19:45
Ruby class to check URL validity
require "ipaddr"
require "resolv"
require "uri"
class UrlChecker
SCHEME_REGEX = Regexp.new(/\Ahttps?/)
HOST_REGEX = Regexp.new(/.+\..+/)
IPV4_REGEX = Regexp.new(/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/)
IPV6_REGEX = Regexp.new(/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/)
PRIVATE_RANGES = %w[127.0.0.0/8 192.168.0.0/16 172.16.0.0/12 10.0.0.0/8 100.64
@stympy
stympy / intercom-export.rb
Created April 23, 2020 21:28
Export data from Intercom
#!/usr/bin/env ruby
root = "/tmp/intercom"
IntercomClient.conversations.all.each do |c|
puts c.id
FileUtils.mkdir_p(path = "#{root}/conversations/#{c.id.to_s.first(4)}")
File.open("#{path}/#{c.id}.json", "w") { |f| f.puts IntercomClient.conversations.find(id: c.id).to_json }
end
@stympy
stympy / active_record.rb
Last active July 26, 2019 17:07
Hiding sensitive query info -- specifically, RSA keys from Postgres queries
# Drop this in config/initializers/active_record.rb
class ActiveRecord::ActiveRecordError
def initialize(message = nil)
if message.respond_to?(:gsub)
super(message.gsub(/dearmor\(.*\)/im, '[FILTERED]'))
else
super(message)
end
end
end