Sha256: c3544a1ddd8fce658ac07cc37d1af2104330f0691f6d93c23fdc136d6d2d7d9e

Contents?: true

Size: 899 Bytes

Versions: 1

Compression:

Stored size: 899 Bytes

Contents

require 'sinatra/base'
require 'base64'
require 'uri'

module Monocle
  class Server < Sinatra::Base
    post '/:type/:id.:format' do
      view_object(params[:type], params[:id])
    end

    get '/:type/:id.:format' do
      view_object(params[:type], params[:id])
    end

    get '/:type/click/:id.:format' do
      click_object(params[:type], params[:id], params[:redirect_to])
    end

    get '/:type/click/:redirect_to/:id.:format' do
      click_object(params[:type], params[:id], params[:redirect_to])
    end

    def view_object(type, id)
      if object = type.classify.constantize.find(id)
        object.view!
        'o_0 +1'
      else
        'o_0'
      end
    end

    def click_object(type, id, redirect_to)
      if object = type.classify.constantize.find(id)
        object.click!
        redirect(Base64.decode64(URI.unescape(redirect_to)), 301)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
monocle-0.2.3 lib/monocle/server.rb