app/controllers/shortener/shortened_urls_controller.rb
# find the real link for the shortened link key and redirect
find the real link for the shortened link key and redirect
find the real link for the shortened link key and redirect
def show
    token = /^([#{Shortener.key_chars.join}]*).*/.match(params[:id])[1]
    sl = ::Shortener::ShortenedUrl.find_by_unique_key(token)
    if sl
      Thread.new do
        sl.increment!(:use_count)
        ActiveRecord::Base.connection.close
      end
      redirect_to sl.url, :status => :moved_permanently
    else
      redirect_to '/'
    end
  end
def show token = / ^([ #{ Shortener . key_chars . join } ]*).* / . match ( params [ :id ] ) [ 1 ] sl = :: Shortener :: ShortenedUrl . find_by_unique_key ( token ) if sl Thread . new do sl . increment! ( :use_count ) ActiveRecord :: Base . connection . close end redirect_to sl . url , :status => :moved_permanently else redirect_to '/' end end
find the real link for the shortened link key and redirect
find the real link for the shortened link key and redirect
# only use the leading valid characters # pull the link out of the db # don't want to wait for the increment to happen, make it snappy! # this is the place to enhance the metrics captured # for the system. You could log the request origin # browser type, ip address etc. # do a 301 redirect to the destination url # if we don't find the shortened link, redirect to the root # make this configurable in future versions