lib/grape/batch.rb in grape-batch-1.0.4 vs lib/grape/batch.rb in grape-batch-1.1.0
- old
+ new
@@ -1,19 +1,18 @@
require 'grape/batch/errors'
+require 'grape/batch/configuration'
require 'grape/batch/parser'
require 'grape/batch/response'
require 'grape/batch/version'
require 'multi_json'
module Grape
module Batch
class Base
- def initialize(app, opt = {})
+ def initialize(app)
@app = app
- @limit = opt[:limit] || 10
- @path = opt[:path] || '/batch'
- @response_klass = opt[:formatter] || Grape::Batch::Response
+ @response_klass = Grape::Batch.configuration.formatter
end
def call(env)
return @app.call(env) unless is_batch_request?(env)
batch_call(env)
@@ -22,11 +21,11 @@
def batch_call(env)
status = 200
headers = {'Content-Type' => 'application/json'}
begin
- batch_requests = Grape::Batch::Validator::parse(env, @limit)
+ batch_requests = Grape::Batch::Validator::parse(env, Grape::Batch.configuration.limit)
result = dispatch(env, batch_requests)
body = MultiJson.encode(result)
rescue Grape::Batch::RequestBodyError, Grape::Batch::TooManyRequestsError => e
e.class == TooManyRequestsError ? status = 429 : status = 400
body = e.message
@@ -36,10 +35,10 @@
end
private
def is_batch_request?(env)
- env['PATH_INFO'].start_with?(@path) &&
+ env['PATH_INFO'].start_with?(Grape::Batch.configuration.path) &&
env['REQUEST_METHOD'] == 'POST' &&
env['CONTENT_TYPE'] == 'application/json'
end
def dispatch(env, batch_requests)