Class: Raddocs::Middleware

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

Overview

Rack middleware

This lets you cURL for documentation.

curl -H "Accept: text/docs+plain" http://localhost/orders

This will return all of the docs for a given resource, “orders.” It is returned as a giant flat file containing all of the documentation. “combined_text” output must be selected in ‘rspec_api_documentation`.

The route matches the folder structure of the docs.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



14
15
16
17
# File 'lib/raddocs/middleware.rb', line 14

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

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/raddocs/middleware.rb', line 19

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