Sha256: 6491e91808dd786effeb92778af1e2da3e67e037fd8b3a6c6eaaea65bede32cb

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'sinatra'
require 'dm-core'

module Shorty
  class Core < Sinatra::Base
    attr_accessor :database_url
    
    def initialize(&block)
      yield self
      DataMapper.setup(:default, database_url)
      super
    end
    
    helpers do
      def url(path)
        "http://#{host_name}/#{path}"
      end
      
      def host_name
        env['HTTP_HOST']
      end
    end
    
    # If you PUT then you are defining the path!
    put '/:path' do
      halt [ 409, "#{url(params[:path])} has been taken" ] if Url.get(params[:path])
      @url = Url.new(:url => request.body.read.chomp, :path => params[:path])
      shorten_url
    end

    # If you post, we'll pick the path for you! This is here for curlability.
    post '/' do
      @url = Url.new(:url => request.body.read.chomp, :path => Url.random_path)
      shorten_url
    end

    get '/path/random' do
      Url.random_path
    end

    get '/:path' do
      if @url = Url.get(params[:path])
        redirect @url.url
      else
        halt 404, "Eh?"
      end
    end

  protected
    # Tries to save the url
    def shorten_url
      if @url.save
        headers 'Location' => url(@url.path)
        halt 201, "Created #{url(@url.path)}"
      else
        halt 406, "#{@url.errors.full_messages.join(".\n")}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shorty_server-0.5.0 lib/shorty/core.rb