drewish/rspec-rails-swagger

View on GitHub
lib/rspec/rails/swagger/response_formatters.rb

Summary

Maintainability
A
0 mins
Test Coverage
module RSpec
  module Rails
    module Swagger
      class ResponseFormatters

        class JSON
          def call(resp)
            if resp.kind_of? String
              ::JSON.parse(resp)
            else
              resp
            end
          rescue ::JSON::ParserError
            resp
          end
        end

        @formatters = {
          :default => ->(resp) { resp },
          'application/json' => JSON.new
        }

        class << self

          def register(format, callable)
            @formatters[format] = callable
          end

          def [](key)
            @formatters[key] || @formatters[:default]
          end
        end

      end
    end
  end
end