Sha256: a57242736abd00a7b635bd72d8855ef52210e98ccc0d128d0bebd6f347301b57
Contents?: true
Size: 1.96 KB
Versions: 2
Compression:
Stored size: 1.96 KB
Contents
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) @app = app @response_klass = Grape::Batch.configuration.formatter end def call(env) return @app.call(env) unless is_batch_request?(env) batch_call(env) end def batch_call(env) status = 200 headers = {'Content-Type' => 'application/json'} begin 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 end [status, headers, [body]] end private def is_batch_request?(env) env['PATH_INFO'].start_with?(Grape::Batch.configuration.path) && env['REQUEST_METHOD'] == 'POST' && env['CONTENT_TYPE'] == 'application/json' end def dispatch(env, batch_requests) request_env = env.dup batch_requests.map do |request| method = request['method'] path = request['path'] body = request['body'].is_a?(Hash) ? request['body'] : {} request_env['REQUEST_METHOD'] = method request_env['PATH_INFO'] = path if method == 'GET' request_env['rack.input'] = StringIO.new('{}') request_env['QUERY_STRING'] = URI.encode_www_form(body.to_a) else request_env['rack.input'] = StringIO.new(MultiJson.encode(body)) end status, headers, response = @app.call(request_env) @response_klass::format(status, headers, response) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
grape-batch-1.1.1 | lib/grape/batch.rb |
grape-batch-1.1.0 | lib/grape/batch.rb |