Skip to content

Instantly share code, notes, and snippets.

peterkeen /
Last active Aug 29, 2015
Email courses as state machines.

This is a first draft of an idea I had in the shower this morning.

Email courses (aka sequences of email autoresponders) can be trivially modeled as finite state machines. Each state is the last email they were sent (or queued to receive) and each transition queues an email to be sent.

This example implementation builds on top of AASM for the state machine and ActsAsTaggableOn to keep track of states. In a production-ready implementation, likely the state machine implementation would be wrapped in a DSL that streamlines the process a little more.

View gist.rb
if Rails.env.production?
resp = HTTParty.get(@sale.product.file.url)
filename = @sale.product.file.url
send_data resp.body,
:filename => File.basename(filename),
:content_type => resp.headers['Content-Type']
filename = File.expand_path(Sale.last.product.file_file_name, Rails.root)
peterkeen / gist:79a9d7b9f068f8a199de
Created Jan 30, 2015
A silly torturous PG plan
View gist:79a9d7b9f068f8a199de
Append (cost=1124.07..1134.09 rows=201 width=136) (actual time=13.030..13.052 rows=13 loops=1)
CTE months
-> Function Scan on generate_series x (cost=0.00..25.00 rows=1000 width=4) (actual time=0.042..0.064 rows=12 loops=1)
CTE monthly_income
-> HashAggregate (cost=293.73..293.74 rows=1 width=42) (actual time=12.623..12.625 rows=10 loops=1)
-> Seq Scan on ledger (cost=0.00..293.52 rows=12 width=42) (actual time=10.223..12.497 rows=27 loops=1)
Filter: ((account ~ 'Income:Consulting'::text) AND ((jtags ->> 'Client'::text) IS NOT NULL))
Rows Removed by Filter: 2741
View gist:73663

Cable management in the living room

Attach two cable hooks to the back of each table leg. Run all cables through these, signal on one side and power on the other.

Get an end table for next to the couch and give it the same treatment as the table, except for the cable modem, router, mac and drive. Get a UPS for the mac so it doesn't keep dying in the middle of the day.

View example calories file
# command-line calorie tracker
# - everything is a recipe
# - recipes can contain other recipes
# - resolve recipes as far as possible
# - if a recipe doesn't resolve, report it as the given name
# - unit conversions via recipes
# - each dated entry has one or more recipes
# recipe: count unit label(?) equivalent(?)
# equivalent: '=' recipe(s /,/)
#!/usr/bin/env perl
use strict;
use warnings;
use Try::Tiny;
use IO::File;
use JSON::XS qw/ decode_json /;
use File::Slurp qw/ read_file /;
View gist:605504
#!/usr/local/bin/perl58 -w
use strict;
my @replacements = qw/
View gist:605519
# <-Foo> => random singular element from @Foo, hyphenated
# <^Foo> => random singular element from @Foo, capitalized
# <&Foo> => random singular element from @Foo with the first word capitalized
# <.Foo> => random singular element from @Foo, with the first word lowercased
# <!Foo> => random singular element from @Foo, ALLCAPS
# <*Foo> => random singular element from @Foo with articles stripped
# <,Foo> => random singular element from @Foo with spaces replaced with underscores
# <+Foo> => random singular element from @Foo with spaces replaced with plusses
View gist:705447
select (
date_trunc('day', date_page_requested)
page_request_no = x.page_request_no
) as day,