Skip to content

Instantly share code, notes, and snippets.

View troyk's full-sized avatar

Troy Kruthoff troyk

View GitHub Profile
@troyk
troyk / gist:896c045094ec93c1578b
Created May 29, 2014 17:38
idea to avoid using pointers in go structs just for happy patching
type Repository struct {
Name string `json:"name"`
Description string `json:"description"`
Private bool `json:"private"`
}
// response to blog post https://willnorris.com/2014/05/go-rest-apis-and-pointers
type RepositoryPatch struct {
Repository
@troyk
troyk / awesome.sql
Last active August 29, 2015 14:09
Don't try this with MongoDB Folks
with v(dfrom, dto) as (
VALUES('2014-01-01'::date, '2014-11-12'::date)
--VALUES(_from, _to)
), failures as (
select user_id, 'data-entry'::citext as action, count(*) as "count" from ad_histories, v
where action='qc-fail' and created_at>=v.dfrom and created_at<=v.dto
and exists(select 1 from ad_histories subq where ad_id=subq.ad_id and subq.action='data-entry' limit 1) group by user_id
), elapsed as (
select r.user_id,r.action,avg(r.elapsed) as "avg_time" from (
select user_id,action, lead(created_at,1,now()) over w - created_at as elapsed
# Install dependencies
#
# * checkinstall: package the .deb
# * libpcre3, libpcre3-dev: required for HTTP rewrite module
# * zlib1g zlib1g-dbg zlib1g-dev: required for HTTP gzip module
apt-get install checkinstall libpcre3 libpcre3-dev zlib1g zlib1g-dbg zlib1g-dev && \
mkdir -p ~/sources/ && \
# Compile against OpenSSL to enable NPN
@troyk
troyk / stringer.ex
Created February 5, 2015 10:39
This aint so bad is it??
defmodule Rocket.Stringer do
def sanitize_phone(ph) do
ph = Regex.replace(~r/[^0-9]/,ph,"")
case Regex.run(~r/\d{5,10}$/,ph) do
[phone] -> phone
_ -> nil
end
end
@troyk
troyk / activerecord
Created February 27, 2015 23:59
AR vs Sequel in real world rails scenario
Summary:
Total: 13.0664 secs.
Slowest: 0.3004 secs.
Fastest: 0.0760 secs.
Average: 0.1303 secs.
Requests/sec: 76.5321
Status code distribution:
[200] 1000 responses
@troyk
troyk / cmd
Created April 22, 2015 21:25
how to compile bootstrap
lessc bootstrap.less --clean-css="--s1 --advanced --compatibility=ie8" > ~/Projects/textrocket/public/css/lib/
# BruteForceKilla
#
# A Rack middleware to limit requests by ip address, coded for fun as my first
# middleware, thanks http://coderack.org for giving me a reason :)
#
# For production use, one would want to make a memcache or redis tracker.
#
# options:
#
# :tracker => Class name of the tracker to use (default Memory (all there is for now!))
Cursor.prototype.streamRecords = function(callback) {
var
self = this,
stream = new process.EventEmitter(),
recordLimitValue = this.limitValue || 0,
emittedRecordCount = 0,
queryCommand = this.generateQueryCommand();
// see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
queryCommand.numberToReturn = 500;
Last login: Fri Aug 13 17:17:35 on ttys003
troy:~ $ history | awk '{print $2}' | sort | uniq -c | sort -rn | head
173 node
89 git
36 cd
25 ls
20 ./script/server
15 sshebara
14 sc
12 ssh
@troyk
troyk / PostgreSQLAdapter.rb
Created November 3, 2010 07:10
go go multi-tenant via schema
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def ensure_search_path!
return true if existing_path = Thread.current[:search_path] and @schema_search_path == Thread.current[:search_path]
if existing_path
@schema_search_path = Thread.current[:search_path] = existing_path
else
puts "WARNING::SETTING SEARCH_PATH TO SUB1,PUBLIC CUZ Thread.current[:search_path] is nil"
@schema_search_path = Thread.current[:search_path]='sub1,public'
end