Class: Metry::Psycho

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/metry/psycho.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Psycho

Returns a new instance of Psycho.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/metry/psycho.rb', line 5

def initialize(app, options={})
  super(app)
  @options = options
  path = options[:path] || "/psycho"
  auth = options[:authorize] || proc{true}
  on_deny = options[:on_deny] || proc{[403, {"Content-Type" => "text/plain"}, "You must log in."]}
  
  self.class.class_eval do
    set :views, File.dirname(__FILE__) + '/psycho'
    
    before do
      if unescape(@request.path_info) =~ /^#{path}/
        @env["metry.event"].ignore!
        if !auth.call(env)
          reply = on_deny.call(env)
          status reply.first
          response.header.merge!(reply[1])
          halt reply.last
        end
      end
    end

    get "#{path}/?" do
      @visitors = Visitor.find(:all, :limit => 10)
      @experiments = Experiment.find(:all)
      erb :dashboard
    end
    
    get "#{path}/visitors" do
      @visitors = Visitor.find(:all)
    end
  
    get "#{path}/visitors/:id" do
      @visitor = Visitor.find(params["id"])
      erb :visitor
    end
    
    post "#{path}/experiments/:experiment_id/goals" do
      experiment = Experiment.find(params[:experiment_id])
      experiment.goals << Goal.create(params)
      redirect url("/experiments/#{experiment.id}")
    end
    
    get "#{path}/experiments/:experiment_id/goals/new" do
      @experiment = Experiment.find(params[:experiment_id])
      erb :new_goal
    end
    
    get "#{path}/experiments/:experiment_id/goals/:id/edit" do
      @experiment = Experiment.find(params[:experiment_id])
      @goal = Goal.find(params[:id])
      erb :edit_goal
    end
    
    post "#{path}/experiments/:experiment_id/goals/:id" do
      goal = Goal.find(params[:id])
      goal.name = params[:name]
      goal.path = params[:path]
      goal.save
      redirect url("/experiments/#{params[:experiment_id]}")
    end
    
    get "#{path}/experiments/:id" do
      @experiment = Experiment.find(params[:id])
      @goals = @experiment.goals
      @cohorts = @experiment.cohorts
      erb :experiment
    end
  
    helpers do
      define_method(:url) do |url|
        "#{path}#{url}"
      end

      include ::Rack::Utils
      alias_method :h, :escape_html
    end
  end
end