Class: Rack::API::Middleware::Format
- Inherits:
-
Object
- Object
- Rack::API::Middleware::Format
- Defined in:
- lib/rack/api/middleware/format.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, default_format, formats) ⇒ Format
constructor
A new instance of Format.
Constructor Details
#initialize(app, default_format, formats) ⇒ Format
Returns a new instance of Format.
5 6 7 8 9 |
# File 'lib/rack/api/middleware/format.rb', line 5 def initialize(app, default_format, formats) @app = app @default_format = default_format.to_s @formats = formats.collect {|f| f.to_s} end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rack/api/middleware/format.rb', line 11 def call(env) request = Rack::Request.new(env) params = request.env["rack.routing_args"].merge(request.params) requested_format = params.fetch(:format, @default_format) if @formats.include?(requested_format) @app.call(env) else [406, {"Content-Type" => "text/plain"}, ["Invalid format. Accepts one of [#{@formats.join(", ")}]"]] end end |