Skip to content

Instantly share code, notes, and snippets.

%w(rubygems sinatra dm-core dm-timestamps uri).each { |lib| require lib}
get '/' do haml :index end
post '/' do
uri = URI::parse(params[:original])
raise "Invalid URL" unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS
@url = Url.first_or_create(:original => uri.to_s)
haml :index
end
DataMapper.setup(:default, ENV['DATABASE_URL'] || 'mysql://root:root@localhost/snip')
class Url
include DataMapper::Resource
property :id, Serial
property :original, String, :length => 255
property :created_at, DateTime
def snipped() self.id.to_s(36) end
end
JVBERi0xLjQKJdDUxdgKMyAwIG9iago8PCAvcGdmcHJnYiBbL1BhdHRlcm4g\nL0RldmljZVJHQl0gPj4KZW5kb2JqCjYgMCBvYmogPDwKL0xlbmd0aCAyNTUg\nICAgICAgCi9GaWx0ZXIgL0ZsYXRlRGVjb2RlCj4+CnN0cmVhbQp42oWRQUvD\nQBCF7/kVc0zAne7M7iSbg2BFq4hIkOCl9BDb2BajEdMK/ns3bioNCp72sfPN\nvDeMhjVouIr0P+95GU1mxgJlyCQWyifgVNCJA2GLmh2UK5jHRbIobyYzpmOS\nHGpiP+cbSQNC2TGiD9XTUB1beUmZPSB3g4kIEGEuwj2jrHfxUlmDzkhAHxKR\nePtafSaKRccXddtUzfa5eu8ngDIaMxZQZDC3wwbXRWDv6672HLl4uQk/t9Vj\ndxJkUTVtUNNm1w5x0lEcH5nHkz/6ILj6yXC2ecNl+/LXMn4HkztQ7NCmeWif\n7tf7bhdc0yEHa9J9/697XZbRF2mFbxYKZW5kc3RyZWFtCmVuZG9iago1IDAg\nb2JqIDw8Ci9UeXBlIC9QYWdlCi9Db250ZW50cyA2IDAgUgovUmVzb3VyY2Vz\nIDQgMCBSCi9NZWRpYUJveCBbMCAwIDU5NS4yNzYgODQxLjg5XQovUGFyZW50\nIDEyIDAgUgo+PiBlbmRvYmoKNCAwIG9iaiA8PAogL0NvbG9yU3BhY2UgMyAw\nIFIgL1BhdHRlcm4gMiAwIFIgL0V4dEdTdGF0ZSAxIDAgUiAKL0ZvbnQgPDwg\nL0YzNCA3IDAgUiAvRjIxIDggMCBSIC9GMTcgOSAwIFIgL0Y1NSAxMCAwIFIg\nL0Y1NiAxMSAwIFIgPj4KL1Byb2NTZXQgWyAvUERGIC9UZXh0IF0KPj4gZW5k\nb2JqCjE1IDAgb2JqIDw8Ci9MZW5ndGggMzM3NiAgICAgIAovRmlsdGVyIC9G\nbGF0ZURlY29kZQo+PgpzdHJlYW0KeNrN
call ruby on error
@sausheong
sausheong / unshredder.rb
Created November 15, 2011 05:50
For the Instagram Challenge - Unshredder
require 'rmagick'
include Magick
shredded = ImageList.new "TokyoPanoramaShredded.png" # the original source
ROWS = shredded.rows # number of lines of pixes
working = [] # a working array of images
def strip_width
32
end
@sausheong
sausheong / rtext.rb
Created November 16, 2011 08:52
Random text generator app. Missing the dictionary file as well as the index.html file.
require 'sinatra'
DICTIONARY = File.open("dictionary.txt", 'r').readlines
$books = {}
text_names = %w(holmes1 barsoom1 crusoe)
text_names.each do |name|
$books[name.to_sym] = File.open("#{name}.txt", 'r').readlines.join(" ").split.join(" ").split(". ")
end
@sausheong
sausheong / turkish_march.rb
Created February 25, 2012 06:01
'Turkish March' in Muse, the Ruby DSL for music
require '../muse'
# Mozart's Piano Sonata No. 11 3rd Movement (All Turca)
# more popularly known as the 'Turkish March' or 'Turkish Rondo'
# first 9 bars only
Song.record 'turkish_march' do
bar(1,b:0.25).notes {b4; a4; gis3; a4;}
bar(2,b:0.25).notes {c4 b:0.5; _ b:0.5; d4; c4; b4; c4;}
@sausheong
sausheong / circularlist.rb
Created April 29, 2012 05:09
Circular doubly-linked list
class CircularList < Array
def index
@index ||=0
@index.abs
end
def current
@index ||= 0
get_at(@index)
end
@sausheong
sausheong / gist:2533651
Created April 29, 2012 05:20
Using the circular list
$ irb -r ./circularlist.rb
1.9.3-p125 :001 > cl = CircularList.new %w(a b c d e f g h)
=> ["a", "b", "c", "d", "e", "f", "g", "h"]
1.9.3-p125 :002 > cl.current
=> "a"
1.9.3-p125 :004 > cl.next
=> "b"
1.9.3-p125 :005 > cl.previous
=> "a"
1.9.3-p125 :007 > cl.next 3
@sausheong
sausheong / otto.rb
Created June 29, 2013 13:07
Otto the Algorithmic Composer
# Muse
# Copyright (C) 2012 Chang Sau Sheong
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of