Sha256: 2b0f193fb77ad730672bf87a2cbf8f5c7df5ef009f26605ce5d89f4c56ab9344

Contents?: true

Size: 988 Bytes

Versions: 4

Compression:

Stored size: 988 Bytes

Contents

class Shortener::ShortenedUrlsController < ActionController::Base

  # find the real link for the shortened link key and redirect
  def show
    # only use the leading valid characters
    token = /^([#{Shortener.key_chars.join}]*).*/.match(params[:id])[1]

    # pull the link out of the db
    sl = ::Shortener::ShortenedUrl.find_by_unique_key(token)

    if sl
      # 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.
      Thread.new do
        sl.increment!(:use_count)
        ActiveRecord::Base.connection.close
      end
      # do a 301 redirect to the destination url
      redirect_to sl.url, status: :moved_permanently
    else
      # if we don't find the shortened link, redirect to the root
      # make this configurable in future versions
      redirect_to Shortener.default_redirect
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shortener-0.4.1 app/controllers/shortener/shortened_urls_controller.rb
shortener-0.5.1 app/controllers/shortener/shortened_urls_controller.rb
shortener-0.5.0 app/controllers/shortener/shortened_urls_controller.rb
shortener-0.4.0 app/controllers/shortener/shortened_urls_controller.rb