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



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

def app
  @app ||= instance
end

.call(env) ⇒ Object



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

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

.instanceObject



65
66
67
68
69
70
71
72
73
# File 'lib/time_pilot/web.rb', line 65

def instance
  builder = Rack::Builder.new
  @middleware.each { |c, a, b| builder.use(c, *a, &b) }
  builder.use Rack::Static,
              urls: ['/stylesheets'],
              root: File.join(__dir__, '..', '..', 'web', 'assets')
  builder.run new
  builder.to_app
end

.reset!Object



75
76
77
# File 'lib/time_pilot/web.rb', line 75

def reset!
  @middleware = []
end

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



60
61
62
63
# File 'lib/time_pilot/web.rb', line 60

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)
  @request = Rack::Request.new(env)
  case @request.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', 'views', "#{view}.html.erb"))
end

#root_pathObject



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

def root_path
  "#{@request.env['SCRIPT_NAME']}/"
end