Sha256: 4156372dbc9757ca17fe963ad3f9f39644f780c92562b899dba4162e2d486649

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'sinatra/base'
require 'split'
require 'bigdecimal'

module Split
  class Dashboard < Sinatra::Base
    dir = File.dirname(File.expand_path(__FILE__))

    set :views,  "#{dir}/dashboard/views"
    set :public, "#{dir}/dashboard/public"
    set :static, true

    helpers do
      def url(*path_parts)
        [ path_prefix, path_parts ].join("/").squeeze('/')
      end

      def path_prefix
        request.env['SCRIPT_NAME']
      end

      def number_to_percentage(number, precision = 2)
        round(number * 100)
      end

      def round(number, precision = 2)
        BigDecimal.new(number.to_s).round(precision).to_f
      end
    end

    get '/' do
      @experiments = Split::Experiment.all
      erb :index
    end

    post '/:experiment' do
      @experiment = Split::Experiment.find(params[:experiment])
      @alternative = Split::Alternative.find(params[:alternative], params[:experiment])
      @experiment.winner = @alternative.name
      redirect url('/')
    end

    post '/reset/:experiment' do
      @experiment = Split::Experiment.find(params[:experiment])
      @experiment.reset
      redirect url('/')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
split-0.2.2 lib/split/dashboard.rb