Sha256: 2b1a5db1c12ae245ee7b7d7f9890bbdc71bf51d4094a2aec32cf3eb7b947ae3c

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

require 'useless/doc/serialization/load'

module Useless
  module Doc
    module Rack

      # +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['useless.doc.ui'].
      #
      # If the JSON cannot be parsed, the response will be a 502.
      #
      class Transform
        def initialize(app)
          @app = app
        end

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

          response = @app.call(env)

          if response[0] == 200
            begin
              resource = Serialization::Load.resource(response[2].first)
              html = env['useless.doc.ui'].html(resource)
              [200, {'Content-Type' => 'text/html'}, [html]]
            rescue Oj::ParseError
              [502, {'Content-Type' => 'text/plain'}, ['Documentation JSON is malformed.']]
            end
          else
            response
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
useless-doc-0.2.2 lib/useless/doc/rack/transform.rb
useless-doc-0.2.1 lib/useless/doc/rack/transform.rb
useless-doc-0.2.0 lib/useless/doc/rack/transform.rb
useless-doc-0.1.3 lib/useless/doc/rack/transform.rb
useless-doc-0.1.2 lib/useless/doc/rack/transform.rb