Sha256: 5273b46ced635f2b94d6fe563bd37b06cbae0202de13519aef026a556f40794a

Contents?: true

Size: 592 Bytes

Versions: 2

Compression:

Stored size: 592 Bytes

Contents

module Rack
  class API
    module Middleware
      class Format
        def initialize(app, formats)
          @app, @formats = app, formats.collect {|f| f.to_s}
        end

        def call(env)
          request = Rack::Request.new(env)
          params = request.env["rack.routing_args"].merge(request.params)
          requested_format = params.fetch(:format, "json")

          if @formats.include?(requested_format)
            @app.call(env)
          else
            [406, {"Content-Type" => "text/plain"}, ["Invalid format"]]
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-api-0.1.2 lib/rack/api/middleware/format.rb
rack-api-0.1.1 lib/rack/api/middleware/format.rb