Class: Raddocs::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/raddocs/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
6
# File 'lib/raddocs/middleware.rb', line 3

def initialize(app)
  @app = app
  @file_server = Rack::File.new(Raddocs.configuration.docs_dir)
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/raddocs/middleware.rb', line 8

def call(env)
  if env["HTTP_ACCEPT"] =~ Raddocs.configuration.docs_mime_type
    env = env.merge({ "PATH_INFO" => File.join(env["PATH_INFO"], "index.txt") })
    response = @file_server.call(env)

    if response[0] == 404
      body = "Docs are not available for this resource.\n"
      response = [404, {"Content-Type" => "type/plain", "Content-Length" => body.size.to_s}, [body]]
    end

    response
  else
    @app.call(env)
  end
end