Skip to content

Instantly share code, notes, and snippets.

View jamiehodge's full-sized avatar

Jamie Hodge jamiehodge

  • Zendesk
  • Copenhagen, Denmark
View GitHub Profile
@jamiehodge
jamiehodge / sinatra.rb
Last active December 16, 2015 06:19
What am I?
# What am I? A controller? A router?
class Foo < Sinatra::Base
set :bar, Baz
def self.both
# aren't a composite object in denial?
get '/glee' do
'happy!'
@jamiehodge
jamiehodge / sequel_dot.rb
Created May 6, 2013 07:31
sequel DB diagrams
#! /usr/bin/env ruby
# Usage:
# ruby sequel_dot.rb [SEQUEL-DATABASE-URI] > output.dot
# Or pipe directly to Graphviz:
# ruby sequel_dot.rb [SEQUEL-DATABASE-URI] | dot -Tgif > output.gif
#
# Note adapted from Jeremy Evans' and Rohit Namjoshi's son's code at
# http://sequel.heroku.com/2010/05/29/fun-with-graphviz-and-associations/
#
@jamiehodge
jamiehodge / upload.md
Last active December 17, 2015 14:09
Upload protocols

Server ideals

  • stateless
  • tell, don't ask

Client ideals

  • graceful degredation (should be possible to manually split and submit files via forms)
  • a minimum of logic
  • chunk files
@jamiehodge
jamiehodge / upload.md
Last active December 17, 2015 14:39
Upload protocol proposal

Request:

GET /
Host: tus.example.org

Response:

HTTP/1.1 200 OK

...

Request:

GET /
Host: tus.example.org

Response:

HTTP/1.1 200 OK

...

@jamiehodge
jamiehodge / gist:5642713
Created May 24, 2013 10:56
parse url query string
params = (url) ->
plus = /\+/g
search = /([^&=]+)=?([^&]*)/g
decode = (s) -> decodeURIComponent s.replace(plus, ' ')
index = url.indexOf '?'
if index isnt -1 then hash = url.substr index + 1 else hash = ''
result = {}
while match = search.exec(hash)
@jamiehodge
jamiehodge / gist:5644074
Last active December 17, 2015 17:09
Explicit chunk upload proposal

POST /uploads

  • complete: default false
  • chunk_size: default 1024*x

-> /uploads/id

  • form PUT uploads/id/chunks/id
  • list /uploads/id/chunks/id

PUT (repeat) GET to see what is missing

@jamiehodge
jamiehodge / resource.rb
Last active November 28, 2017 23:23
Server Side Events, Sequel and Postgres LISTEN/NOTIFY
require 'sequel'
DB ||= Sequel.connect ENV['DATABASE_URL']
class Resource < Sequel::Model
include Sequel.inflections
def after_save
db.notify channel
end
@jamiehodge
jamiehodge / migration.rb
Created May 30, 2013 20:24
Notify function and trigger
create_function(:notify, <<-SQL, language: :plpgsql, returns: :trigger, replace: true)
BEGIN
IF (TG_OP = 'DELETE') THEN
PERFORM pg_notify(TG_TABLE_NAME, '{ \"id\": \"' || OLD.id || '\", \"event\": \"' || TG_OP || '\" }');
RETURN OLD;
ELSE
PERFORM pg_notify(TG_TABLE_NAME, '{ \"id\": \"' || NEW.id || '\", \"event\": \"' || TG_OP || '\" }');
RETURN NEW;
END IF;
END;