Skip to content

Instantly share code, notes, and snippets.

View sorentwo's full-sized avatar
🏡

Parker Selbert sorentwo

🏡
View GitHub Profile
@sorentwo
sorentwo / slack.cap
Last active August 29, 2015 13:56
Slack deploy notification task (adapted from capistrano-slack)
require 'json'
namespace :slack do
def post_to_slack(payload)
slack_subdomain = fetch(:slack_subdomain)
slack_token = fetch(:slack_token)
uri = URI.parse("https://#{slack_subdomain}.slack.com/services/hooks/incoming-webhook?token=#{slack_token}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
@sorentwo
sorentwo / db_backup.sh
Created February 28, 2014 20:01
Micro pg_dump backup script
#!/usr/bin/env bash
backup_dir=/var/backups
database=dscout_production
mkdir -p "$backup_dir" || exit 1
filename="$database-$(date +%Y-%m-%d).dump"
echo "Backing up $database ..."
pg_dump -Fc -f "$backup_dir/$filename" $database
@sorentwo
sorentwo / remove_dup.sql
Created March 1, 2014 22:06
Duplicate Removal
DELETE FROM tablename WHERE id NOT IN (SELECT MAX(dup.id) FROM tablename AS dup GROUP BY dup.field1, dup.field2);
@sorentwo
sorentwo / perforated_performance.txt
Created March 7, 2014 03:51
Comparison of different perforated cache backends.
The first pass writes and the second pass reads.
10_000 Objects:
user system total real
memory-1 1.240000 0.010000 1.250000 ( 1.252629)
memory-2 0.110000 0.000000 0.110000 ( 0.114169)
redis-1 1.930000 0.230000 2.160000 ( 2.300067)
redis-2 0.210000 0.010000 0.220000 ( 0.222959)
dalli-1 1.470000 0.090000 1.560000 ( 1.574031)
@sorentwo
sorentwo / curr_prev_next.sql
Created March 7, 2014 22:08
CTE / Window query for efficient current, prev, next id row retrieval
WITH linked AS (
SELECT id,
lead(id) OVER(ORDER BY id DESC) AS next_id,
lag(id) OVER(ORDER BY id ASC) AS prev_id
FROM snippets
WHERE snippets.mission_id = $1
)
SELECT snippets.* FROM snippets, linked WHERE linked.id = $2 AND snippets.id = linked.next_id;
@sorentwo
sorentwo / cors_headers.rb
Created August 29, 2014 17:18
Micro CORS
class CORSHeaders
ACCESS_CONTROL_HEADERS = {
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'OPTIONS,GET,PATCH,PUT,POST,DELETE',
'Access-Control-Expose-Headers' => '',
'Access-Control-Max-Age' => '1728000',
'Content-Type' => 'text/plain'
}.freeze
def initialize(app)
@sorentwo
sorentwo / marshalling-results.txt
Last active August 29, 2015 14:11
Marshalling Benchmarks
Calculating -------------------------------------
oj:hash:dump 1.047k i/100ms
json:hash:dump 833.000 i/100ms
ruby:hash:dump 719.000 i/100ms
-------------------------------------------------
oj:hash:dump 10.622k (± 6.5%) i/s - 53.397k
json:hash:dump 8.478k (± 5.0%) i/s - 42.483k
ruby:hash:dump 7.376k (± 6.7%) i/s - 37.388k
Comparison:
@sorentwo
sorentwo / Model.js
Created December 31, 2014 21:13
Model.js
var extend = require('./utils/extend');
var merge = require('./utils/merge');
var mixin = require('./utils/mixin');
var EventEmitter = require('events').EventEmitter;
var Model = function(attributes, options) {
mixin(this);
options = options || {};
@sorentwo
sorentwo / missing_cookies.txt
Created February 14, 2015 16:23
Cookies missing from view
# Form
<input type="hidden" name="_csrf_token" value="<%= @conn.req_cookies["_csrf_token"] %>"/>
<input type="hidden" name="_csrf_token" value="<%= csrf_token(@conn) %>"/>
# First input has the csrf_token value, second input value is blank
# Inside view module, IO.puts prints an empty map %{}
def csrf_token(conn) do
IO.puts inspect(conn.resp_cookies)
conn.resp_cookies["_csrf_token"]
### Keybase proof
I hereby claim:
* I am sorentwo on github.
* I am sorentwo (https://keybase.io/sorentwo) on keybase.
* I have a public key whose fingerprint is 9A47 77D5 4D4D 16A1 AAB1 A62D AE4E 59A6 0C64 50AA
To claim this, I am signing this object: