Skip to content

Instantly share code, notes, and snippets.

@ktheory
ktheory / rack-attack.rb
Created July 25, 2012 14:31
rack-attack DSL
# NB: `req` is a Rack::Request object (basically an env hash with friendly accessor methods)
# Throttle 10 requests/ip/second
# NB: return value of block is key name for counter
# falsy values bypass throttling
Rack::Attack.throttle("req/ip", :limit => 10, :period => 1.second) { |req| req.ip }
# Block cloud IPs from accessing PATH regexp
Rack::Attack.block "bad_ips from logging in" do |req|
@ktheory
ktheory / bsearch.gemspec
Created July 31, 2012 19:48
Binary search gem
# -*- encoding: utf-8 -*-
$:.unshift '.'
require 'bsearch'
Gem::Specification.new do |s|
s.name = 'bsearch-ruby'
s.version = BSearch::VERSION
s.authors = ["Aaron Suggs"]
s.description = "A binary search implementation in ruby"
@ktheory
ktheory / chef_workflow.md
Created December 14, 2012 19:14
Chef workflow

Cookbook workflow

Prereqs:

Install chef gem (for knife command):

gem install chef

Create a new cookbook:

knife cookbook create my_cookbook

@ktheory
ktheory / test-status.rb
Created February 27, 2013 22:28
script to report Github commit status with CircleCi
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'octokit'
require 'yaml'
commit = ARGV.first || 'HEAD'
full_sha = `git rev-parse --verify #{commit}`.strip
@ktheory
ktheory / Gemfile
Last active December 14, 2015 12:38
Rack::Attack heroku demo w/ Redis
source 'https://rubygems.org'
gem 'rack-attack', :ref => '1c01e6097ce18f486d887ebbeb9f0c4b434cd8f5', :git => 'https://github.com/kickstarter/rack-attack.git'
gem 'redis-activesupport'
@ktheory
ktheory / thor_run_or_die.rb
Created June 4, 2013 21:52
Monkeypatch for Thor `run_or_die` action
require 'thor'
module Thor
class CommandFailedError < Error; end
module Actions
# runs command, raises CommandFailedError unless exit status is 0.
def run_or_die(command, config={})
result = run(command, config)
if behavior == :invoke && $?.exitstatus != 0
message = "#{command} failed with %s" % ($?.exitstatus ? "exit status #{$?.exitstatus}" : "no exit status (likely force killed)")
@ktheory
ktheory / fail2ban.rb
Last active December 18, 2015 04:09
# First blacklist checks for /etc/password, and counts hits in cache
blacklist 'etc/password' do
if req.query_string =~ %r{/etc/passwd}
Fail2Ban.fail('etc_password', req.ip, limit: 3, period: 24.hours, ban_for: 24.hours)
end
end
# 2nd blacklist checks for banned IPs in cache
blacklist 'banned_ips' do
Fail2Ban.banned?(req.ip)
#!/usr/bin/env ruby
##
# Restore simplenote backup
#
# If you want to restore your Simplenote for Mac database, you can use this script.
# I created it for this issue:
# http://help.simplenote.com/customer/en/portal/questions/2773965-most-of-my-notes-were-deleted?new=2773965
#
# First, find a good backup of your simplenote database stored in

Rails Asset Pipeline + S3

The asset pipeline changed a bit in Rails4, and I've never been super happy w/ the egads approach to tar'ing assets.

So here's my idea for a better approach.

Features:

  • Assets are only built once per SHA. (Deploys are fast.)
  • Old assets remain accessible on S3 (say, for emails)
  • Minimal compiling/uploading/downloading locally and on deployed servers. (FAAAASSSSTTT!)
FROM ubuntu:14.04
MAINTAINER aaron@ktheory.com
RUN apt-get update
RUN apt-get install -y build-essential libssl-dev curl
# Install ruby-build
RUN export ruby_build_release=20140420 && \