Points
- urlをDBのidに置き換え、さらにInteger#to_i(36)を使って短縮する
- cssをw3cサイトから借用
- 環境変数envから参照元つまり自サイトのurlを取得
%w(rubygems sinatra dm-core dm-timestamps dm-migrations uri).each { |lib| require lib }
configure :development do
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/chop.db")
end
get '/' do
haml :index
end
post '/' do
uri = URI::parse(params[:original])
raise "Invalid URL" unless [URI::HTTP, URI::HTTPS].any? { |scm| uri.kind_of? scm }
@url = Url.first_or_create(:original => uri.to_s)
haml :index
end
get '/:chopped' do |chopped|
redirect Url.first(:id => chopped.to_i(36)).original
end
error do
'Sorry there was a nasty error - ' + env['sinatra.error'].name
end
class Url
include DataMapper::Resource
property :id, Serial
property :original, String, :length => 255
property :created_at, DateTime
def chopped
self.id.to_s(36)
end
end
__END__
@@ layout
!!! 1.1
%html
%head
%title Chop!
%link{:rel => 'stylesheet', :href => 'http://www.w3.org/StyleSheets/Core/Modernist', :type => 'text/css'}
%body
= yield
@@ index
%h1.title Chop!
- unless @url.nil?
%code= @url.original
chopped to
%a{:href => env['HTTP_REFERER'] + @url.chopped}
= env['HTTP_REFERER'] + @url.chopped
%form{:method => 'post', :action => '/'}
Chop this:
%input{:type => 'text', :name => 'original', :size => '50'}
%input{:type => 'submit', :value => 'chop!'}
%small copyright ©
No comments:
Post a Comment