Class: TimePilot::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/time_pilot/web.rb

Overview

Rack application displaying member counts

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.appObject



48
49
50
# File 'lib/time_pilot/web.rb', line 48

def app
  @app ||= instance
end

.call(env) ⇒ Object



52
53
54
# File 'lib/time_pilot/web.rb', line 52

def call(env)
  app.call(env)
end

.instanceObject



61
62
63
64
65
66
# File 'lib/time_pilot/web.rb', line 61

def instance
  builder = Rack::Builder.new
  @middleware.each { |c, a, b| builder.use(c, *a, &b) }
  builder.run new
  builder.to_app
end

.reset!Object



68
69
70
# File 'lib/time_pilot/web.rb', line 68

def reset!
  @middleware = []
end

.use(middleware, *args, &block) ⇒ Object



56
57
58
59
# File 'lib/time_pilot/web.rb', line 56

def use(middleware, *args, &block)
  @app = nil
  @middleware << [middleware, args, block]
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
# File 'lib/time_pilot/web.rb', line 6

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/time_pilot/web.rb', line 10

def call!(env)
  req = Rack::Request.new(env)
  case req.path_info
  when '/'
    dashboard
  else
    not_found
  end
end

#dashboardObject

TODO: use Redis pipelining



21
22
23
24
25
26
27
28
29
# File 'lib/time_pilot/web.rb', line 21

def dashboard
  @total_count = TimePilot.features.map do |feature_name|
    counts = TimePilot.group_classes.map do |klass|
      [klass.to_s, klass.pilot_feature_cardinality(feature_name)]
    end.to_h
    [feature_name, counts]
  end.to_h
  erb :index
end

#erb(view) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/time_pilot/web.rb', line 35

def erb(view)
  [
    200,
    { 'Content-Type' => 'text/html' },
    [ERB.new(read_template(view)).result(binding)]
  ]
end

#not_foundObject



31
32
33
# File 'lib/time_pilot/web.rb', line 31

def not_found
  [404, { 'Content-Type' => 'text/html' }, ['Not Found']]
end

#read_template(view) ⇒ Object



43
44
45
# File 'lib/time_pilot/web.rb', line 43

def read_template(view)
  File.read(File.join(__dir__, '..', '..', 'web', "#{view}.html.erb"))
end