Method: Moonrope::DocServer#call

Defined in:
lib/moonrope/doc_server.rb

#call(env) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/moonrope/doc_server.rb', line 64

def call(env)
  if env['PATH_INFO'] =~ self.class.path_regex
    version = $1
    doc_path = $2
    request = Rack::Request.new(env)
    generator = Generator.new(@base, :host => "#{request.scheme}://#{request.host_with_port}", :version => version, :prefix => env['PATH_INFO'].split('/')[1])

    if @options[:reload_on_each_request]
      @base.load
    end

    file = nil
    content_type = nil

    case doc_path
    when nil, ""
      return [302, {'Location' => "#{env['PATH_INFO']}/welcome"}, ['']]
    when /\Awelcome\z/, /\Aindex\.html\z/
      file = generator.generate_file(doc_path, 'index')
    when /\Acontrollers\/(\w+)(\.html)?\z/
      if controller = @base.controller($1.to_sym)
        file = generator.generate_file(doc_path, 'controller', :controller => controller)
      end
    when /\Acontrollers\/(\w+)\/(\w+)(\.html)?\z/
      if controller = @base.controller($1.to_sym)
        if action = controller.action($2.to_sym)
          file = generator.generate_file(doc_path, 'action', :controller => controller, :action => action)
        end
      end
    when /\Astructures\/(\w+)(\.html)?\z/
      if structure = @base.structure($1.to_sym)
        file = generator.generate_file(doc_path, 'structure', :structure => structure)
      end
    when /\Aauthenticators\/(\w+)(\.html)?\z/
      if authenticator = @base.authenticators[$1.to_sym]
        file = generator.generate_file(doc_path, 'authenticator', :authenticator => authenticator)
      end
    when /\Aassets\/([\w]+)\.([a-z]+)\z/
      path = File.join(generator.template_root_path, 'assets', "#{$1}.#{$2}")
      if File.exist?(path)
        file = File.read(path)
        content_type = CONTENT_TYPES[$2] || 'text/plain'
      end
    end

    if file
      [200, {
        'Content-Type' => content_type || 'text/html',
        'Content-Length' => file.bytesize.to_s},
      [file]]
    else
      [404, {}, ['Not found']]
    end
  else
    return @app.call(env)
  end
end