Class: Useless::Doc::Rack::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/useless/doc/rack/transform.rb

Overview

Doc::Rack::Transform takes the a JSON response and attempts to parse it via Doc::Serialization::Load and render it as HTML via the UI specified by env.

If the JSON cannot be parsed, the response will be a 502.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Transform

Returns a new instance of Transform.



14
15
16
# File 'lib/useless/doc/rack/transform.rb', line 14

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/useless/doc/rack/transform.rb', line 18

def call(env)
  unless env['useless.doc.ui']
    raise 'No UI specified.'
  end

  response = @app.call(env)

  if response[0] == 200
    begin
      if env['PATH_INFO'] == '/'
        api = Serialization::Load.api(response[2].first)
        html = env['useless.doc.ui'].api(api)
      else
        resource = Serialization::Load.resource(response[2].first)
        html = env['useless.doc.ui'].resource(resource)
      end

      [200, {'Content-Type' => 'text/html'}, [html]]
    rescue Oj::ParseError
      [502, {'Content-Type' => 'text/plain'}, ['Documentation JSON is malformed.']]
    end
  else
    response
  end
end