sausheong / snip

Simple TinyURL clone

This URL has Read+Write access

Sausheong Chang (author)
Fri May 22 08:39:02 -0700 2009
snip / snip.rb
100644 54 lines (45 sloc) 1.422 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
%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
 
get '/:snipped' do redirect Url[params[:snipped].to_i(36)].original end
 
error do haml :index end
 
use_in_file_templates!
 
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
 
__END__
 
@@ layout
!!! 1.1
%html
%head
%title Snip!
%link{:rel => 'stylesheet', :href => 'http://www.w3.org/StyleSheets/Core/Modernist', :type => 'text/css'}
= yield
 
@@ index
%h1.title Snip!
- unless @url.nil?
%code= @url.original
snipped to
%a{:href => env['HTTP_REFERER'] + @url.snipped}
= env['HTTP_REFERER'] + @url.snipped
#err.warning= env['sinatra.error']
%form{:method => 'post', :action => '/'}
Snip this:
%input{:type => 'text', :name => 'original', :size => '50'}
%input{:type => 'submit', :value => 'snip!'}
%small copyright ©
%a{:href => 'http://blog.saush.com'}
Chang Sau Sheong
%br
%a{:href => 'http://github.com/sausheong/snip'}
Full source code